Create based CSIT test for SO-CNFM - Simulator Changes.
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / test / java / org / onap / so / sdcsimulator / controller / CatalogControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.so.sdcsimulator.controller;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import java.util.Base64;
26 import java.util.Set;
27 import java.util.UUID;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.so.sdcsimulator.models.ResourceAssetInfo;
31 import org.onap.so.sdcsimulator.models.ResourceMetadata;
32 import org.onap.so.sdcsimulator.models.ServiceAssetInfo;
33 import org.onap.so.sdcsimulator.models.ServiceMetadata;
34 import org.onap.so.sdcsimulator.utils.Constants;
35 import org.onap.so.simulator.model.UserCredentials;
36 import org.springframework.beans.factory.annotation.Autowired;
37 import org.springframework.boot.test.context.SpringBootTest;
38 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
39 import org.springframework.boot.test.web.client.TestRestTemplate;
40 import org.springframework.boot.web.server.LocalServerPort;
41 import org.springframework.context.annotation.Configuration;
42 import org.springframework.core.ParameterizedTypeReference;
43 import org.springframework.http.HttpEntity;
44 import org.springframework.http.HttpHeaders;
45 import org.springframework.http.HttpMethod;
46 import org.springframework.http.HttpStatus;
47 import org.springframework.http.MediaType;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.test.context.ActiveProfiles;
50 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
51
52 /**
53  * @author Waqas Ikram (waqas.ikram@est.tech)
54  *
55  */
56 @RunWith(SpringJUnit4ClassRunner.class)
57 @ActiveProfiles("test")
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
59 @Configuration
60 public class CatalogControllerTest {
61
62     private static final String SUB_CATEGORY = "Network Service";
63
64     private static final String DISTRIBUTION_STATUS = "DISTRIBUTED";
65
66     private static final String SERVICE_ID = "9bb8c882-44a1-4b67-a12c-5a998e18d6ba";
67
68     private static final String RESOURCE_ID = "73522444-e8e9-49c1-be29-d355800aa349";
69
70     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
71
72     @LocalServerPort
73     private int port;
74
75     @Autowired
76     private TestRestTemplate restTemplate;
77
78     @Autowired
79     private UserCredentials userCredentials;
80
81     @Test
82     public void test_getResourceCsar_validCsarId_matchContent() {
83
84         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/toscaModel";
85
86         final ResponseEntity<byte[]> response =
87                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
88
89         assertEquals(HttpStatus.OK, response.getStatusCode());
90         assertTrue(response.hasBody());
91         assertEquals(117247, response.getBody().length);
92
93     }
94
95     @Test
96     public void test_getServiceCsar_validCsarId_matchContent() {
97
98         final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/toscaModel";
99
100         final ResponseEntity<byte[]> response =
101                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
102
103         assertEquals(HttpStatus.OK, response.getStatusCode());
104         assertTrue(response.hasBody());
105         assertEquals(147255, response.getBody().length);
106
107     }
108
109     @Test
110     public void test_getResources_validResourcesFromClassPath() {
111
112         final ResponseEntity<Set<ResourceAssetInfo>> response =
113                 restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
114                         new ParameterizedTypeReference<Set<ResourceAssetInfo>>() {});
115
116         assertEquals(HttpStatus.OK, response.getStatusCode());
117         assertTrue(response.hasBody());
118         assertEquals(2, response.getBody().size());
119         assertTrue(response.getBody().stream().anyMatch(predicate -> SUB_CATEGORY.equals(predicate.getSubCategory())));
120
121     }
122
123     @Test
124     public void test_getServices_validServicesFromClassPath() {
125
126         final ResponseEntity<Set<ServiceAssetInfo>> response =
127                 restTemplate.exchange(getBaseUrl() + "/services", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
128                         new ParameterizedTypeReference<Set<ServiceAssetInfo>>() {});
129
130         assertEquals(HttpStatus.OK, response.getStatusCode());
131         assertTrue(response.hasBody());
132         assertEquals(2, response.getBody().size());
133         assertTrue(response.getBody().stream()
134             .anyMatch(predicate -> DISTRIBUTION_STATUS.equals(predicate.getDistributionStatus())));
135
136     }
137
138     @Test
139     public void test_getResourceCsar_invalidCsar_internalServerError() {
140         final String url = getBaseUrl() + "/resources/" + UUID.randomUUID().toString() + "/toscaModel";
141
142         final ResponseEntity<byte[]> response =
143                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
144
145
146         assertFalse(response.hasBody());
147         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
148     }
149
150     @Test
151     public void test_getResourceMetadata_validMetadataFileInClasspath_matchContent() {
152
153         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/metadata";
154
155         final ResponseEntity<ResourceMetadata> response =
156                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ResourceMetadata.class);
157
158
159         assertEquals(HttpStatus.OK, response.getStatusCode());
160         assertTrue(response.hasBody());
161         final ResourceMetadata actual = response.getBody();
162         assertEquals(8, actual.getResources().size());
163         assertEquals(3, actual.getArtifacts().size());
164         assertEquals(SUB_CATEGORY, actual.getSubCategory());
165
166     }
167
168     @Test
169     public void test_getServiceMetadata_validMetadataFileInClasspath_matchContent() {
170
171         final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/metadata";
172
173         final ResponseEntity<ServiceMetadata> response =
174                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceMetadata.class);
175
176
177         assertEquals(HttpStatus.OK, response.getStatusCode());
178         assertTrue(response.hasBody());
179         final ServiceMetadata actual = response.getBody();
180         assertEquals(1, actual.getResources().size());
181         assertEquals(1, actual.getArtifacts().size());
182         assertEquals(DISTRIBUTION_STATUS, actual.getDistributionStatus());
183
184     }
185
186
187     private String getBaseUrl() {
188         return "http://localhost:" + port + Constants.CATALOG_URL;
189     }
190
191     private HttpHeaders getHttpHeaders() {
192         final HttpHeaders requestHeaders = new HttpHeaders();
193         requestHeaders.add("Authorization", getBasicAuth(userCredentials.getUsers().get(0).getUsername()));
194         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
195         return requestHeaders;
196     }
197
198     private String getBasicAuth(final String username) {
199         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
200     }
201
202 }