Clean up of Pair classes - models
[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-2020 AT&T Intellectual Property. All rights reserved
8  * Modifications Copyright (C) 2019-2020 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.any;
28 import static org.mockito.ArgumentMatchers.anyMap;
29 import static org.mockito.ArgumentMatchers.anyString;
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.verify;
34 import static org.mockito.Mockito.when;
35
36 import java.util.UUID;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.onap.policy.rest.RestManager;
41 import org.onap.policy.sdnc.SdncManager.SdncCallback;
42 import org.onap.policy.sdnc.util.Serialization;
43
44 public class SdncManagerTest implements SdncCallback {
45     private static final String SOMEWHERE_OVER_THE_RAINBOW = "http://somewhere.over.the.rainbow";
46
47     private static final String DOROTHY = "Dorothy";
48
49     private RestManager   mockedRestManager;
50
51     private Pair<Integer, String> httpResponsePutOk;
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       = Pair.of(202, Serialization.gsonPretty.toJson(response));
66         httpResponseBadResponse = Pair.of(202, Serialization.gsonPretty.toJson(null));
67         httpResponseErr         = Pair.of(200, null);
68     }
69
70     /**
71      * Create the request and response before.
72      */
73     @Before
74     public void createRequestAndResponse() {
75         SdncHealServiceInfo serviceInfo = new SdncHealServiceInfo();
76         serviceInfo.setServiceInstanceId("E-City");
77
78         SdncHealRequestHeaderInfo additionalParams = new SdncHealRequestHeaderInfo();
79         additionalParams.setSvcAction("Go Home");
80         additionalParams.setSvcRequestId("My Request");
81
82         SdncHealRequest healRequest = new SdncHealRequest();
83         healRequest.setRequestHeaderInfo(additionalParams);
84         healRequest.setServiceInfo(serviceInfo);
85
86         UUID requestId = UUID.randomUUID();
87         request = new SdncRequest();
88         request.setRequestId(requestId);
89         request.setHealRequest(healRequest);
90         request.setNsInstanceId(DOROTHY);
91
92         SdncResponseOutput responseDescriptor = new SdncResponseOutput();
93         responseDescriptor.setSvcRequestId("1234");
94         responseDescriptor.setResponseCode("200");
95         responseDescriptor.setAckFinalIndicator("final-indicator-00");
96
97         response = new SdncResponse();
98         response.setRequestId(request.getRequestId().toString());
99         response.setResponseOutput(responseDescriptor);
100     }
101
102     @Test
103     public void testSdncInitiation() {
104
105         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(null, null, null, null, null))
106             .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
107
108         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, null, null, null, null))
109             .withMessage("the parameters \"callback\" and \"request\" on the SdncManager constructor may not be null");
110
111         assertThatIllegalArgumentException().isThrownBy(() -> new SdncManager(this, request, null, null, null))
112             .withMessage("the \"url\" parameter on the SdncManager constructor may not be null");
113
114         new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Toto");
115     }
116
117     @Test
118     public void testSdncExecutionException() throws InterruptedException {
119         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Exception");
120         manager.setRestManager(mockedRestManager);
121
122         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Exception"), anyMap(),
123                         anyString(), anyString())).thenThrow(new RuntimeException("OzException"));
124
125         Thread managerThread = new Thread(manager);
126         managerThread.start();
127
128         managerThread.join(1000);
129
130         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
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         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Null"), anyMap(),
139                         anyString(), anyString())).thenReturn(null);
140
141         manager.run();
142
143         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
144     }
145
146
147     @Test
148     public void testSdncExecutionError0() throws InterruptedException {
149         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "Error0");
150         manager.setRestManager(mockedRestManager);
151
152         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("Error0"), anyMap(),
153                         anyString(), anyString())).thenReturn(httpResponseErr);
154
155         manager.run();
156
157         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
158     }
159
160     @Test
161     public void testSdncExecutionBadResponse() throws InterruptedException {
162         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "BadResponse");
163         manager.setRestManager(mockedRestManager);
164
165         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
166                         anyString(), anyString())).thenReturn(httpResponseBadResponse);
167
168         manager.run();
169
170         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
171     }
172
173     @Test
174     public void testSdncExecutionOk() throws InterruptedException {
175         SdncManager manager = new SdncManager(this, request, SOMEWHERE_OVER_THE_RAINBOW, DOROTHY, "OOK");
176         manager.setRestManager(mockedRestManager);
177
178         when(mockedRestManager.post(startsWith(SOMEWHERE_OVER_THE_RAINBOW), eq(DOROTHY), eq("OK"), anyMap(),
179                         anyString(), anyString())).thenReturn(httpResponsePutOk);
180
181         manager.run();
182
183         verify(mockedRestManager).post(any(), any(), any(), any(), any(), any());
184     }
185
186     @Override
187     public void onCallback(SdncResponse response) {
188         //
189         // Nothing really to do
190         //
191     }
192 }