Merge "Remove sleep from DmaapMessageConsumer"
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / test / java / org / o_ran_sc / nonrtric / sdnc_a1 / northbound / provider / NonrtRicApiProviderTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.o_ran_sc.nonrtric.sdnc_a1.northbound.provider;
22
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;
54
55 /**
56  * This class Tests all the methods in NonrtRicApiProvider
57  *
58  * @author lathishbabu.ganesan@est.tech
59  *
60  */
61
62 @RunWith(MockitoJUnitRunner.class)
63 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
64
65   protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
66   protected NonrtRicApiProvider nonrtRicApiProvider;
67   protected DataBroker dataBroker;
68   @Mock
69   protected NotificationPublishService mockNotificationPublishService;
70   @Mock
71   protected RpcProviderRegistry mockRpcProviderRegistry;
72   @Mock
73   private RestAdapter restAdapter;
74   private static Uri nearRtRicUrl = new Uri("http://ric1:8085");
75
76   @Before
77   public void setUp() throws Exception {
78     dataBroker = getDataBroker();
79     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry);
80   }
81
82   @Test
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());
93   }
94
95   @Test
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());
106   }
107
108   @Test
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());
119   }
120
121   @Test
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());
133   }
134
135   @Test
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());
148   }
149
150   @Test
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());
164   }
165
166   @Test
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());
180   }
181
182   @Test
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());
194   }
195
196 }