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.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.when;
26 import java.util.concurrent.ExecutionException;
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);
67 private static final Integer HTTP_OK_AS_INTEGER = HttpStatus.OK.value();
69 protected NonrtRicApiProvider nonrtRicApiProvider;
70 protected DataBroker dataBroker;
72 protected NotificationPublishService mockNotificationPublishService;
74 protected RpcProviderRegistry mockRpcProviderRegistry;
76 private RestAdapter restAdapter;
77 private static Uri nearRtRicUrl = new Uri("http://ric1:8085");
80 public void setUp() throws Exception {
81 dataBroker = getDataBroker();
82 nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry);
86 public void testGetA1PolicySuccess() throws InterruptedException, ExecutionException {
87 GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder();
88 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
89 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
90 String returnedBody = "returned body";
91 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
92 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
93 GetA1PolicyOutput result = nonrtRicApiProvider.getA1Policy(inputBuilder.build()).get().getResult();
94 assertEquals(returnedBody, result.getBody());
95 assertEquals(HTTP_OK_AS_INTEGER, result.getHttpStatus());
99 public void testGetA1PolicyTypeSuccess() throws InterruptedException, ExecutionException {
100 GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder();
101 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
102 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
103 String returnedBody = "returned body";
104 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
105 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
106 GetA1PolicyTypeOutput result = nonrtRicApiProvider.getA1PolicyType(inputBuilder.build()).get().getResult();
107 assertEquals(returnedBody, result.getBody());
108 assertEquals(HTTP_OK_AS_INTEGER, result.getHttpStatus());
112 public void testGetA1PolicyStatusSuccess() throws InterruptedException, ExecutionException {
113 GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder();
114 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
115 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
116 String returnedBody = "returned body";
117 ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
118 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
119 GetA1PolicyStatusOutput result = nonrtRicApiProvider.getA1PolicyStatus(inputBuilder.build()).get().getResult();
120 assertEquals(returnedBody, result.getBody());
121 assertEquals(HTTP_OK_AS_INTEGER, result.getHttpStatus());
125 public void testGetA1Failure() throws InterruptedException, ExecutionException {
126 GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder();
127 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
128 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
129 String returnedBody = "GET failed";
130 Integer notFoundStatusCode = HttpStatus.NOT_FOUND.value();
131 when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class)))
132 .thenThrow(new RestClientResponseException(null, notFoundStatusCode, null, null, returnedBody.getBytes(), null));
133 GetA1PolicyOutput result = nonrtRicApiProvider.getA1(inputBuilder.build());
134 assertEquals(returnedBody, result.getBody());
135 assertEquals(notFoundStatusCode, result.getHttpStatus());
139 public void testPutA1PolicySuccess() throws InterruptedException, ExecutionException {
140 PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder();
141 String testPolicy = "{}";
142 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
143 inputBuilder.setBody(testPolicy);
144 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
145 String returnedBody = "returned body";
146 Integer createdStatusCode = HttpStatus.CREATED.value();
147 ResponseEntity<String> putResponse = new ResponseEntity<>(returnedBody, HttpStatus.CREATED);
148 when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class))).thenReturn(putResponse);
149 PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult();
150 assertEquals(returnedBody, result.getBody());
151 assertEquals(createdStatusCode, result.getHttpStatus());
155 public void testPutA1PolicyFailure() throws InterruptedException, ExecutionException {
156 PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder();
157 String testPolicy = "{}";
158 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
159 inputBuilder.setBody(testPolicy);
160 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
161 String returnedBody = "PUT failed";
162 Integer badRequestStatusCode = HttpStatus.BAD_REQUEST.value();
163 when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class)))
164 .thenThrow(new RestClientResponseException(null, badRequestStatusCode, null, null, returnedBody.getBytes(), null));
165 PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult();
166 assertEquals(returnedBody, result.getBody());
167 assertEquals(badRequestStatusCode, result.getHttpStatus());
171 public void testDeleteA1Success() throws InterruptedException, ExecutionException {
172 DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder();
173 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
174 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
175 ResponseEntity<Object> getResponseNoContent = new ResponseEntity<>(HttpStatus.NO_CONTENT);
176 String returnedBody = "returned body";
177 ResponseEntity<Object> getResponseOk = new ResponseEntity<>(returnedBody, HttpStatus.OK);
178 when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponseNoContent).thenReturn(getResponseOk);
179 DeleteA1PolicyOutput resultNoContent = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
180 assertEquals(Integer.valueOf(HttpStatus.NO_CONTENT.value()), resultNoContent.getHttpStatus());
181 DeleteA1PolicyOutput resultOk = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
182 assertEquals(returnedBody, resultOk.getBody());
183 assertEquals(HTTP_OK_AS_INTEGER, resultOk.getHttpStatus());
187 public void testDeleteA1Failure() throws InterruptedException, ExecutionException {
188 DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder();
189 inputBuilder.setNearRtRicUrl(nearRtRicUrl);
190 Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
191 String returnedBody = "DELETE failed";
192 Integer notFoundStatusCode = HttpStatus.NOT_FOUND.value();
193 when(restAdapter.delete(nearRtRicUrl.getValue()))
194 .thenThrow(new RestClientResponseException(null, notFoundStatusCode, null, null, returnedBody.getBytes(), null));
195 DeleteA1PolicyOutput result = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
196 assertEquals(returnedBody, result.getBody());
197 assertEquals(notFoundStatusCode, result.getHttpStatus());