make Logging a service and inject it to SyncRestClient
[vid.git] / vid-app-common / src / test / java / org / onap / vid / scheduler / SchedulerRestInterfaceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
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
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 package org.onap.vid.scheduler;
23
24 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
25 import static org.mockito.Mockito.mock;
26
27 import com.fasterxml.jackson.core.JsonProcessingException;
28 import com.xebialabs.restito.semantics.Action;
29 import java.util.HashMap;
30 import java.util.Map;
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;
43
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class SchedulerRestInterfaceTest {
47
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);
59     }};
60     private static StubServerUtil serverUtil;
61     private static SchedulerRestInterface schedulerInterface = new SchedulerRestInterface((key) -> DUMMY_SYSTEM_PROPERTIES.get(key), mock(Logging.class));
62
63     @BeforeClass
64     public static void setUpClass() {
65         serverUtil = new StubServerUtil();
66         serverUtil.runServer();
67     }
68
69     @AfterClass
70     public static void tearDownClass() {
71         serverUtil.stopServer();
72     }
73
74
75     @AfterMethod
76     public void tearDown() {
77         serverUtil.stopServer();
78     }
79
80     @Test
81     public void testShouldGetOKWhenStringIsExpected() throws JsonProcessingException, ParseException {
82         prepareEnvForTest();
83         RestObject<String> sampleRestObj = new RestObject<>();
84         serverUtil.prepareGetCall("/test", RESPONSE_CONTENT, Action.ok());
85
86         schedulerInterface.Get("", "", sampleRestObj);
87
88         assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
89     }
90
91     @Test(expected = org.onap.vid.aai.ExceptionWithRequestInfo.class)
92     public void shouldRaiseExceptionWhenErrorOccursDuringGet() throws JsonProcessingException {
93         prepareEnvForTest();
94         RestObject<String> sampleRestObj = new RestObject<>();
95
96         serverUtil.prepareGetCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
97
98         schedulerInterface.Get("", "", sampleRestObj);
99     }
100
101     @Test
102     public void shouldDeleteResourceSuccessfully() throws JsonProcessingException, ParseException {
103         prepareEnvForTest();
104         RestObject<String> sampleRestObj = new RestObject<>();
105         serverUtil.prepareDeleteCall("/test", RESPONSE_CONTENT, Action.ok());
106
107         schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
108
109         assertResponseHasExpectedBodyAndStatus(sampleRestObj, RESPONSE_CONTENT, 200);
110     }
111
112     @Test
113     public void shouldRaiseExceptionWhenErrorOccursDuringDelete() throws JsonProcessingException, ParseException {
114         prepareEnvForTest();
115         RestObject<String> sampleRestObj = new RestObject<>();
116         serverUtil.prepareDeleteCall("/test", ERROR_RESPONSE, Action.status(HttpStatus.INTERNAL_SERVER_ERROR_500));
117
118         schedulerInterface.Delete("", SAMPLE_SOURCE_ID, "", sampleRestObj);
119
120         assertResponseHasExpectedBodyAndStatus(sampleRestObj, ERROR_RESPONSE, 500);
121     }
122
123
124     private void assertResponseHasExpectedBodyAndStatus(RestObject<String> sampleRestObj, String expectedResponse, int expectedStatusCode) throws ParseException {
125         Object parsedResult = JSON_PARSER.parse(sampleRestObj.get());
126
127         assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode);
128         assertThat(parsedResult).isInstanceOf(String.class).isEqualTo(expectedResponse);
129
130     }
131
132     private void prepareEnvForTest() {
133         String targetUrl = serverUtil.constructTargetUrl("http", "test");
134         DUMMY_SYSTEM_PROPERTIES.put(SchedulerProperties.SCHEDULER_SERVER_URL_VAL, targetUrl);
135     }
136 }