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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.sdc.simulator;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
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;
46 * @author Waqas Ikram (waqas.ikram@est.tech)
48 @RunWith(SpringJUnit4ClassRunner.class)
49 @ActiveProfiles("test")
50 @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
52 public class SdcSimulatorControllerTest {
54 private static final String MOCKER_SDC_CONTROLLER_BEAN = "mockResourceProvider";
60 private TestRestTemplate restTemplate;
63 public void test_healthCheck_matchContent() {
64 final String url = getBaseUrl() + "/healthcheck";
65 final ResponseEntity<String> object = restTemplate.getForEntity(url, String.class);
67 assertEquals(Constant.HEALTHY, object.getBody());
72 public void test_getCsar_validCsarId_matchContent() {
74 final String url = getBaseUrl() + "/resources/" + Constant.DEFAULT_CSAR_NAME + "/toscaModel";
76 final ResponseEntity<byte[]> response = restTemplate.getForEntity(url, byte[].class);
78 assertTrue(response.hasBody());
79 assertEquals(3982, response.getBody().length);
81 assertEquals(HttpStatus.OK, response.getStatusCode());
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);
90 final ResponseEntity<byte[]> response = objUnderTest.getCsar(Constant.DEFAULT_CSAR_NAME);
92 assertFalse(response.hasBody());
93 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
96 private String getBaseUrl() {
97 return "http://localhost:" + port + Constant.BASE_URL;