7834425da6b2b57c8adabd2342b345fab2cbe2a4
[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 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.Constant;
32 import org.springframework.beans.factory.annotation.Autowired;
33 import org.springframework.boot.test.context.SpringBootTest;
34 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
35 import org.springframework.boot.test.web.client.TestRestTemplate;
36 import org.springframework.boot.web.server.LocalServerPort;
37 import org.springframework.context.annotation.Configuration;
38 import org.springframework.http.HttpStatus;
39 import org.springframework.http.ResponseEntity;
40 import org.springframework.test.context.ActiveProfiles;
41 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
42
43 /**
44  * @author Waqas Ikram (waqas.ikram@est.tech)
45  */
46 @RunWith(SpringJUnit4ClassRunner.class)
47 @ActiveProfiles("test")
48 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
49 @Configuration
50 public class SdcSimulatorControllerTest {
51
52     @LocalServerPort
53     private int port;
54
55     @Autowired
56     private TestRestTemplate restTemplate;
57
58     @Test
59     public void test_healthCheck_matchContent() {
60         final String url = getBaseUrl() + "/healthcheck";
61         final ResponseEntity<String> object = restTemplate.getForEntity(url, String.class);
62
63         assertEquals(Constant.HEALTHY, object.getBody());
64
65     }
66
67     @Test
68     public void test_getCsar_validCsarId_matchContent() {
69
70         final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel";
71
72         final ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
73
74         assertTrue(response.hasBody());
75         assertEquals(3982, response.getBody().length);
76
77         assertEquals(HttpStatus.OK, response.getStatusCode());
78     }
79
80     @Test
81     public void test_getCsar_invalidCsar_internalServerError() {
82         final ResourceProvider mockedResourceProvider = Mockito.mock(ResourceProvider.class);
83         Mockito.when(mockedResourceProvider.getResource(Mockito.anyString())).thenReturn(Optional.empty());
84         final SdcSimulatorController objUnderTest = new SdcSimulatorController(mockedResourceProvider);
85
86         final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constant.DEFAULT_CSAR_NAME);
87
88         assertFalse(response.hasBody());
89         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
90     }
91
92     private String getBaseUrl() {
93         return "http://localhost:" + port + Constant.BASE_URL;
94     }
95
96 }