98ec448e19ce4615c6905d15bd00abb11f4f8298
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / test / java / org / onap / so / sdc / simulator / 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.sdc.simulator.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.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;
47
48 /**
49  * @author Waqas Ikram (waqas.ikram@est.tech)
50  *
51  */
52 @RunWith(SpringJUnit4ClassRunner.class)
53 @ActiveProfiles("test")
54 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
55 @Configuration
56 public class CatalogControllerTest {
57
58     private static final String PASSWORD = "Kp8bJ4SXszM0WXlhak3eHlcse2gAw84vaoGGmJvUy2U";
59
60     @LocalServerPort
61     private int port;
62
63     @Autowired
64     private TestRestTemplate restTemplate;
65
66     @Value("${spring.security.username}")
67     private String username;
68
69     @Test
70     public void test_getCsar_validCsarId_matchContent() {
71
72         final String url = getBaseUrl() + "/resources/" + Constants.DEFAULT_CSAR_NAME + "/toscaModel";
73
74         final ResponseEntity<byte[]> response =
75                 restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<>(getHttpHeaders()), byte[].class);
76
77         assertEquals(HttpStatus.OK, response.getStatusCode());
78         assertTrue(response.hasBody());
79         assertEquals(3982, response.getBody().length);
80
81     }
82
83     @Test
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);
88
89         final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constants.DEFAULT_CSAR_NAME);
90
91         assertFalse(response.hasBody());
92         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
93     }
94
95     private String getBaseUrl() {
96         return "http://localhost:" + port + Constants.CATALOG_URL;
97     }
98
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;
104     }
105
106     private String getBasicAuth(final String username) {
107         return "Basic " + new String(Base64.getEncoder().encodeToString((username + ":" + PASSWORD).getBytes()));
108     }
109
110 }