2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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 com.fasterxml.jackson.core.JsonProcessingException;
25 import com.xebialabs.restito.semantics.Action;
26 import org.glassfish.grizzly.http.util.HttpStatus;
27 import org.json.simple.parser.JSONParser;
28 import org.json.simple.parser.ParseException;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.junit.MockitoJUnitRunner;
34 import org.onap.vid.exceptions.GenericUncheckedException;
35 import org.onap.vid.testUtils.StubServerUtil;
36 import org.testng.annotations.AfterMethod;
38 import java.util.HashMap;
41 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
44 @RunWith(MockitoJUnitRunner.class)
45 public class SchedulerRestInterfaceTest {
47 private static final String SAMPLE_USERNAME = "sample";
48 private static final String SAMPLE_PASSWORD = "paS$w0Rd";
49 private static final String SAMPLE_SCHEDULER_SERVER_URL = "http://localhost";
50 private static final String SAMPLE_SOURCE_ID = "AAI";
51 private static final JSONParser JSON_PARSER = new JSONParser();
52 private static final String RESPONSE_CONTENT = "\"schedules\": \"SAMPLE STRING\"";
53 private static final String ERROR_RESPONSE = "\"error\": \"Internal server error!\"";
54 private static Map<String, String> DUMMY_SYSTEM_PROPERTIES = new HashMap<String, String>() {{
55 put(SchedulerProperties.SCHEDULER_USER_NAME_VAL, SAMPLE_USERNAME);
56 put(SchedulerProperties.SCHEDULER_PASSWORD_VAL, SAMPLE_PASSWORD);
57 put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, SAMPLE_SCHEDULER_SERVER_URL);
59 private static StubServerUtil serverUtil;
60 private static SchedulerRestInterface schedulerInterface = new SchedulerRestInterface((key) -> DUMMY_SYSTEM_PROPERTIES.get(key));
63 public static void setUpClass() {
64 serverUtil = new StubServerUtil();
65 serverUtil.runServer();
69 public static void tearDownClass() {
70 serverUtil.stopServer();
75 public void tearDown() {
76 serverUtil.stopServer();
80 public void testShouldGetOKWhenStringIsExpected() throws JsonProcessingException, ParseException {
82 RestObject<String> sampleRestObj = new RestObject<>();
83 serverUtil.prepareGetCall("/test", RESPONSE_CONTENT, Action.ok());
85 schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
87 assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
90 @Test(expected = GenericUncheckedException.class)
91 public void shouldRaiseExceptionWhenErrorOccursDuringGet() throws JsonProcessingException {
93 RestObject<String> sampleRestObj = new RestObject<>();
95 serverUtil.prepareGetCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
97 schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
101 public void shouldDeleteResourceSuccessfully() throws JsonProcessingException, ParseException {
103 RestObject<String> sampleRestObj = new RestObject<>();
104 serverUtil.prepareDeleteCall("/test", RESPONSE_CONTENT, Action.ok());
106 schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
108 assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
112 public void shouldRaiseExceptionWhenErrorOccursDuringDelete() throws JsonProcessingException, ParseException {
114 RestObject<String> sampleRestObj = new RestObject<>();
115 serverUtil.prepareDeleteCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
117 schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
119 assertResponseHasExpectedBodyAndStatus(sampleRestObj, ERROR_RESPONSE, 500);
123 private void assertResponseHasExpectedBodyAndStatus(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) throws ParseException {
124 Object parsedResult = JSON_PARSER.parse(sampleRestObj.get());
126 assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
127 assertThat(parsedResult).isInstanceOf(String.class).isEqualTo(expectedResponse);
128 assertThat(sampleRestObj.getUUID()).isNull();
132 private void prepareEnvForTest() {
133 String targetUrl = serverUtil.constructTargetUrl("http", "test");
134 DUMMY_SYSTEM_PROPERTIES.put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, targetUrl);