Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 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=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 | package org.onap.osam.scheduler;
|
| 25 |
|
| 26 | import com.fasterxml.jackson.core.JsonProcessingException;
|
| 27 | import com.xebialabs.restito.semantics.Action;
|
| 28 | import org.glassfish.grizzly.http.util.HttpStatus;
|
| 29 | import org.json.simple.parser.JSONParser;
|
| 30 | import org.json.simple.parser.ParseException;
|
| 31 | import org.junit.AfterClass;
|
| 32 | import org.junit.BeforeClass;
|
| 33 | import org.junit.Test;
|
| 34 | import org.junit.runner.RunWith;
|
| 35 | import org.mockito.runners.MockitoJUnitRunner;
|
| 36 | import org.onap.osam.exceptions.GenericUncheckedException;
|
| 37 | import org.onap.osam.testUtils.StubServerUtil;
|
| 38 | import org.testng.annotations.AfterMethod;
|
| 39 |
|
| 40 | import java.util.HashMap;
|
| 41 | import java.util.Map;
|
| 42 |
|
| 43 | import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
| 44 |
|
| 45 |
|
| 46 | @RunWith(MockitoJUnitRunner.class)
|
| 47 | public class SchedulerRestInterfaceTest {
|
| 48 |
|
| 49 | private static final String SAMPLE_USERNAME = "sample";
|
| 50 | private static final String SAMPLE_PASSWORD = "paS$w0Rd";
|
| 51 | private static final String SAMPLE_SCHEDULER_SERVER_URL = "http://localhost";
|
| 52 | private static final String SAMPLE_SOURCE_ID = "AAI";
|
| 53 | private static final JSONParser JSON_PARSER = new JSONParser();
|
| 54 | private static final String RESPONSE_CONTENT = "\"schedules\": \"SAMPLE STRING\"";
|
| 55 | private static final String ERROR_RESPONSE = "\"error\": \"Internal server error!\"";
|
| 56 | private static Map<String, String> DUMMY_SYSTEM_PROPERTIES = new HashMap<String, String>() {{
|
| 57 | put(SchedulerProperties.SCHEDULER_USER_NAME_VAL, SAMPLE_USERNAME);
|
| 58 | put(SchedulerProperties.SCHEDULER_PASSWORD_VAL, SAMPLE_PASSWORD);
|
| 59 | put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, SAMPLE_SCHEDULER_SERVER_URL);
|
| 60 | }};
|
| 61 | private static StubServerUtil serverUtil;
|
| 62 | private static SchedulerRestInterface schedulerInterface = new SchedulerRestInterface((key) -> DUMMY_SYSTEM_PROPERTIES.get(key));
|
| 63 |
|
| 64 | @BeforeClass
|
| 65 | public static void setUpClass() {
|
| 66 | serverUtil = new StubServerUtil();
|
| 67 | serverUtil.runServer();
|
| 68 | }
|
| 69 |
|
| 70 | @AfterClass
|
| 71 | public static void tearDownClass() {
|
| 72 | serverUtil.stopServer();
|
| 73 | }
|
| 74 |
|
| 75 |
|
| 76 | @AfterMethod
|
| 77 | public void tearDown() {
|
| 78 | serverUtil.stopServer();
|
| 79 | }
|
| 80 |
|
| 81 | @Test
|
| 82 | public void testShouldGetOKWhenStringIsExpected() throws JsonProcessingException, ParseException {
|
| 83 | prepareEnvForTest();
|
| 84 | RestObject<String> sampleRestObj = new RestObject<>();
|
| 85 | serverUtil.prepareGetCall("/test", RESPONSE_CONTENT, Action.ok());
|
| 86 |
|
| 87 | schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
|
| 88 |
|
| 89 | assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
|
| 90 | }
|
| 91 |
|
| 92 | @Test(expected = GenericUncheckedException.class)
|
| 93 | public void shouldRaiseExceptionWhenErrorOccursDuringGet() throws JsonProcessingException {
|
| 94 | prepareEnvForTest();
|
| 95 | RestObject<String> sampleRestObj = new RestObject<>();
|
| 96 |
|
| 97 | serverUtil.prepareGetCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
|
| 98 |
|
| 99 | schedulerInterface.Get("", SAMPLE_SOURCE_ID, "", sampleRestObj);
|
| 100 | }
|
| 101 |
|
| 102 | @Test
|
| 103 | public void shouldDeleteResourceSuccessfully() throws JsonProcessingException, ParseException {
|
| 104 | prepareEnvForTest();
|
| 105 | RestObject<String> sampleRestObj = new RestObject<>();
|
| 106 | serverUtil.prepareDeleteCall("/test", RESPONSE_CONTENT, Action.ok());
|
| 107 |
|
| 108 | schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
|
| 109 |
|
| 110 | assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
|
| 111 | }
|
| 112 |
|
| 113 | @Test
|
| 114 | public void shouldRaiseExceptionWhenErrorOccursDuringDelete() throws JsonProcessingException, ParseException {
|
| 115 | prepareEnvForTest();
|
| 116 | RestObject<String> sampleRestObj = new RestObject<>();
|
| 117 | serverUtil.prepareDeleteCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
|
| 118 |
|
| 119 | schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
|
| 120 |
|
| 121 | assertResponseHasExpectedBodyAndStatus(sampleRestObj, ERROR_RESPONSE, 500);
|
| 122 | }
|
| 123 |
|
| 124 |
|
| 125 | private void assertResponseHasExpectedBodyAndStatus(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) throws ParseException {
|
| 126 | Object parsedResult = JSON_PARSER.parse(sampleRestObj.get());
|
| 127 |
|
| 128 | assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
|
| 129 | assertThat(parsedResult).isInstanceOf(String.class).isEqualTo(expectedResponse);
|
| 130 | assertThat(sampleRestObj.getUUID()).isNull();
|
| 131 |
|
| 132 | }
|
| 133 |
|
| 134 | private void prepareEnvForTest() {
|
| 135 | String targetUrl = serverUtil.constructTargetUrl("http", "test");
|
| 136 | DUMMY_SYSTEM_PROPERTIES.put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, targetUrl);
|
| 137 | }
|
| 138 | }
|