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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.onap.so.sdcsimulator.controller;
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;
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;
53 * @author Waqas Ikram (waqas.ikram@est.tech)
56 @RunWith(SpringJUnit4ClassRunner.class)
57 @ActiveProfiles("test")
58 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
60 public class CatalogControllerTest {
62 private static final String SUB_CATEGORY = "Network Service";
64 private static final String DISTRIBUTION_STATUS = "DISTRIBUTED";
66 private static final String SERVICE_ID = "9bb8c882-44a1-4b67-a12c-5a998e18d6ba";
68 private static final String RESOURCE_ID = "73522444-e8e9-49c1-be29-d355800aa349";
70 private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
76 private TestRestTemplate restTemplate;
79 private UserCredentials userCredentials;
82 public void test_getResourceCsar_validCsarId_matchContent() {
84 final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/toscaModel";
86 final ResponseEntity<byte[]> response =
87 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
89 assertEquals(HttpStatus.OK, response.getStatusCode());
90 assertTrue(response.hasBody());
91 assertEquals(117247, response.getBody().length);
96 public void test_getServiceCsar_validCsarId_matchContent() {
98 final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/toscaModel";
100 final ResponseEntity<byte[]> response =
101 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
103 assertEquals(HttpStatus.OK, response.getStatusCode());
104 assertTrue(response.hasBody());
105 assertEquals(147255, response.getBody().length);
110 public void test_getResources_validResourcesFromClassPath() {
112 final ResponseEntity<Set<ResourceAssetInfo>> response =
113 restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
114 new ParameterizedTypeReference<Set<ResourceAssetInfo>>() {});
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())));
124 public void test_getServices_validServicesFromClassPath() {
126 final ResponseEntity<Set<ServiceAssetInfo>> response =
127 restTemplate.exchange(getBaseUrl() + "/services", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
128 new ParameterizedTypeReference<Set<ServiceAssetInfo>>() {});
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())));
139 public void test_getResourceCsar_invalidCsar_internalServerError() {
140 final String url = getBaseUrl() + "/resources/" + UUID.randomUUID().toString() + "/toscaModel";
142 final ResponseEntity<byte[]> response =
143 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
146 assertFalse(response.hasBody());
147 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
151 public void test_getResourceMetadata_validMetadataFileInClasspath_matchContent() {
153 final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/metadata";
155 final ResponseEntity<ResourceMetadata> response =
156 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ResourceMetadata.class);
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());
169 public void test_getServiceMetadata_validMetadataFileInClasspath_matchContent() {
171 final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/metadata";
173 final ResponseEntity<ServiceMetadata> response =
174 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceMetadata.class);
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());
187 private String getBaseUrl() {
188 return "http://localhost:" + port + Constants.CATALOG_URL;
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;
198 private String getBasicAuth(final String username) {
199 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));