Unit tests for local interface 07/1607/4
authorSerban Jora <sj2381@att.com>
Fri, 13 Apr 2018 13:52:05 +0000 (09:52 -0400)
committerLott, Christopher (cl778h) <cl778h@att.com>
Fri, 13 Apr 2018 19:59:19 +0000 (15:59 -0400)
Add license to pom file.

Change-Id: I12b969c5ccdcd347369224b3a6e7de6717e6ebc0
Issue-ID: ACUMOS-572
Signed-off-by: Serban Jora <sj2381@att.com>
gateway/pom.xml
gateway/src/main/java/org/acumos/federation/gateway/config/LocalConfiguration.java
gateway/src/test/java/org/acumos/federation/gateway/test/LocalControllerTest.java [new file with mode: 0644]
gateway/src/test/java/org/acumos/federation/gateway/test/LocalTester.java [new file with mode: 0644]
gateway/src/test/resources/mockPeerPeersResponse.json [new file with mode: 0644]
gateway/src/test/resources/mockPeerPingResponse.json [new file with mode: 0644]
gateway/src/test/resources/mockPeerSolutionResponse.json [new file with mode: 0644]
gateway/src/test/resources/mockPeerSubscriptionResponse.json [new file with mode: 0644]
gateway/src/test/resources/test-peers.json

index 89519c8..8a7c452 100644 (file)
@@ -1,4 +1,23 @@
 <?xml version="1.0"?>
+<!--
+===============LICENSE_START=======================================================
+Acumos Apache-2.0
+===================================================================================
+Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
+===================================================================================
+This Acumos software file is distributed by AT&T and Tech Mahindra
+under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+This file is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+===============LICENSE_END=========================================================
+-->
 <project
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
        xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
@@ -24,7 +43,7 @@
                <start-class>org.acumos.federation.gateway.Application</start-class>
                <!-- Jenkins invokes mvn with argument -Dbuild.number=${BUILD_NUMBER} -->
                <build.number>0</build.number>
-               <skipTests>true</skipTests>
+               <skipTests>false</skipTests>
                <!-- dependencies versions -->
                <slf4j.version>1.7.25</slf4j.version>
                <logback.version>1.2.3</logback.version>
index d1c847f..84c7514 100644 (file)
@@ -1,8 +1,8 @@
 /*-
  * ===============LICENSE_START=======================================================
- * Acumos
+ * Acumos Apache-2.0
  * ===================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
  * ===================================================================================
  * This Acumos software file is distributed by AT&T and Tech Mahindra
  * under the Apache License, Version 2.0 (the "License");
@@ -41,12 +41,13 @@ import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletCon
 
 import org.acumos.federation.gateway.config.EELFLoggerDelegate;
 import org.acumos.federation.gateway.controller.PeerPingController;
+import org.acumos.federation.gateway.controller.PeerPeersController;
 import org.acumos.federation.gateway.controller.PeerCatalogController;
 import org.acumos.federation.gateway.controller.PeerSubscriptionController;
 import org.acumos.federation.gateway.security.AuthenticationConfiguration;
 
 /**
- * Provides the neans used in interactions with the local Acumos system
+ * Provides the beans used in interactions with the local Acumos system
  */
 @Configuration
 @Import(AuthenticationConfiguration.class)
@@ -75,6 +76,12 @@ public class LocalConfiguration /* implements ApplicationContextAware */ {
        public PeerSubscriptionController peerSubscriptionServer() {
                return new PeerSubscriptionController();
        }
+       
+       @Bean
+       public PeerPeersController peerPeersServer() {
+               return new PeerPeersController();
+       }
+
        /**
    * Build a client for interacting with other local Acumos components
         * through the local interface.
diff --git a/gateway/src/test/java/org/acumos/federation/gateway/test/LocalControllerTest.java b/gateway/src/test/java/org/acumos/federation/gateway/test/LocalControllerTest.java
new file mode 100644 (file)
index 0000000..b9e497c
--- /dev/null
@@ -0,0 +1,423 @@
+/*-
+ * ===============LICENSE_START=======================================================
+ * Acumos Apache-2.0
+ * ===================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
+ * ===================================================================================
+ * This Acumos software file is distributed by AT&T and Tech Mahindra
+ * under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ===============LICENSE_END=========================================================
+ */
+package org.acumos.federation.gateway.test;
+
+import java.util.List;
+import java.util.Scanner;
+import java.io.IOException;
+
+import static org.mockito.Mockito.*;
+import static org.mockito.Matchers.*;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.mockito.stubbing.Answer;
+import org.mockito.invocation.InvocationOnMock;
+
+import org.junit.FixMethodOrder;
+import org.junit.runners.MethodSorters;
+import org.junit.Test;
+import org.junit.Before;
+import org.junit.runner.RunWith;
+import static org.junit.Assert.assertTrue;
+import static org.assertj.core.api.Assertions.assertThat; 
+
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.client.HttpClientErrorException;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.test.context.ContextHierarchy;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.test.context.web.WebAppConfiguration;
+import org.springframework.boot.context.embedded.LocalServerPort;
+import org.springframework.boot.test.web.client.TestRestTemplate;
+import org.springframework.boot.test.mock.mockito.MockBean;
+
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.core.io.ClassPathResource;
+
+import org.apache.http.ProtocolVersion;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.message.BasicStatusLine;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+
+import org.acumos.federation.gateway.common.Clients;
+import org.acumos.federation.gateway.common.FederationClient;
+import org.acumos.federation.gateway.config.EELFLoggerDelegate;
+import org.acumos.federation.gateway.common.JsonResponse;
+import org.acumos.federation.gateway.config.InterfaceConfigurationBuilder;
+import static org.acumos.federation.gateway.config.InterfaceConfigurationBuilder.SSLBuilder;
+
+import org.acumos.cds.domain.MLPPeer;
+import org.acumos.cds.domain.MLPSolution;
+import org.acumos.cds.domain.MLPSolutionRevision;
+import org.acumos.cds.domain.MLPArtifact;
+
+
+
+/**
+ * Contains tests of the local interface of a federation gateway
+ */
+
+//@RunWith(SpringJUnit4ClassRunner.class)
+@RunWith(SpringRunner.class)
+@ContextHierarchy({
+       @ContextConfiguration(classes = org.acumos.federation.gateway.test.TestAdapterConfiguration.class),
+       @ContextConfiguration(classes = org.acumos.federation.gateway.config.LocalConfiguration.class)
+})
+@SpringBootTest(classes = org.acumos.federation.gateway.Application.class,
+                                                               webEnvironment = WebEnvironment.DEFINED_PORT,
+                                                               properties = {
+                                                                       "federation.instance=adapter",
+                                                                       "federation.instance.name=test",
+                                                                       "federation.operator=admin",
+                                                                       "peersLocal.source=classpath:test-peers.json",
+                                                                       "catalogLocal.source=classpath:test-catalog.json",
+                                                                       "federation.server.port=${random.int[49152,65535]}",
+                                                                       "federation.ssl.key-store=classpath:acumosa.pkcs12",
+                                                                       "federation.ssl.key-store-password=acumosa",
+                                                                       "federation.ssl.key-store-type=PKCS12",
+                                                                       "federation.ssl.key-password = acumosa",
+                                                                       "federation.ssl.trust-store=classpath:acumosTrustStore.jks",
+                                                                       "federation.ssl.trust-store-password=acumos",
+                                                                       "local.server.port=${random.int[49152,65535]}",
+                                                                       "local.ssl.key-store=classpath:acumosa.pkcs12",
+                                                                       "local.ssl.key-store-password=acumosa",
+                                                                       "local.ssl.key-store-type=PKCS12",
+                                                                       "local.ssl.key-password = acumosa",
+                                                                       "local.ssl.trust-store=classpath:acumosTrustStore.jks",
+                                                                       "local.ssl.trust-store-password=acumos"
+                                                               })
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class LocalControllerTest {
+
+       private final EELFLoggerDelegate log = EELFLoggerDelegate.getLogger(getClass().getName());
+//     @Autowired
+//     private TestRestTemplate restTemplate;
+
+       private RestTemplate    restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory());
+       
+       @Value("${local.server.port}")
+       private int localPort;
+
+       @MockBean(name = "federationClient")
+       private HttpClient      federationClient;
+
+       @MockBean(name = "clients")
+       private Clients clients;
+
+       @Before
+       public void initLocalTests() throws IOException {
+
+               MockitoAnnotations.initMocks(this);
+
+               BasicHttpResponse mockSolutionsResponse = 
+                       new BasicHttpResponse(
+                               new BasicStatusLine(
+                                       new ProtocolVersion("HTTP",1,1), 200, "Success"));
+               ClassPathResource mockSolutions =
+                       new ClassPathResource("mockPeerSolutionsResponse.json");
+               mockSolutionsResponse.setEntity(
+                       new InputStreamEntity(mockSolutions.getInputStream()));
+               mockSolutionsResponse
+                       .addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
+               mockSolutionsResponse
+                       .addHeader("Content-Length", String.valueOf(mockSolutions.contentLength()));
+
+               BasicHttpResponse mockSolutionResponse = 
+                       new BasicHttpResponse(
+                               new BasicStatusLine(
+                                       new ProtocolVersion("HTTP",1,1), 200, "Success"));
+               ClassPathResource mockSolution =
+                       new ClassPathResource("mockPeerSolutionResponse.json");
+               mockSolutionResponse.setEntity(
+                       new InputStreamEntity(mockSolution.getInputStream()));
+               mockSolutionResponse
+                       .addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
+               mockSolutionResponse
+                       .addHeader("Content-Length", String.valueOf(mockSolution.contentLength()));
+
+               BasicHttpResponse mockPingResponse = 
+                       new BasicHttpResponse(
+                               new BasicStatusLine(
+                                       new ProtocolVersion("HTTP",1,1), 200, "Success"));
+               ClassPathResource mockPing =
+                       new ClassPathResource("mockPeerPingResponse.json");
+               mockPingResponse.setEntity(
+                       new InputStreamEntity(mockPing.getInputStream()));
+               mockPingResponse
+                       .addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
+               mockPingResponse
+                       .addHeader("Content-Length", String.valueOf(mockPing.contentLength()));
+
+               BasicHttpResponse mockPeersResponse = 
+                       new BasicHttpResponse(
+                               new BasicStatusLine(
+                                       new ProtocolVersion("HTTP",1,1), 200, "Success"));
+               ClassPathResource mockPeers =
+                       new ClassPathResource("mockPeerPeersResponse.json");
+               mockPeersResponse.setEntity(
+                       new InputStreamEntity(mockPeers.getInputStream()));
+               mockPeersResponse
+                       .addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
+               mockPeersResponse
+                       .addHeader("Content-Length", String.valueOf(mockPeers.contentLength()));
+
+               BasicHttpResponse mockSubscriptionResponse = 
+                       new BasicHttpResponse(
+                               new BasicStatusLine(
+                                       new ProtocolVersion("HTTP",1,1), 200, "Success"));
+               ClassPathResource mockSubscription =
+                       new ClassPathResource("mockPeerSubscriptionResponse.json");
+               mockSubscriptionResponse.setEntity(
+                       new InputStreamEntity(mockSubscription.getInputStream()));
+               mockSubscriptionResponse
+                       .addHeader("Content-Type", ContentType.APPLICATION_JSON.toString());
+               mockSubscriptionResponse
+                       .addHeader("Content-Length", String.valueOf(mockSubscription.contentLength()));
+
+               when(
+                       this.federationClient.execute(
+                               any(HttpUriRequest.class), any(HttpContext.class)
+                       )
+               ).thenAnswer(new Answer<HttpResponse>() {
+                               public HttpResponse answer(InvocationOnMock theInvocation) {
+                                       HttpUriRequest req = (HttpUriRequest)
+                                               theInvocation.getArguments()[0];
+                                       String path = req.getURI().getPath();
+                                       log.warn("Mock path " + path);
+                                       if (path.equals("/solutions"))
+                                               return mockSolutionsResponse;
+                                       if (path.endsWith("/ping"))
+                                               return mockPingResponse;
+                                       if (path.endsWith("/peers"))
+                                               return mockPeersResponse;
+                                       if (path.contains("/solutions/"))
+                                               return mockSolutionResponse;
+                                       if (path.contains("/subscription/"))
+                                               return mockSubscriptionResponse;
+
+                                       log.warn(" *** Mock unhandled path " + path);
+                                       return null;
+                               }
+                       });
+
+               when(
+                       this.clients.getFederationClient(
+                               any(String.class)
+                       )
+               )
+               .thenAnswer(new Answer<FederationClient>() {
+                       public FederationClient answer(InvocationOnMock theInvocation) {
+                               //this should end up providing a client based on the mocked http
+                               //client
+                               log.warn("Mock client for " + theInvocation.getArguments()[0]);
+                         return new FederationClient(
+                  (String)theInvocation.getArguments()[0]/*the URI*/,
+                  federationClient);
+                       }
+               });
+       }
+
+       @Test
+       public void testPeerSolutions() {
+
+    ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+               
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111111/solutions";
+
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerSolutions: {}", url);
+               ResponseEntity<JsonResponse<List<MLPSolution>>> response =
+                       this.restTemplate.exchange(url, HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolution>>>() {});
+               
+               if (response != null)   {
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerSolutions: {}", response.getBody());
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerSolutions: {}", response);
+               }
+               
+               assertTrue(response != null);
+               assertTrue(response.getStatusCodeValue() == 200);
+               assertTrue(response.getBody().getContent().size() == 1);
+       }
+
+
+       @Test
+       public void testPeerSolution() {
+
+    ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111111/solutions/00000000-0000-0000-0000-000000000000";
+
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerSolution: {}", url);
+               ResponseEntity<JsonResponse<MLPSolution>> response =
+                       this.restTemplate.exchange(url, HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPSolution>>() {} );
+       
+               if (response != null)   {
+                       log.info(EELFLoggerDelegate.debugLogger, "testSolution: {}", response.getBody());
+                       log.info(EELFLoggerDelegate.debugLogger, "testSolution: {}", response);
+               }
+
+               assertTrue(response != null);
+               assertTrue(response.getStatusCodeValue() == 200);
+               assertTrue(response.getBody().getContent().getModelTypeCode().equals("CL")); //no errors
+       }
+
+       @Test
+       public void testPeerPing() {
+    
+               ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111111/ping";
+
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerPing: {}", url);
+               ResponseEntity<JsonResponse<MLPPeer>> response =
+                       this.restTemplate.exchange(url, HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPPeer>>() {});
+               
+               if (response != null)   {
+                       log.info(EELFLoggerDelegate.debugLogger, "testPing: {}", response.getBody());
+                       log.info(EELFLoggerDelegate.debugLogger, "testPing: {}", response);
+               }
+
+               assertTrue(response != null);
+               assertTrue(response.getStatusCodeValue() == 200);
+               assertTrue(response.getBody().getContent().getPeerId().equals("11111111-1111-1111-1111-111111111111")); //no errors
+       }
+
+       @Test
+       public void testPeerPeers() {
+
+    ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111111/peers";
+               
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerPeers: {}", url);
+               ResponseEntity<JsonResponse<List<MLPPeer>>> response =
+                       this.restTemplate.exchange(url, HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPPeer>>>() {} );
+       
+               if (response != null)   {
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerPeers: {}", response.getBody());
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerPeers: {}", response);
+               }
+
+               assertTrue(response != null);
+               assertTrue(response.getStatusCodeValue() == 200);
+               assertTrue(response.getBody().getContent().get(0).getName().startsWith("acumos")); //no errors
+       }
+
+       @Test
+       public void testPeerSubscription() {
+    
+               ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111111/subscription/1";
+
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerSubscription: {}", url);
+               ResponseEntity<JsonResponse<String>> response =
+                       this.restTemplate.exchange(url, HttpMethod.POST, prepareRequest(), new ParameterizedTypeReference<JsonResponse<String>>() {});
+               
+               if (response != null)   {
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerSubscription: {}", response.getBody());
+                       log.info(EELFLoggerDelegate.debugLogger, "testPeerSubscription: {}", response);
+               }
+
+               assertTrue(response != null);
+               assertTrue(response.getStatusCodeValue() == 200);
+       }
+
+       @Test
+       public void testPeerNoSuch() {
+
+    ((HttpComponentsClientHttpRequestFactory)
+                       this.restTemplate.getRequestFactory())
+                               .setHttpClient(prepareHttpClient());
+
+               String url = "https://localhost:" + this.localPort + "/peer/11111111-1111-1111-1111-111111111112/ping";
+       
+               log.info(EELFLoggerDelegate.debugLogger, "testPeerNoSuch: {}", url);
+               try {
+                       this.restTemplate.exchange(url, HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPPeer>>() {} );
+                       assertTrue("expected to fail", true);
+               }
+               catch (HttpClientErrorException httpx) {
+                       assertTrue(httpx.getStatusCode().value() == 404);
+               }
+       }
+
+
+       private HttpEntity prepareRequest(String theResourceName) {
+               String content = new Scanner(
+                                                                          Thread.currentThread().getContextClassLoader().getResourceAsStream(theResourceName), "UTF-8")
+                                                                                       .useDelimiter("\\Z").next();
+
+               HttpHeaders headers = new HttpHeaders();
+               headers.setContentType(MediaType.APPLICATION_JSON);
+               return new HttpEntity<String>(content, headers);
+       }
+       
+       private HttpEntity prepareRequest() {
+               HttpHeaders headers = new HttpHeaders();
+               headers.setContentType(MediaType.APPLICATION_JSON);
+               return new HttpEntity<String>(headers);
+       }
+
+       private HttpClient prepareHttpClient() {
+               return new InterfaceConfigurationBuilder()
+                                                               .withSSL(new SSLBuilder()
+                                                                                                                       .withKeyStore("classpath:/acumosa.pkcs12")
+                                                                                                                       .withKeyStorePassword("acumosa")
+                                                                                                                       //.withKeyPassword("acumosa")
+                                                                                                                       .withTrustStore("classpath:/acumosTrustStore.jks")
+                                                                                                                       .withTrustStoreType("JKS")
+                                                                                                                       .withTrustStorePassword("acumos")
+                                                                                                                       .build())
+                                                               .buildConfig()
+                                                               .buildClient();
+       }
+}
+
diff --git a/gateway/src/test/java/org/acumos/federation/gateway/test/LocalTester.java b/gateway/src/test/java/org/acumos/federation/gateway/test/LocalTester.java
new file mode 100644 (file)
index 0000000..a836478
--- /dev/null
@@ -0,0 +1,120 @@
+/*-
+ * ===============LICENSE_START=======================================================
+ * Acumos Apache-2.0
+ * ===================================================================================
+ * Copyright (C) 2017-2018 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
+ * ===================================================================================
+ * This Acumos software file is distributed by AT&T and Tech Mahindra
+ * under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * This file is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ===============LICENSE_END=========================================================
+ */
+package org.acumos.federation.gateway.test;
+
+import java.util.List;
+import java.util.Scanner;
+import java.util.Collections;
+
+import org.springframework.web.client.RestTemplate;
+
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.EventListener;
+import org.springframework.context.annotation.Bean;
+
+import org.springframework.boot.test.mock.mockito.MockBean;
+
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.client.SimpleClientHttpRequestFactory;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.core.ParameterizedTypeReference;
+import org.springframework.core.io.ClassPathResource;
+
+import org.apache.http.ProtocolVersion;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpRequest;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.ResponseHandler;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.protocol.HttpContext;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.message.BasicStatusLine;
+import org.apache.http.entity.InputStreamEntity;
+import org.apache.http.entity.ByteArrayEntity;
+import org.apache.http.entity.ContentType;
+
+/* this is not good for unit testing .. */
+import org.acumos.federation.gateway.common.JsonResponse;
+import org.acumos.federation.gateway.config.InterfaceConfigurationBuilder;
+import static org.acumos.federation.gateway.config.InterfaceConfigurationBuilder.SSLBuilder;
+
+import org.acumos.cds.domain.MLPPeer;
+import org.acumos.cds.domain.MLPSolution;
+
+
+
+/**
+ * Standalone tester of the local interface
+ */
+public class LocalTester {
+
+
+       /**
+        */
+       public static void main(String[] theArgs) throws Exception {
+
+               RestTemplate template = new RestTemplate(
+                                                                                                                       new HttpComponentsClientHttpRequestFactory(
+                                                                                                                               prepareHttpClient(theArgs[0])));
+
+               ResponseEntity<JsonResponse<MLPPeer>> pingResponse =
+                       template.exchange(theArgs[1] + "/ping", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPPeer>>() {});
+               
+               if (pingResponse != null)       {
+                       System.out.println("testPeerPing: " + pingResponse);
+               }
+
+
+               ResponseEntity<JsonResponse<List<MLPSolution>>> solutionsResponse =
+                       template.exchange(theArgs[1] + "/solutions", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolution>>>() {});
+               
+               if (solutionsResponse != null)  {
+                       System.out.println("testPeerSolutions: " + solutionsResponse);
+               }
+
+       }
+
+       private static HttpEntity prepareRequest() {
+               HttpHeaders headers = new HttpHeaders();
+               headers.setContentType(MediaType.APPLICATION_JSON);
+               return new HttpEntity<String>(headers);
+       }
+
+       private static HttpClient prepareHttpClient(String theTestSystem) {
+               return new InterfaceConfigurationBuilder()
+                                                               .withSSL(new SSLBuilder()
+                                                                                                                       .withKeyStore("classpath:/acumos" + theTestSystem + ".pkcs12")
+                                                                                                                       .withKeyStorePassword("acumos" + theTestSystem)
+                                                                                                                       //.withKeyPassword("acumosb")
+                                                                                                                       .withTrustStore("classpath:/acumosTrustStore.jks")
+                                                                                                                       .withTrustStoreType("JKS")
+                                                                                                                       .withTrustStorePassword("acumos")
+                                                                                                                       .build())
+                                                               .buildConfig()
+                                                               .buildClient();
+       }
+}
diff --git a/gateway/src/test/resources/mockPeerPeersResponse.json b/gateway/src/test/resources/mockPeerPeersResponse.json
new file mode 100644 (file)
index 0000000..99b451c
--- /dev/null
@@ -0,0 +1,14 @@
+{
+ "message": "Success",
+ "content": [
+       {
+       "peerId":"11111111-1111-1111-1111-111111111111",
+         "name":"acumosb",
+               "subjectName":"gateway.acumosb.org",
+               "description":"acumosb",
+               "statusCode": "AC",
+               "self": "false",
+               "apiUrl":"https://localhost:9002"
+       }
+ ]
+}
diff --git a/gateway/src/test/resources/mockPeerPingResponse.json b/gateway/src/test/resources/mockPeerPingResponse.json
new file mode 100644 (file)
index 0000000..f4b1a69
--- /dev/null
@@ -0,0 +1,12 @@
+{
+ "message": "Success",
+ "content": {
+  "peerId":"11111111-1111-1111-1111-111111111111",
+  "name":"acumosb",
+       "subjectName":"gateway.acumosb.org",
+       "description":"acumosb",
+  "statusCode": "AC",
+  "self": "false",
+       "apiUrl":"https://localhost:9002"
+ }
+}
diff --git a/gateway/src/test/resources/mockPeerSolutionResponse.json b/gateway/src/test/resources/mockPeerSolutionResponse.json
new file mode 100644 (file)
index 0000000..e44b06b
--- /dev/null
@@ -0,0 +1,16 @@
+{
+ "message": "Success",
+ "content": {
+  "solutionId":"6793411f-c7a1-4e93-85bc-f91d267541d8",
+  "name":"mock model",
+  "description":"Test mock model",
+  "ownerId":"admin",
+  "active":"true",
+  "modelTypeCode":"CL",
+  "toolkitTypeCode":"",
+  "validationStatusCode":"",
+  "metadata":"acumosa",
+  "created":"2017-08-10",
+  "modified":"2017-08-11"
+ }
+}
diff --git a/gateway/src/test/resources/mockPeerSubscriptionResponse.json b/gateway/src/test/resources/mockPeerSubscriptionResponse.json
new file mode 100644 (file)
index 0000000..7be0fe3
--- /dev/null
@@ -0,0 +1,4 @@
+{
+ "message": "Success",
+ "content": "Will do"
+}
index 9225bf6..f43df71 100644 (file)
     "statusCode": "AC",
     "self": "false",
                "apiUrl":"https://localhost:9002",
-               "subscriptions":[]
+               "subscriptions":[
+                       {
+                               "subId":1,
+                               "peerId":"11111111-1111-1111-1111-111111111111",
+                               "selector":"{\"modelTypeCode\":\"CL,PR\"}",
+                               "refreshInterval":"120",
+                               "maxArtifactSize":"2048",
+                               "created":"2017-09-21",
+                               "modified":"2017-10-10"
+                       }
+               ]
        }
 ]