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.sdc.simulator.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;
26 import java.util.Optional;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mockito;
30 import org.onap.so.sdc.simulator.providers.ResourceProvider;
31 import org.onap.so.sdc.simulator.utils.Constants;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.beans.factory.annotation.Value;
34 import org.springframework.boot.test.context.SpringBootTest;
35 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
36 import org.springframework.boot.test.web.client.TestRestTemplate;
37 import org.springframework.boot.web.server.LocalServerPort;
38 import org.springframework.context.annotation.Configuration;
39 import org.springframework.http.HttpEntity;
40 import org.springframework.http.HttpHeaders;
41 import org.springframework.http.HttpMethod;
42 import org.springframework.http.HttpStatus;
43 import org.springframework.http.MediaType;
44 import org.springframework.http.ResponseEntity;
45 import org.springframework.test.context.ActiveProfiles;
46 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
49 * @author Waqas Ikram (waqas.ikram@est.tech)
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @ActiveProfiles("test")
54 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
56 public class CatalogControllerTest {
58 private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
64 private TestRestTemplate restTemplate;
66 @Value("${spring.security.username}")
67 private String username;
70 public void test_getCsar_validCsarId_matchContent() {
72 final String url = getBaseUrl() + "/resources/" + Constants.DEFAULT_CSAR_NAME + "/toscaModel";
74 final ResponseEntity<byte[]> response =
75 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
77 assertEquals(HttpStatus.OK, response.getStatusCode());
78 assertTrue(response.hasBody());
79 assertEquals(3982, response.getBody().length);
84 public void test_getCsar_invalidCsar_internalServerError() {
85 final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class);
86 Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty());
87 final CatalogController objUnderTest = new CatalogController(mockedResourceProvider);
89 final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constants.DEFAULT_CSAR_NAME);
91 assertFalse(response.hasBody());
92 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
95 private String getBaseUrl() {
96 return "http://localhost:" + port + Constants.CATALOG_URL;
99 private HttpHeaders getHttpHeaders() {
100 final HttpHeaders requestHeaders = new HttpHeaders();
101 requestHeaders.add("Authorization", getBasicAuth(username));
102 requestHeaders.setContentType(MediaType.APPLICATION_JSON);
103 return requestHeaders;
106 private String getBasicAuth(final String username) {
107 return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));