Refine access control, peer access
[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.MLPPeer;
56 import org.acumos.cds.domain.MLPSolution;
57 import org.acumos.cds.domain.MLPSolutionRevision;
58 import org.acumos.cds.domain.MLPArtifact;
59
60
61
62 /**
63  */
64
65 //@RunWith(SpringJUnit4ClassRunner.class)
66 @RunWith(SpringRunner.class)
67 @SpringBootTest(classes = org.acumos.federation.gateway.Application.class,
68                                                                 webEnvironment = WebEnvironment.RANDOM_PORT,
69                                                                 properties = {
70                                                                         "federation.instance=adapter",
71                                                                         "federation.instance.name=Ghost",
72                                                                         "peersLocal.source=classpath:/test-peers.json",
73                                                                         "catalogLocal.source=classpath:/test-catalog.json",
74                                                                         "server.ssl.key-store=classpath:acumosa.pkcs12",
75                                                                         "server.ssl.key-store-password=acumosa",
76                                                                         "server.ssl.key-store-type=PKCS12",
77                                                                         "server.ssl.key-password = acumosa",
78                                                                         "server.ssl.trust-store=classpath:acumosTrustStore.jks",
79                                                                         "server.ssl.trust-store-password=acumos",
80                                                                         "server.ssl.client-auth=need"
81                                                                 })
82 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
83 public class PeerGatewayTest {
84
85         @Autowired
86         private TestRestTemplate restTemplate;
87
88         @Test
89         public void testSolutions() {
90
91     ((HttpComponentsClientHttpRequestFactory)
92                         this.restTemplate.getRestTemplate().getRequestFactory())
93                                 .setHttpClient(prepareHttpClient());
94                 
95                 ResponseEntity<JsonResponse<List<MLPSolution>>> response =
96                         this.restTemplate.exchange("/solutions", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolution>>>() {});
97                 
98                 if (response != null)   {
99                         System.out.println("testSolutions: " + response.getBody());
100                         System.out.println("testSolutions: " + response);
101                 }
102                 
103                 assertTrue(response != null);
104                 assertTrue(response.getStatusCodeValue() == 200);
105                 assertTrue(response.getBody().getResponseBody().size() == 1);
106         }
107
108
109         @Test
110         public void testSolutionSuccess() {
111
112     ((HttpComponentsClientHttpRequestFactory)
113                         this.restTemplate.getRestTemplate().getRequestFactory())
114                                 .setHttpClient(prepareHttpClient());
115
116                 ResponseEntity<JsonResponse<MLPSolution>> response =
117                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPSolution>>() {} );
118         
119                 if (response != null)   {
120                         System.out.println("testSolution: " + response.getBody());
121                         System.out.println("testSolution: " + response);
122                 }
123
124                 assertTrue(response != null);
125                 assertTrue(response.getStatusCodeValue() == 200);
126                 assertTrue(response.getBody().getResponseBody().getModelTypeCode().equals("CL")); //no errors
127         }
128
129         @Test
130         public void testSolutionRevisionsSuccess() {
131     
132                 ((HttpComponentsClientHttpRequestFactory)
133                         this.restTemplate.getRestTemplate().getRequestFactory())
134                                 .setHttpClient(prepareHttpClient());
135
136                 ResponseEntity<JsonResponse<List<MLPSolutionRevision>>> response =
137                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000/revisions", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPSolutionRevision>>>() {});
138                 
139                 if (response != null)   {
140                         System.out.println("testSolutionRevisions: " + response.getBody());
141                         System.out.println("testSolutionRevisions: " + response);
142                 }
143
144                 assertTrue(response != null);
145                 assertTrue(response.getStatusCodeValue() == 200);
146                 assertTrue(response.getBody().getResponseBody().size() == 1); //no errors
147         }
148
149         @Test
150         public void testSolutionRevisionSuccess() {
151
152     ((HttpComponentsClientHttpRequestFactory)
153                         this.restTemplate.getRestTemplate().getRequestFactory())
154                                 .setHttpClient(prepareHttpClient());
155
156                 ResponseEntity<JsonResponse<MLPSolution>> response =
157                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000/revisions/01010101-0101-0101-0101-010101010101", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<MLPSolution>>() {} );
158         
159                 if (response != null)   {
160                         System.out.println("testSolution: " + response.getBody());
161                         System.out.println("testSolution: " + response);
162                 }
163
164                 assertTrue(response != null);
165                 assertTrue(response.getStatusCodeValue() == 200);
166                 assertTrue(response.getBody().getResponseBody().getOwnerId().equals("admin")); //no errors
167         }
168
169         @Test
170         public void testSolutionRevisionArtifactsSuccess() {
171     
172                 ((HttpComponentsClientHttpRequestFactory)
173                         this.restTemplate.getRestTemplate().getRequestFactory())
174                                 .setHttpClient(prepareHttpClient());
175
176                 ResponseEntity<JsonResponse<List<MLPArtifact>>> response =
177                         this.restTemplate.exchange("/solutions/00000000-0000-0000-0000-000000000000/revisions/01010101-0101-0101-0101-010101010101/artifacts", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPArtifact>>>() {});
178                 
179                 if (response != null)   {
180                         System.out.println("testSolutionRevisionsArtifacts: " + response.getBody());
181                         System.out.println("testSolutionRevisionsArtifacts: " + response);
182                 }
183
184                 assertTrue(response != null);
185                 assertTrue(response.getStatusCodeValue() == 200);
186                 assertTrue(response.getBody().getResponseBody().size() == 1); //no errors
187         }
188
189         @Test
190         public void testPeersForbidden() {
191
192     ((HttpComponentsClientHttpRequestFactory)
193                         this.restTemplate.getRestTemplate().getRequestFactory())
194                                 .setHttpClient(prepareHttpClient());
195
196                 ResponseEntity<JsonResponse<List<MLPPeer>>> response =
197                         this.restTemplate.exchange("/peers", HttpMethod.GET, prepareRequest(), new ParameterizedTypeReference<JsonResponse<List<MLPPeer>>>() {} );
198         
199                 if (response != null)   {
200                         System.out.println("testPeers: " + response.getBody());
201                         System.out.println("testPeers: " + response);
202                 }
203
204                 assertTrue(response.getStatusCodeValue() == 403);
205         }
206
207         private HttpEntity prepareRequest(String theResourceName) {
208                 String content = new Scanner(
209                                                                            Thread.currentThread().getContextClassLoader().getResourceAsStream(theResourceName), "UTF-8")
210                                                                                         .useDelimiter("\\Z").next();
211
212                 HttpHeaders headers = new HttpHeaders();
213                 headers.setContentType(MediaType.APPLICATION_JSON);
214                 return new HttpEntity<String>(content, headers);
215         }
216         
217         private HttpEntity prepareRequest() {
218                 HttpHeaders headers = new HttpHeaders();
219                 headers.setContentType(MediaType.APPLICATION_JSON);
220                 return new HttpEntity<String>(headers);
221         }
222
223         private HttpClient prepareHttpClient() {
224                 return new HttpClientConfigurationBuilder()
225                                                                 .withSSL(new SSLBuilder()
226                                                                                                                         .withKeyStore("classpath:/acumosb.pkcs12")
227                                                                                                                         .withKeyStorePassword("acumosb")
228                                                                                                                         //.withKeyPassword("acumosb")
229                                                                                                                         .withTrustStore("classpath:/acumosTrustStore.jks")
230                                                                                                                         .withTrustStoreType("JKS")
231                                                                                                                         .withTrustStorePassword("acumos")
232                                                                                                                         .build())
233                                                                 .buildConfig()
234                                                                 .buildClient();
235         }
236 }
237