Simulate Request for SDC to get CSAR
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / sdc-simulator / src / test / java / org / onap / so / sdc / simulator / SdcSimulatorControllerTest.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
21 package org.onap.so.sdc.simulator;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Optional;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.TemporaryFolder;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mockito;
33 import org.onap.so.sdc.simulator.providers.ResourceProvider;
34 import org.springframework.beans.factory.annotation.Autowired;
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.HttpStatus;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.test.context.ActiveProfiles;
43 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
44
45 /**
46  * @author Waqas Ikram (waqas.ikram@est.tech)
47  */
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ActiveProfiles("test")
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
51 @Configuration
52 public class SdcSimulatorControllerTest {
53
54     private static final String MOCKER_SDC_CONTROLLER_BEAN = "mockResourceProvider";
55
56     @LocalServerPort
57     private int port;
58
59     @Autowired
60     private TestRestTemplate restTemplate;
61
62     @Test
63     public void test_healthCheck_matchContent() {
64         final String url = getBaseUrl() + "/healthcheck";
65         final ResponseEntity<String> object = restTemplate.getForEntity(url, String.class);
66
67         assertEquals(Constant.HEALTHY, object.getBody());
68
69     }
70
71     @Test
72     public void test_getCsar_validCsarId_matchContent() {
73
74         final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel";
75
76         final ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
77
78         assertTrue(response.hasBody());
79         assertEquals(3982, response.getBody().length);
80
81         assertEquals(HttpStatus.OK, response.getStatusCode());
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 SdcSimulatorController objUnderTest = new SdcSimulatorController(mockedResourceProvider);
89
90         final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constant.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 + Constant.BASE_URL;
98     }
99
100 }