SDN Controller Blueprints Rest Adaptor 83/64483/1
authorSingal, Kapil (ks220y) <ks220y@att.com>
Tue, 4 Sep 2018 17:24:11 +0000 (13:24 -0400)
committerSingal, Kapil (ks220y) <ks220y@att.com>
Tue, 4 Sep 2018 17:24:11 +0000 (13:24 -0400)
Creating SDN Controller Blueprints Rest Adaptor Junit Tests

Change-Id: Id388b0905d570a75b6842cc69b52f2d26e3b060e
Issue-ID: CCSDK-512
Signed-off-by: Singal, Kapil (ks220y) <ks220y@att.com>
blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapterTest.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientServiceTest.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/SSLClientServiceTest.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/utils/RestTemplateFactoryTest.java [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/config-rest-adaptor.properties [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/keystore.client.p12 [new file with mode: 0644]
blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/truststore.client.jks [new file with mode: 0644]

diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapterTest.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/AbstractConfigRestClientAdapterTest.java
new file mode 100644 (file)
index 0000000..e698567
--- /dev/null
@@ -0,0 +1,53 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import java.io.FileInputStream;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Properties;\r
+import java.util.stream.Collectors;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+\r
+public class AbstractConfigRestClientAdapterTest {\r
+    \r
+    Map<String, String> properties = new HashMap<>();\r
+    \r
+    @Before\r
+    public void setup() throws Exception {\r
+        String propertyfile = "src/test/resources/config-rest-adaptor.properties";\r
+        \r
+        Properties restProperties = new Properties();\r
+        restProperties.load(new FileInputStream(propertyfile));\r
+        \r
+        properties.putAll(restProperties.entrySet().stream()\r
+                .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString())));\r
+    }\r
+    \r
+    @Test\r
+    public void testInitGenericRestClient() throws Exception {\r
+        ConfigRestClientServiceAdapter genericRestClient = new GenericRestClientAdapterImpl(properties, "modelservice");\r
+        Assert.assertNotNull(genericRestClient);\r
+    }\r
+    \r
+    @Test\r
+    public void testInitSSLClient() throws Exception {\r
+        ConfigRestClientServiceAdapter sslClient = new SSLRestClientAdapterImpl(properties, "aai");\r
+        Assert.assertNotNull(sslClient);\r
+    }\r
+    \r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientServiceTest.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/GenericRestClientServiceTest.java
new file mode 100644 (file)
index 0000000..65a9042
--- /dev/null
@@ -0,0 +1,116 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import static org.powermock.api.mockito.PowerMockito.mock;\r
+import static org.powermock.api.mockito.PowerMockito.when;\r
+import static org.powermock.api.mockito.PowerMockito.whenNew;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mockito;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+import org.springframework.http.HttpMethod;\r
+import org.springframework.http.HttpStatus;\r
+import org.springframework.http.ResponseEntity;\r
+import org.springframework.web.client.RestTemplate;\r
+\r
+@SuppressWarnings("unchecked")\r
+@RunWith(PowerMockRunner.class)\r
+@PowerMockIgnore("javax.net.ssl.*")\r
+@PrepareForTest({AbstractConfigRestClientAdapter.class})\r
+public class GenericRestClientServiceTest {\r
+    \r
+    ConfigRestAdaptorService configRestAdaptorService;\r
+    \r
+    RestTemplate mockRestTemplate = mock(RestTemplate.class);\r
+    \r
+    String path = "path";\r
+    \r
+    @Before\r
+    public void before() throws Exception {\r
+        whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);\r
+        \r
+        String propertyDir = "src/test/resources";\r
+        configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);\r
+    }\r
+    \r
+    @Test\r
+    public void testGetResource() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.getResource("modelservice", path, String.class);\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test\r
+    public void testPostResource() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test\r
+    public void testExchange() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testGetResourceError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);\r
+        \r
+        configRestAdaptorService.getResource("modelservice", path, String.class);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testPostResourceError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))\r
+                .thenReturn(response);\r
+        \r
+        configRestAdaptorService.postResource("modelservice", path, null, String.class);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testExchangeError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        configRestAdaptorService.exchangeResource("modelservice", path, null, String.class, "GET");\r
+    }\r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/SSLClientServiceTest.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/service/SSLClientServiceTest.java
new file mode 100644 (file)
index 0000000..4592de8
--- /dev/null
@@ -0,0 +1,116 @@
+/*\r
+ * Copyright © 2017-2018 AT&T Intellectual Property.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except\r
+ * in compliance with the License. You may obtain a copy of the License at\r
+ * \r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software distributed under the License\r
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express\r
+ * or implied. See the License for the specific language governing permissions and limitations under\r
+ * the License.\r
+ */\r
+\r
+package org.onap.ccsdk.config.rest.adaptor.service;\r
+\r
+import static org.powermock.api.mockito.PowerMockito.mock;\r
+import static org.powermock.api.mockito.PowerMockito.when;\r
+import static org.powermock.api.mockito.PowerMockito.whenNew;\r
+import org.junit.Assert;\r
+import org.junit.Before;\r
+import org.junit.Test;\r
+import org.junit.runner.RunWith;\r
+import org.mockito.Matchers;\r
+import org.mockito.Mockito;\r
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;\r
+import org.powermock.core.classloader.annotations.PowerMockIgnore;\r
+import org.powermock.core.classloader.annotations.PrepareForTest;\r
+import org.powermock.modules.junit4.PowerMockRunner;\r
+import org.springframework.http.HttpMethod;\r
+import org.springframework.http.HttpStatus;\r
+import org.springframework.http.ResponseEntity;\r
+import org.springframework.web.client.RestTemplate;\r
+\r
+@SuppressWarnings("unchecked")\r
+@RunWith(PowerMockRunner.class)\r
+@PowerMockIgnore("javax.net.ssl.*")\r
+@PrepareForTest({AbstractConfigRestClientAdapter.class})\r
+public class SSLClientServiceTest {\r
+    \r
+    ConfigRestAdaptorService configRestAdaptorService;\r
+    \r
+    RestTemplate mockRestTemplate = mock(RestTemplate.class);\r
+    \r
+    String path = "path";\r
+    \r
+    @Before\r
+    public void before() throws Exception {\r
+        whenNew(RestTemplate.class).withAnyArguments().thenReturn(mockRestTemplate);\r
+        \r
+        String propertyDir = "src/test/resources";\r
+        configRestAdaptorService = new ConfigRestAdaptorServiceImpl(propertyDir);\r
+    }\r
+    \r
+    @Test\r
+    public void testGetResource() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.getResource("aai", path, String.class);\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test\r
+    public void testPostResource() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.POST), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.postResource("aai", path, null, String.class);\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test\r
+    public void testExchange() throws Exception {\r
+        String responseBody = "sampleBodyString";\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>(responseBody, HttpStatus.OK);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        String body = configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");\r
+        \r
+        Assert.assertEquals(responseBody, body);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testGetResourceError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.getForEntity(Matchers.endsWith(path), Matchers.any())).thenReturn(response);\r
+        \r
+        configRestAdaptorService.getResource("aai", path, String.class);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testPostResourceError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.postForEntity(Matchers.endsWith(path), Matchers.anyObject(), Matchers.any()))\r
+                .thenReturn(response);\r
+        \r
+        configRestAdaptorService.postResource("aai", path, null, String.class);\r
+    }\r
+    \r
+    @Test(expected = ConfigRestAdaptorException.class)\r
+    public void testExchangeError() throws Exception {\r
+        ResponseEntity<Object> response = new ResponseEntity<Object>("", HttpStatus.INTERNAL_SERVER_ERROR);\r
+        when(mockRestTemplate.exchange(Matchers.endsWith(path), Matchers.eq(HttpMethod.GET), Matchers.any(),\r
+                Matchers.any(Class.class))).thenReturn(response);\r
+        \r
+        configRestAdaptorService.exchangeResource("aai", path, null, String.class, "GET");\r
+    }\r
+}\r
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/utils/RestTemplateFactoryTest.java b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/java/org/onap/ccsdk/config/rest/adaptor/utils/RestTemplateFactoryTest.java
new file mode 100644 (file)
index 0000000..a01630d
--- /dev/null
@@ -0,0 +1,32 @@
+package org.onap.ccsdk.config.rest.adaptor.utils;
+
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorConstants;
+import org.onap.ccsdk.config.rest.adaptor.ConfigRestAdaptorException;
+import org.onap.ccsdk.config.rest.adaptor.service.ConfigRestAdaptorServiceImpl;
+
+@SuppressWarnings("squid:S2187")
+public class RestTemplateFactoryTest {
+    
+    public static void main(String[] args) {
+        
+        String propertyFile = RestTemplateFactoryTest.class.getClassLoader().getResource(".").getPath();
+        System.out.println(" Property : " + propertyFile);
+        
+        try {
+            ConfigRestAdaptorServiceImpl configRestAdaptorServiceImpl = new ConfigRestAdaptorServiceImpl(propertyFile);
+            String restconfResponse = genericRestGetMDSALOperation(args, configRestAdaptorServiceImpl);
+            System.out.println("RestTemplateFactoryTest.main Completed with response :" + restconfResponse);
+        } catch (ConfigRestAdaptorException e) {
+            e.printStackTrace();
+        }
+    }
+    
+    public static String genericRestGetMDSALOperation(String[] args,
+            ConfigRestAdaptorServiceImpl configRestAdaptorServiceImpl) throws ConfigRestAdaptorException {
+        String path = "config/Dummy-API:services/service-list/dummy-1234";
+        String restconfResponse = configRestAdaptorServiceImpl.getResource(ConfigRestAdaptorConstants.SELECTOR_RESTCONF,
+                path, String.class);
+        return restconfResponse;
+    }
+    
+}
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/config-rest-adaptor.properties b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/config-rest-adaptor.properties
new file mode 100644 (file)
index 0000000..725e484
--- /dev/null
@@ -0,0 +1,29 @@
+#
+# Configuration file for SDNC Controller Module
+#
+
+org.onap.ccsdk.config.rest.adaptors.envtype=solo
+
+# Config Generator Microservices
+org.onap.ccsdk.config.rest.adaptors.modelservice.type=generic
+org.onap.ccsdk.config.rest.adaptors.modelservice.enable=true
+org.onap.ccsdk.config.rest.adaptors.modelservice.url=http://localhost:8080/configgenerator/service/
+org.onap.ccsdk.config.rest.adaptors.modelservice.user=admin
+org.onap.ccsdk.config.rest.adaptors.modelservice.passwd=admin
+
+# Generic RESTCONF Adaptor
+org.onap.ccsdk.config.rest.adaptors.restconf.type=generic
+org.onap.ccsdk.config.rest.adaptors.restconf.enable=true
+org.onap.ccsdk.config.rest.adaptors.restconf.user=admin
+org.onap.ccsdk.config.rest.adaptors.restconf.passwd=admin
+org.onap.ccsdk.config.rest.adaptors.restconf.url=http://localhost:8181/restconf/
+
+# SSL AAI Adaptor
+org.onap.ccsdk.config.rest.adaptors.aai.propertyfile=aai.properties
+org.onap.ccsdk.config.rest.adaptors.aai.type=ssl
+org.onap.ccsdk.config.rest.adaptors.aai.enable=true
+org.onap.ccsdk.config.rest.adaptors.aai.url=https://localhost:8443/onap-aai/
+org.onap.ccsdk.config.rest.adaptors.aai.ssl.trust=src/test/resources/truststore.client.jks
+org.onap.ccsdk.config.rest.adaptors.aai.ssl.trust.psswd=changeme
+org.onap.ccsdk.config.rest.adaptors.aai.ssl.key=src/test/resources/keystore.client.p12
+org.onap.ccsdk.config.rest.adaptors.aai.ssl.key.psswd=changeme
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/keystore.client.p12 b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/keystore.client.p12
new file mode 100644 (file)
index 0000000..c28c8cb
Binary files /dev/null and b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/keystore.client.p12 differ
diff --git a/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/truststore.client.jks b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/truststore.client.jks
new file mode 100644 (file)
index 0000000..d38a5e5
Binary files /dev/null and b/blueprints-processor/adaptors/rest-adaptor-provider/src/test/resources/truststore.client.jks differ