2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.o_ran_sc.nonrtric.sdnc_a1.northbound.provider;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.when;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.internal.util.reflection.Whitebox;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapter;
34 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
35 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
36 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
37 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
38 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyInputBuilder;
39 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput;
40 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder;
41 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutput;
42 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusInputBuilder;
43 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput;
44 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeInputBuilder;
45 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput;
46 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyInputBuilder;
47 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutput;
48 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51 import org.springframework.http.HttpStatus;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.web.client.RestClientResponseException;
56 * This class Tests all the methods in NonrtRicApiProvider
58 * @author lathishbabu.ganesan@est.tech
62 @RunWith(MockitoJUnitRunner.class)
63 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
65 protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
66 protected NonrtRicApiProvider nonrtRicApiProvider;
67 protected DataBroker dataBroker;
69 protected NotificationPublishService mockNotificationPublishService;
71 protected RpcProviderRegistry mockRpcProviderRegistry;
73 private RestAdapter restAdapter;
74 private static Uri nearRtRicUrl = new Uri("http://ric1:8085");
77 public void setUp() throws Exception {
78 dataBroker = getDataBroker();
79 nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry);
83 public void testGetA1PolicySuccess() throws InterruptedException, ExecutionException {
84 GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder();
85 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
86 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
87 String returnedBody = "returned body";
88 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
89 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
90 GetA1PolicyOutput result = nonrtRicApiProvider.getA1Policy(inputBuilder.build()).get().getResult();
91 Assert.assertEquals(returnedBody, result.getBody());
92 Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
96 public void testGetA1PolicyTypeSuccess() throws InterruptedException, ExecutionException {
97 GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder();
98 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
99 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
100 String returnedBody = "returned body";
101 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
102 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
103 GetA1PolicyTypeOutput result = nonrtRicApiProvider.getA1PolicyType(inputBuilder.build()).get().getResult();
104 Assert.assertEquals(returnedBody, result.getBody());
105 Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
109 public void testGetA1PolicyStatusSuccess() throws InterruptedException, ExecutionException {
110 GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder();
111 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
112 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
113 String returnedBody = "returned body";
114 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
115 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
116 GetA1PolicyStatusOutput result = nonrtRicApiProvider.getA1PolicyStatus(inputBuilder.build()).get().getResult();
117 Assert.assertEquals(returnedBody, result.getBody());
118 Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
122 public void testGetA1Failure() throws InterruptedException, ExecutionException {
123 GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder();
124 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
125 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
126 String returnedBody = "GET failed";
127 int returnedStatusCode = 404;
128 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class)))
129 .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null));
130 GetA1PolicyOutput result = nonrtRicApiProvider.getA1(inputBuilder.build());
131 Assert.assertEquals(returnedBody, result.getBody());
132 Assert.assertTrue(returnedStatusCode == result.getHttpStatus());
136 public void testPutA1PolicySuccess() throws InterruptedException, ExecutionException {
137 PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder();
138 String testPolicy = "{}";
139 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
140 inputBuilder.setBody(testPolicy);
141 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
142 String returnedBody = "returned body";
143 ResponseEntity<String> putResponse = new ResponseEntity<>(returnedBody, HttpStatus.CREATED);
144 when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class))).thenReturn(putResponse);
145 PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult();
146 Assert.assertEquals(returnedBody, result.getBody());
147 Assert.assertTrue(HttpStatus.CREATED.value() == result.getHttpStatus());
151 public void testPutA1PolicyFailure() throws InterruptedException, ExecutionException {
152 PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder();
153 String testPolicy = "{}";
154 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
155 inputBuilder.setBody(testPolicy);
156 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
157 String returnedBody = "PUT failed";
158 int returnedStatusCode = 400;
159 when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class)))
160 .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null));
161 PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult();
162 Assert.assertEquals(returnedBody, result.getBody());
163 Assert.assertTrue(returnedStatusCode == result.getHttpStatus());
167 public void testDeleteA1Success() throws InterruptedException, ExecutionException {
168 DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder();
169 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
170 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
171 ResponseEntity<Object> getResponseNoContent = new ResponseEntity<>(HttpStatus.NO_CONTENT);
172 String returnedBody = "returned body";
173 ResponseEntity<Object> getResponseOk = new ResponseEntity<>(returnedBody, HttpStatus.OK);
174 when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponseNoContent).thenReturn(getResponseOk);
175 DeleteA1PolicyOutput resultNoContent = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
176 Assert.assertTrue(HttpStatus.NO_CONTENT.value() == resultNoContent.getHttpStatus());
177 DeleteA1PolicyOutput resultOk = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
178 Assert.assertEquals(returnedBody, resultOk.getBody());
179 Assert.assertTrue(HttpStatus.OK.value() == resultOk.getHttpStatus());
183 public void testDeleteA1Failure() throws InterruptedException, ExecutionException {
184 DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder();
185 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
186 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
187 String returnedBody = "DELETE failed";
188 int returnedStatusCode = 404;
189 when(restAdapter.delete(nearRtRicUrl.getValue()))
190 .thenThrow(new RestClientResponseException(null, returnedStatusCode, null, null, returnedBody.getBytes(), null));
191 DeleteA1PolicyOutput result = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
192 Assert.assertEquals(returnedBody, result.getBody());
193 Assert.assertTrue(returnedStatusCode == result.getHttpStatus());