57db74c35f3e1ee523d5e9a0398173b2be9192a2
[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.Metadata;
31 import org.onap.so.sdcsimulator.models.ResourceAssetInfo;
32 import org.onap.so.sdcsimulator.models.ResourceMetadata;
33 import org.onap.so.sdcsimulator.models.ServiceAssetInfo;
34 import org.onap.so.sdcsimulator.models.ServiceMetadata;
35 import org.onap.so.sdcsimulator.utils.Constants;
36 import org.onap.so.simulator.model.UserCredentials;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.boot.test.context.SpringBootTest;
39 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
40 import org.springframework.boot.test.web.client.TestRestTemplate;
41 import org.springframework.boot.web.server.LocalServerPort;
42 import org.springframework.context.annotation.Configuration;
43 import org.springframework.core.ParameterizedTypeReference;
44 import org.springframework.http.HttpEntity;
45 import org.springframework.http.HttpHeaders;
46 import org.springframework.http.HttpMethod;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.MediaType;
49 import org.springframework.http.ResponseEntity;
50 import org.springframework.test.context.ActiveProfiles;
51 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
52
53 /**
54  * @author Waqas Ikram (waqas.ikram@est.tech)
55  *
56  */
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 @Configuration
61 public class CatalogControllerTest {
62
63     private static final String SUB_CATEGORY = "Network Service";
64
65     private static final String DISTRIBUTION_STATUS = "DISTRIBUTED";
66
67     private static final String SERVICE_ID = "9bb8c882-44a1-4b67-a12c-5a998e18d6ba";
68
69     private static final String RESOURCE_ID = "73522444-e8e9-49c1-be29-d355800aa349";
70
71     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
72
73     @LocalServerPort
74     private int port;
75
76     @Autowired
77     private TestRestTemplate restTemplate;
78
79     @Autowired
80     private UserCredentials userCredentials;
81
82     @Test
83     public void test_getCsar_validCsarId_matchContent() {
84
85         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/toscaModel";
86
87         final ResponseEntity<byte[]> response =
88                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
89
90         assertEquals(HttpStatus.OK, response.getStatusCode());
91         assertTrue(response.hasBody());
92         assertEquals(117247, response.getBody().length);
93
94     }
95
96     @Test
97     public void test_getResources_validResourcesFromClassPath() {
98
99         final ResponseEntity<Set<ResourceAssetInfo>> response =
100                 restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
101                         new ParameterizedTypeReference<Set<ResourceAssetInfo>>() {});
102
103         assertEquals(HttpStatus.OK, response.getStatusCode());
104         assertTrue(response.hasBody());
105         assertEquals(1, response.getBody().size());
106         assertEquals(SUB_CATEGORY, response.getBody().iterator().next().getSubCategory());
107
108     }
109
110     @Test
111     public void test_getServices_validServicesFromClassPath() {
112
113         final ResponseEntity<Set<ServiceAssetInfo>> response =
114                 restTemplate.exchange(getBaseUrl() + "/services", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
115                         new ParameterizedTypeReference<Set<ServiceAssetInfo>>() {});
116
117         assertEquals(HttpStatus.OK, response.getStatusCode());
118         assertTrue(response.hasBody());
119         assertEquals(1, response.getBody().size());
120         assertEquals(DISTRIBUTION_STATUS, response.getBody().iterator().next().getDistributionStatus());
121
122     }
123
124     @Test
125     public void test_getResourceCsar_invalidCsar_internalServerError() {
126         final String url = getBaseUrl() + "/resources/" + UUID.randomUUID().toString() + "/toscaModel";
127
128         final ResponseEntity<byte[]> response =
129                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
130
131
132         assertFalse(response.hasBody());
133         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
134     }
135
136     @Test
137     public void test_getResourceMetadata_validMetadataFileInClasspath_matchContent() {
138
139         final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/metadata";
140
141         final ResponseEntity<ResourceMetadata> response =
142                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ResourceMetadata.class);
143
144
145         assertEquals(HttpStatus.OK, response.getStatusCode());
146         assertTrue(response.hasBody());
147         final ResourceMetadata actual = response.getBody();
148         assertEquals(8, actual.getResources().size());
149         assertEquals(3, actual.getArtifacts().size());
150         assertEquals(SUB_CATEGORY, actual.getSubCategory());
151
152     }
153
154     @Test
155     public void test_getServiceMetadata_validMetadataFileInClasspath_matchContent() {
156
157         final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/metadata";
158
159         final ResponseEntity<ServiceMetadata> response =
160                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceMetadata.class);
161
162
163         assertEquals(HttpStatus.OK, response.getStatusCode());
164         assertTrue(response.hasBody());
165         final ServiceMetadata actual = response.getBody();
166         assertEquals(1, actual.getResources().size());
167         assertEquals(1, actual.getArtifacts().size());
168         assertEquals(DISTRIBUTION_STATUS, actual.getDistributionStatus());
169
170     }
171
172
173     private String getBaseUrl() {
174         return "http://localhost:" + port + Constants.CATALOG_URL;
175     }
176
177     private HttpHeaders getHttpHeaders() {
178         final HttpHeaders requestHeaders = new HttpHeaders();
179         requestHeaders.add("Authorization", getBasicAuth(userCredentials.getUsers().get(0).getUsername()));
180         requestHeaders.setContentType(MediaType.APPLICATION_JSON);
181         return requestHeaders;
182     }
183
184     private String getBasicAuth(final String username) {
185         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
186     }
187
188 }