2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.onap.vid.scheduler;
24 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
25 import static org.mockito.Mockito.mock;
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.xebialabs.restito.semantics.Action;
29 import java.util.HashMap;
31 import org.glassfish.grizzly.http.util.HttpStatus;
32 import org.json.simple.parser.JSONParser;
33 import org.json.simple.parser.ParseException;
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.vid.mso.RestObject;
40 import org.onap.vid.testUtils.StubServerUtil;
41 import org.onap.vid.utils.Logging;
42 import org.testng.annotations.AfterMethod;
45 @RunWith(MockitoJUnitRunner.class)
46 public class SchedulerRestInterfaceTest {
48 private static final String SAMPLE_USERNAME = "sample";
49 private static final String SAMPLE_PASSWORD = "paS$w0Rd";
50 private static final String SAMPLE_SCHEDULER_SERVER_URL = "http://localhost";
51 private static final String SAMPLE_SOURCE_ID = "AAI";
52 private static final JSONParser JSON_PARSER = new JSONParser();
53 private static final String RESPONSE_CONTENT = "\"schedules\": \"SAMPLE STRING\"";
54 private static final String ERROR_RESPONSE = "\"error\": \"Internal server error!\"";
55 private static Map<String, String> DUMMY_SYSTEM_PROPERTIES = new HashMap<String, String>() {{
56 put(SchedulerProperties.SCHEDULER_USER_NAME_VAL, SAMPLE_USERNAME);
57 put(SchedulerProperties.SCHEDULER_PASSWORD_VAL, SAMPLE_PASSWORD);
58 put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, SAMPLE_SCHEDULER_SERVER_URL);
60 private static StubServerUtil serverUtil;
61 private static SchedulerRestInterface schedulerInterface = new SchedulerRestInterface((key) -> DUMMY_SYSTEM_PROPERTIES.get(key), mock(Logging.class));
64 public static void setUpClass() {
65 serverUtil = new StubServerUtil();
66 serverUtil.runServer();
70 public static void tearDownClass() {
71 serverUtil.stopServer();
76 public void tearDown() {
77 serverUtil.stopServer();
81 public void testShouldGetOKWhenStringIsExpected() throws JsonProcessingException, ParseException {
83 RestObject<String> sampleRestObj = new RestObject<>();
84 serverUtil.prepareGetCall("/test", RESPONSE_CONTENT, Action.ok());
86 schedulerInterface.Get("", "", sampleRestObj);
88 assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
91 @Test(expected = org.onap.vid.aai.ExceptionWithRequestInfo.class)
92 public void shouldRaiseExceptionWhenErrorOccursDuringGet() throws JsonProcessingException {
94 RestObject<String> sampleRestObj = new RestObject<>();
96 serverUtil.prepareGetCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
98 schedulerInterface.Get("", "", sampleRestObj);
102 public void shouldDeleteResourceSuccessfully() throws JsonProcessingException, ParseException {
104 RestObject<String> sampleRestObj = new RestObject<>();
105 serverUtil.prepareDeleteCall("/test", RESPONSE_CONTENT, Action.ok());
107 schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
109 assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
113 public void shouldRaiseExceptionWhenErrorOccursDuringDelete() throws JsonProcessingException, ParseException {
115 RestObject<String> sampleRestObj = new RestObject<>();
116 serverUtil.prepareDeleteCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
118 schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
120 assertResponseHasExpectedBodyAndStatus(sampleRestObj, ERROR_RESPONSE, 500);
124 private void assertResponseHasExpectedBodyAndStatus(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) throws ParseException {
125 Object parsedResult = JSON_PARSER.parse(sampleRestObj.get());
127 assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
128 assertThat(parsedResult).isInstanceOf(String.class).isEqualTo(expectedResponse);
132 private void prepareEnvForTest() {
133 String targetUrl = serverUtil.constructTargetUrl("http", "test");
134 DUMMY_SYSTEM_PROPERTIES.put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, targetUrl);