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.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;
54 * @author Waqas Ikram (waqas.ikram@est.tech)
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @ActiveProfiles("test")
59 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
61 public class CatalogControllerTest {
63 private static final String SUB_CATEGORY = "Network Service";
65 private static final String DISTRIBUTION_STATUS = "DISTRIBUTED";
67 private static final String SERVICE_ID = "9bb8c882-44a1-4b67-a12c-5a998e18d6ba";
69 private static final String RESOURCE_ID = "73522444-e8e9-49c1-be29-d355800aa349";
71 private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
77 private TestRestTemplate restTemplate;
80 private UserCredentials userCredentials;
83 public void test_getCsar_validCsarId_matchContent() {
85 final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/toscaModel";
87 final ResponseEntity<byte[]> response =
88 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
90 assertEquals(HttpStatus.OK, response.getStatusCode());
91 assertTrue(response.hasBody());
92 assertEquals(117247, response.getBody().length);
97 public void test_getResources_validResourcesFromClassPath() {
99 final ResponseEntity<Set<ResourceAssetInfo>> response =
100 restTemplate.exchange(getBaseUrl() + "/resources", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
101 new ParameterizedTypeReference<Set<ResourceAssetInfo>>() {});
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());
111 public void test_getServices_validServicesFromClassPath() {
113 final ResponseEntity<Set<ServiceAssetInfo>> response =
114 restTemplate.exchange(getBaseUrl() + "/services", HttpMethod.GET, new HttpEntity<>(getHttpHeaders()),
115 new ParameterizedTypeReference<Set<ServiceAssetInfo>>() {});
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());
125 public void test_getResourceCsar_invalidCsar_internalServerError() {
126 final String url = getBaseUrl() + "/resources/" + UUID.randomUUID().toString() + "/toscaModel";
128 final ResponseEntity<byte[]> response =
129 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
132 assertFalse(response.hasBody());
133 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
137 public void test_getResourceMetadata_validMetadataFileInClasspath_matchContent() {
139 final String url = getBaseUrl() + "/resources/" + RESOURCE_ID + "/metadata";
141 final ResponseEntity<ResourceMetadata> response =
142 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ResourceMetadata.class);
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());
155 public void test_getServiceMetadata_validMetadataFileInClasspath_matchContent() {
157 final String url = getBaseUrl() + "/services/" + SERVICE_ID + "/metadata";
159 final ResponseEntity<ServiceMetadata> response =
160 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), ServiceMetadata.class);
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());
173 private String getBaseUrl() {
174 return "http://localhost:" + port + Constants.CATALOG_URL;
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;
184 private String getBasicAuth(final String username) {
185 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));