Fix sonar issues in models: sdc to vfc
[policy/models.git] / models-interactions / model-impl / sdnc / src / test / java / org / onap / policy / sdnc / SdncManagerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * sdnc
4  * ================================================================================
5  * Copyright (C) 2018 Huawei. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved
8  * Modifications Copyright (C) 2019 Nordix Foundation.
9  * ================================================================================
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.policy.sdnc;
25
26 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
27 import static org.mockito.ArgumentMatchers.anyMap;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.endsWith;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.startsWith;
32 import static org.mockito.Mockito.mock;
33 import static org.mockito.Mockito.when;
34
35 import java.util.UUID;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.rest.RestManager;
39 import org.onap.policy.rest.RestManager.Pair;
40 import org.onap.policy.sdnc.SdncManager.SdncCallback;
41 import org.onap.policy.sdnc.util.Serialization;
42
43 public class SdncManagerTest implements SdncCallback {
44     private static final String SOMEWHERE_OVER_THE_RAINBOW = "http://somewhere.over.the.rainbow";
45
46     private static final String DOROTHY = "Dorothy";
47
48     private RestManager   mockedRestManager;
49
50     private Pair<Integer, String> httpResponsePutOk;
51     private Pair<Integer, String> httpResponseGetOk;
52     private Pair<Integer, String> httpResponseBadResponse;
53     private Pair<Integer, String> httpResponseErr;
54
55     private SdncRequest  request;
56     private SdncResponse response;
57
58     /**
59      * Set up the mocked REST manager.
60      */
61     @Before
62     public void setupMockedRest() {
63         mockedRestManager   = mock(RestManager.class);
64
65         httpResponsePutOk       = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(response));
66         httpResponseGetOk       = mockedRestManager.new Pair<>(200, Serialization.gsonPretty.toJson(response));
67         httpResponseBadResponse = mockedRestManager.new Pair<>(202, Serialization.gsonPretty.toJson(null));
68         httpResponseErr         = mockedRestManager.new Pair<>(200, null);
69     }
70
71     /**
72      * Create the request and response before.
73      */
74     @Before
75     public void createRequestAndResponse() {
76         SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
77         serviceInfo.setServiceInstanceId("E-City");
78
79         SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
80         additionalParams.setSvcAction("Go Home");
81         additionalParams.setSvcRequestId("My Request");
82
83         SdncHealRequest healRequest = new SdncHealRequest();
84         healRequest.setRequestHeaderInfo(additionalParams);
85         healRequest.setServiceInfo(serviceInfo);
86
87         UUID requestId = UUID.randomUUID();
88         request = new SdncRequest();
89         request.setRequestId(requestId);
90         request.setHealRequest(healRequest);
91         request.setNsInstanceId(DOROTHY);
92
93         SdncResponseOutput responseDescriptor = new SdncResponseOutput();
94         responseDescriptor.setSvcRequestId("1234");
95         responseDescriptor.setResponseCode("200");
96         responseDescriptor.setAckFinalIndicator("final-indicator-00");
97
98         response = new SdncResponse();
99         response.setRequestId(request.getRequestId().toString());
100         response.setResponseOutput(responseDescriptor);
101     }
102
103     @Test
104     public void testSdncInitiation() {
105
106         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(null, null, null, null, null))
107             .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
108
109         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, null, null, null, null))
110             .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
111
112         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, request, null, null, null))
113             .withMessage("the \"url\" parameter on the SdncManager constructor may not be null");
114
115         new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Toto");
116     }
117
118     @Test
119     public void testSdncExecutionException() throws InterruptedException {
120         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Exception");
121         manager.setRestManager(mockedRestManager);
122
123         Thread managerThread = new Thread(manager);
124         managerThread.start();
125
126         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Exception"), anyMap(),
127                         anyString(), anyString())).thenThrow(new RuntimeException("OzException"));
128
129
130         managerThread.join(100);
131     }
132
133     @Test
134     public void testSdncExecutionNull() throws InterruptedException {
135         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Null");
136         manager.setRestManager(mockedRestManager);
137
138         Thread managerThread = new Thread(manager);
139         managerThread.start();
140
141         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Null"), anyMap(),
142                         anyString(), anyString())).thenReturn(null);
143
144         managerThread.join(100);
145     }
146
147
148     @Test
149     public void testSdncExecutionError0() throws InterruptedException {
150         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Error0");
151         manager.setRestManager(mockedRestManager);
152
153         Thread managerThread = new Thread(manager);
154         managerThread.start();
155
156         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Error0"), anyMap(),
157                         anyString(), anyString())).thenReturn(httpResponseErr);
158
159         managerThread.join(100);
160     }
161
162     @Test
163     public void testSdncExecutionBadResponse() throws InterruptedException {
164         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "BadResponse");
165         manager.setRestManager(mockedRestManager);
166
167         Thread managerThread = new Thread(manager);
168         managerThread.start();
169
170         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
171                         anyString(), anyString())).thenReturn(httpResponseBadResponse);
172
173         managerThread.join(100);
174     }
175
176     @Test
177     public void testSdncExecutionOk() throws InterruptedException {
178         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "OOK");
179         manager.setRestManager(mockedRestManager);
180
181         Thread managerThread = new Thread(manager);
182         managerThread.start();
183
184         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
185                         anyString(), anyString())).thenReturn(httpResponsePutOk);
186
187         when(mockedRestManager.get(endsWith("1234"), eq(DOROTHY), eq("OK"), anyMap()))
188             .thenReturn(httpResponseGetOk);
189
190
191         managerThread.join(100);
192     }
193
194     @Override
195     public void onCallback(SdncResponse response) {
196         //
197         // Nothing really to do
198         //
199     }
200 }