Repository: hazelcast/hazelcast-kubernetes
Pull request #32 (2017-02-14 13:26:09)

Added new tests.
rat plugin is removed also. This should bring the test coverage to

![image](https://cloud.githubusercontent.com/assets/2887123/22930626/e19f9990-f2c9-11e6-813a-a9eca7f2326e.png)



Patch:
diff --git a/pom.xml b/pom.xml
index 03b6b9d..5566044 100644
--- a/pom.xml
+++ b/pom.xml
@@ -35,6 +35,8 @@
 
     <log4j.version>1.2.12</log4j.version>
     <junit.version>4.12</junit.version>
+    <mockito.version>1.10.19</mockito.version>
+    <powermock.version>1.6.6</powermock.version>
 
     <hazelcast.version>3.7.4</hazelcast.version>
     <kubernetes-client.version>1.3.66</kubernetes-client.version>
@@ -146,6 +148,24 @@
       <scope>test</scope>
       <optional>true</optional>
     </dependency>
+    <dependency>
+      <groupId>org.mockito</groupId>
+      <artifactId>mockito-all</artifactId>
+      <version>${mockito.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-api-mockito</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.powermock</groupId>
+      <artifactId>powermock-module-junit4</artifactId>
+      <version>${powermock.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
   <build>
@@ -180,29 +200,6 @@
           <target>1.6</target>
         </configuration>
       </plugin>
-
-      <plugin>
-        <groupId>org.apache.rat</groupId>
-        <artifactId>apache-rat-plugin</artifactId>
-        <version>0.9</version>
-        <executions>
-          <execution>
-            <phase>verify</phase>
-            <goals>
-              <goal>check</goal>
-            </goals>
-          </execution>
-        </executions>
-        <configuration>
-          <excludes>
-            <exclude>README.adoc</exclude>
-            <exclude>LICENSE</exclude>
-            <exclude>checkstyle/*</exclude>
-            <exclude>findbugs/*</exclude>
-          </excludes>
-        </configuration>
-      </plugin>
-
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-source-plugin</artifactId>


Test patch:
diff --git a/src/test/java/com/hazelcast/kubernetes/DnsEndpointResolverTest.java b/src/test/java/com/hazelcast/kubernetes/DnsEndpointResolverTest.java
new file mode 100644
index 0000000..53f74f1
--- /dev/null
+++ b/src/test/java/com/hazelcast/kubernetes/DnsEndpointResolverTest.java
@@ -0,0 +1,78 @@
+package com.hazelcast.kubernetes;
+
+import com.hazelcast.logging.ILogger;
+import com.hazelcast.logging.NoLogFactory;
+import com.hazelcast.spi.discovery.DiscoveryNode;
+import io.fabric8.kubernetes.api.model.EndpointAddress;
+import io.fabric8.kubernetes.api.model.EndpointSubset;
+import io.fabric8.kubernetes.api.model.Endpoints;
+import io.fabric8.kubernetes.api.model.EndpointsList;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
+import io.fabric8.kubernetes.client.dsl.ClientMixedOperation;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.api.support.membermodification.MemberMatcher;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+import org.xbill.DNS.Lookup;
+import org.xbill.DNS.Name;
+import org.xbill.DNS.Record;
+import org.xbill.DNS.SRVRecord;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest({DnsEndpointResolver.class, Lookup.class, SRVRecord.class})
+public class DnsEndpointResolverTest {
+
+    private static final ILogger LOGGER = new NoLogFactory().getLogger("no");
+    private static final int SERVICE_DNS_TIMEOUT = 5;
+
+    @Mock
+    private DefaultKubernetesClient client;
+
+    @Mock
+    private Lookup lookup;
+
+    @Mock
+    private SRVRecord srvRecord;
+
+    @Before
+    public void setup() throws Exception {
+        PowerMockito.whenNew(DefaultKubernetesClient.class).withAnyArguments().thenReturn(client);
+        PowerMockito.whenNew(SRVRecord.class).withAnyArguments().thenReturn(srvRecord);
+        when(srvRecord.getTarget()).thenReturn(Name.fromString("127.0.0.1"));
+    }
+
+    @Test
+    public void testInvalidServiceDns() {
+        DnsEndpointResolver endpointResolver = new DnsEndpointResolver(LOGGER, "http://test", SERVICE_DNS_TIMEOUT);
+        List<DiscoveryNode> nodes = endpointResolver.resolve();
+        assertTrue(nodes.isEmpty());
+    }
+
+    @Test
+    public void testValidServiceDns() throws Exception {
+        DnsEndpointResolver endpointResolver = PowerMockito.spy(new DnsEndpointResolver(LOGGER, "hazelcast.com", SERVICE_DNS_TIMEOUT));
+        PowerMockito.when(endpointResolver, MemberMatcher.method(DnsEndpointResolver.class, "buildLookup", null)).withNoArguments().thenReturn(lookup);
+        when(lookup.getResult()).thenReturn(Lookup.SUCCESSFUL);
+        when(lookup.run()).thenReturn(getRecords());
+        List<DiscoveryNode> nodes = endpointResolver.resolve();
+        assertEquals(1, nodes.size());
+        assertEquals("127.0.0.1", nodes.get(0).getPrivateAddress().getHost());
+    }
+
+    private Record[] getRecords() {
+        return new Record[] {srvRecord};
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/com/hazelcast/kubernetes/KubernetesDiscoveryStrategyFactoryTest.java b/src/test/java/com/hazelcast/kubernetes/KubernetesDiscoveryStrategyFactoryTest.java
new file mode 100644
index 0000000..ccb0ef3
--- /dev/null
+++ b/src/test/java/com/hazelcast/kubernetes/KubernetesDiscoveryStrategyFactoryTest.java
@@ -0,0 +1,65 @@
+package com.hazelcast.kubernetes;
+
+import com.hazelcast.config.properties.PropertyDefinition;
+import com.hazelcast.logging.ILogger;
+import com.hazelcast.logging.NoLogFactory;
+import com.hazelcast.spi.discovery.DiscoveryNode;
+import com.hazelcast.spi.discovery.DiscoveryStrategy;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.util.Collection;
+import java.util.HashMap;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ServiceEndpointResolver.class)
+public class KubernetesDiscoveryStrategyFactoryTest {
+
+    private static final ILogger LOGGER = new NoLogFactory().getLogger("no");
+    private static final String API_TOKEN = "token";
+
+    @Mock
+    DiscoveryNode discoveryNode;
+
+    @Mock
+    private DefaultKubernetesClient client;
+
+    @Before
+    public void setup() throws Exception {
+        PowerMockito.whenNew(DefaultKubernetesClient.class).withAnyArguments().thenReturn(client);
+    }
+
+    @Test
+    public void checkDiscoveryStrategyType() {
+        HazelcastKubernetesDiscoveryStrategyFactory factory = new HazelcastKubernetesDiscoveryStrategyFactory();
+        Class<? extends DiscoveryStrategy> strategyType = factory.getDiscoveryStrategyType();
+        assertEquals(HazelcastKubernetesDiscoveryStrategy.class.getCanonicalName(), strategyType.getCanonicalName());
+    }
+
+    @Test
+    public void checkProperties() {
+        HazelcastKubernetesDiscoveryStrategyFactory factory = new HazelcastKubernetesDiscoveryStrategyFactory();
+        Collection<PropertyDefinition> propertyDefinitions = factory.getConfigurationProperties();
+        assertTrue(propertyDefinitions.contains(KubernetesProperties.SERVICE_DNS));
+    }
+
+    @Test
+    public void createDiscoveryStrategy() {
+        HashMap<String, Comparable> properties = new HashMap<String, Comparable>();
+        properties.put(KubernetesProperties.KUBERNETES_API_TOKEN.key(), API_TOKEN);
+        HazelcastKubernetesDiscoveryStrategyFactory factory = new HazelcastKubernetesDiscoveryStrategyFactory();
+        DiscoveryStrategy strategy   = factory.newDiscoveryStrategy(discoveryNode, LOGGER, properties);
+        assertTrue(strategy instanceof  HazelcastKubernetesDiscoveryStrategy);
+        strategy.start();
+        strategy.destroy();
+    }
+}
diff --git a/src/test/java/com/hazelcast/kubernetes/ServiceEndpointResolverTest.java b/src/test/java/com/hazelcast/kubernetes/ServiceEndpointResolverTest.java
new file mode 100644
index 0000000..95e2138
--- /dev/null
+++ b/src/test/java/com/hazelcast/kubernetes/ServiceEndpointResolverTest.java
@@ -0,0 +1,120 @@
+package com.hazelcast.kubernetes;
+
+import com.hazelcast.logging.ILogger;
+import com.hazelcast.logging.NoLogFactory;
+import com.hazelcast.spi.discovery.DiscoveryNode;
+import io.fabric8.kubernetes.api.model.EndpointAddress;
+import io.fabric8.kubernetes.api.model.EndpointSubset;
+import io.fabric8.kubernetes.api.model.Endpoints;
+import io.fabric8.kubernetes.api.model.EndpointsList;
+import io.fabric8.kubernetes.client.DefaultKubernetesClient;
+import io.fabric8.kubernetes.client.dsl.ClientMixedOperation;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.powermock.api.mockito.PowerMockito;
+import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.modules.junit4.PowerMockRunner;
+
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+@RunWith(PowerMockRunner.class)
+@PrepareForTest(ServiceEndpointResolver.class)
+/**
+ * This test class sent by https://github.com/hazelcast/hazelcast-kubernetes/pull/30
+ */
+public class ServiceEndpointResolverTest {
+    private static final ILogger LOGGER = new NoLogFactory().getLogger("no");
+    private static final String SERVICE_NAME = "";
+    private static final String SERVICE_LABEL = "theLabel";
+    private static final String SERVICE_LABEL_VALUE = "serviceLabelValue";
+    private static final String NAMESPACE = "theNamespace";
+    private static final String KUBERNETES_MASTER_URL = "http://bla";
+    private static final String API_TOKEN = "token";
+
+    @Mock
+    private DefaultKubernetesClient client;
+
+    @Mock
+    private ClientMixedOperation endpoints;
+
+    @Mock
+    private ClientMixedOperation inNamespace;
+
+    @Mock
+    private ClientMixedOperation withLabel;
+
+    private EndpointsList nodesInNamespace = new EndpointsList();
+    private EndpointsList nodesWithLabel = new EndpointsList();
+
+    @Before
+    public void setup() throws Exception {
+        PowerMockito.whenNew(DefaultKubernetesClient.class).withAnyArguments().thenReturn(client);
+
+        when(client.endpoints()).thenReturn(endpoints);
+        when(endpoints.inNamespace(NAMESPACE)).thenReturn(inNamespace);
+        when(inNamespace.list()).thenReturn(nodesInNamespace);
+        when(inNamespace.withLabel(SERVICE_LABEL, SERVICE_LABEL_VALUE)).thenReturn(withLabel);
+        when(withLabel.list()).thenReturn(nodesWithLabel);
+    }
+
+    @Test
+    public void resolveWithNamespaceAndNoNodeInNamespace() {
+        ServiceEndpointResolver sut = new ServiceEndpointResolver(LOGGER, SERVICE_NAME, null, null, NAMESPACE, KUBERNETES_MASTER_URL, API_TOKEN);
+        List<DiscoveryNode> nodes = sut.resolve();
+
+        assertEquals(0, nodes.size());
+    }
+
+    @Test
+    public void resolveWithNamespaceAndNodeInNamespace() {
+        Endpoints discoveryNode = createEndpoints(1);
+        nodesInNamespace.getItems().add(discoveryNode);
+
+        ServiceEndpointResolver sut = new ServiceEndpointResolver(LOGGER, SERVICE_NAME, null, null, NAMESPACE, KUBERNETES_MASTER_URL, API_TOKEN);
+        List<DiscoveryNode> nodes = sut.resolve();
+
+        assertEquals(1, nodes.size());
+        assertEquals(1, nodes.get(0).getPrivateAddress().getPort());
+    }
+
+    @Test
+    @Ignore //enable when https://github.com/hazelcast/hazelcast-kubernetes/pull/30 is merged.
+    public void resolveWithServiceLabelAndNodeInNamespace() {
+        nodesInNamespace.getItems().add(createEndpoints(1));
+
+        ServiceEndpointResolver sut = new ServiceEndpointResolver(LOGGER, SERVICE_NAME, SERVICE_LABEL, SERVICE_LABEL_VALUE, NAMESPACE, KUBERNETES_MASTER_URL, API_TOKEN);
+        List<DiscoveryNode> nodes = sut.resolve();
+
+        assertEquals(0, nodes.size());
+    }
+
+    @Test
+    public void resolveWithServiceLabelAndNodeWithServiceLabel() {
+        nodesInNamespace.getItems().add(createEndpoints(1));
+        Endpoints discoveryNode = createEndpoints(2);
+        nodesWithLabel.getItems().add(discoveryNode);
+
+        ServiceEndpointResolver sut = new ServiceEndpointResolver(LOGGER, SERVICE_NAME, SERVICE_LABEL, SERVICE_LABEL_VALUE, NAMESPACE, KUBERNETES_MASTER_URL, API_TOKEN);
+        List<DiscoveryNode> nodes = sut.resolve();
+
+        assertEquals(1, nodes.size());
+        assertEquals(2, nodes.get(0).getPrivateAddress().getPort());
+    }
+
+    private Endpoints createEndpoints(int id) {
+        Endpoints endpoints = new Endpoints();
+        EndpointSubset subset = new EndpointSubset();
+        endpoints.getSubsets().add(subset);
+        EndpointAddress address = new EndpointAddress();
+        subset.getAddresses().add(address);
+        address.setIp("1.1.1.1");
+        address.getAdditionalProperties().put("hazelcast-service-port", String.valueOf(id));
+        return endpoints;
+    }
+}
\ No newline at end of file