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