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