c10fc46b87cf32b743899acfe6ebad4ee844a3c1
[federation.git] / gateway / src / test / java / org / acumos / federation / gateway / test / PeerGatewayTest.java
1 /*-
2  * ===============LICENSE_START=======================================================
3  * Acumos
4  * ===================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
6  * ===================================================================================
7  * This Acumos software file is distributed by AT&T and Tech Mahindra
8  * under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * This file is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ===============LICENSE_END=========================================================
19  */
20 package org.acumos.federation.gateway.test;
21
22 import java.util.List;
23 import java.util.Scanner;
24
25 import static org.junit.Assert.assertTrue;
26 import static org.assertj.core.api.Assertions.assertThat; 
27
28 import org.junit.FixMethodOrder;
29 import org.junit.runners.MethodSorters;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
35 import org.springframework.test.context.junit4.SpringRunner;
36 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
37 import org.springframework.test.context.web.WebAppConfiguration;
38 import org.springframework.boot.test.web.client.TestRestTemplate;
39
40 import org.springframework.http.HttpMethod;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.HttpHeaders;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
46 import org.springframework.core.ParameterizedTypeReference;
47
48 import org.apache.http.client.HttpClient;
49
50 /* this is not good for unit testing .. */
51 import org.acumos.federation.gateway.common.JsonResponse;
52 import org.acumos.federation.gateway.common.HttpClientConfigurationBuilder;
53 import static org.acumos.federation.gateway.common.HttpClientConfigurationBuilder.SSLBuilder;
54
55 import org.acumos.cds.domain.MLPSolution;
56 import org.acumos.cds.domain.MLPSolutionRevision;
57 import org.acumos.cds.domain.MLPArtifact;
58
59
60
61 /**
62  */
63
64 //@RunWith(SpringJUnit4ClassRunner.class)
65 @RunWith(SpringRunner.class)
66 @SpringBootTest(classes = org.acumos.federation.gateway.Application.class,
67                                                                 webEnvironment = WebEnvironment.RANDOM_PORT,
68                                                                 properties = {
69                                                                         "federation.instance=adapter",
70                                                                         "federation.instance.name=Ghost",
71                                                                         "peersLocal.source=classpath:/test-peers.json",
72                                                                         "catalogLocal.source=classpath:/test-catalog.json",
73                                                                         "server.ssl.key-store=classpath:acumosa.pkcs12",
74                                                                         "server.ssl.key-store-password=acumosa",
75                                                                         "server.ssl.key-store-type=PKCS12",
76                                                                         "server.ssl.key-password = acumosa",
77                                                                         "server.ssl.trust-store=classpath:acumosTrustStore.jks",
78                                                                         "server.ssl.trust-store-password=acumos",
79                                                                         "server.ssl.client-auth=need"
80                                                                 })
81 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
82 public class PeerGatewayTest {
83
84         @Autowired
85         private TestRestTemplate restTemplate;
86
87         @Test
88         public void testSolutions() {
89
90     ((HttpComponentsClientHttpRequestFactory)
91                         this.restTemplate.getRestTemplate().getRequestFactory())
92                                 .setHttpClient(prepareHttpClient());
93                 
94                 ResponseEntity<JsonResponse<List<MLPSolution>>> response =
95                         this.restTemplate.exchange("/solutions", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolution>>>() {});
96                 
97                 if (response != null)   {
98                         System.out.println("testSolutions: " + response.getBody());
99                         System.out.println("testSolutions: " + response);
100                 }
101                 
102                 assertTrue(response != null);
103                 assertTrue(response.getStatusCodeValue() == 200);
104                 assertTrue(response.getBody().getResponseBody().size() == 1);
105         }
106
107 /*
108         @Test
109         public void testSolutionSuccess() {
110
111     ((HttpComponentsClientHttpRequestFactory)
112                         this.restTemplate.getRestTemplate().getRequestFactory())
113                                 .setHttpClient(prepareHttpClient());
114
115                 ResponseEntity<JsonResponse<MLPSolution>> response =
116                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPSolution>>() {} );
117         
118                 if (response != null)   {
119                         System.out.println("testSolution: " + response.getBody());
120                         System.out.println("testSolution: " + response);
121                 }
122
123                 assertTrue(response != null);
124                 assertTrue(response.getStatusCodeValue() == 200);
125                 assertTrue(response.getBody().getResponseBody().getModelTypeCode().equals("CL")); //no errors
126         }
127 */
128         @Test
129         public void testSolutionRevisionsSuccess() {
130     
131                 ((HttpComponentsClientHttpRequestFactory)
132                         this.restTemplate.getRestTemplate().getRequestFactory())
133                                 .setHttpClient(prepareHttpClient());
134
135                 ResponseEntity<JsonResponse<List<MLPSolutionRevision>>> response =
136                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000/revisions", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolutionRevision>>>() {});
137                 
138                 if (response != null)   {
139                         System.out.println("testSolutionRevisions: " + response.getBody());
140                         System.out.println("testSolutionRevisions: " + response);
141                 }
142
143                 assertTrue(response != null);
144                 assertTrue(response.getStatusCodeValue() == 200);
145                 assertTrue(response.getBody().getResponseBody().size() == 1); //no errors
146         }
147
148         @Test
149         public void testSolutionRevisionArtifactsSuccess() {
150     
151                 ((HttpComponentsClientHttpRequestFactory)
152                         this.restTemplate.getRestTemplate().getRequestFactory())
153                                 .setHttpClient(prepareHttpClient());
154
155                 ResponseEntity<JsonResponse<List<MLPArtifact>>> response =
156                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000/revisions/01010101-0101-0101-0101-010101010101", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPArtifact>>>() {});
157                 
158                 if (response != null)   {
159                         System.out.println("testSolutionRevisionsArtifacts: " + response.getBody());
160                         System.out.println("testSolutionRevisionsArtifacts: " + response);
161                 }
162
163                 assertTrue(response != null);
164                 assertTrue(response.getStatusCodeValue() == 200);
165                 assertTrue(response.getBody().getResponseBody().size() == 1); //no errors
166         }
167
168         private HttpEntity prepareRequest(String theResourceName) {
169                 String content = new Scanner(
170                                                                            Thread.currentThread().getContextClassLoader().getResourceAsStream(theResourceName), "UTF-8")
171                                                                                         .useDelimiter("\\Z").next();
172
173                 HttpHeaders headers = new HttpHeaders();
174                 headers.setContentType(MediaType.APPLICATION_JSON);
175                 return new HttpEntity<String>(content, headers);
176         }
177         
178         private HttpEntity prepareRequest() {
179                 HttpHeaders headers = new HttpHeaders();
180                 headers.setContentType(MediaType.APPLICATION_JSON);
181                 return new HttpEntity<String>(headers);
182         }
183
184         private HttpClient prepareHttpClient() {
185                 return new HttpClientConfigurationBuilder()
186                                                                 .withSSL(new SSLBuilder()
187                                                                                                                         .withKeyStore("classpath:/acumosb.pkcs12")
188                                                                                                                         .withKeyStorePassword("acumosb")
189                                                                                                                         //.withKeyPassword("acumosb")
190                                                                                                                         .withTrustStore("classpath:/acumosTrustStore.jks")
191                                                                                                                         .withTrustStoreType("JKS")
192                                                                                                                         .withTrustStorePassword("acumos")
193                                                                                                                         .build())
194                                                                 .buildConfig()
195                                                                 .buildClient();
196         }
197 }
198