Merge "Remove unnecessary stuff from northbound directory of A1 controller"
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / test / java / org / o_ran_sc / nonrtric / sdnc_a1 / northbound / 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;
22
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.when;
25
26 import java.util.concurrent.ExecutionException;
27
28 import org.junit.Assert;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.internal.util.reflection.Whitebox;
34 import org.mockito.runners.MockitoJUnitRunner;
35 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.provider.NonrtRicApiProvider;
36 import org.o_ran_sc.nonrtric.sdnc_a1.northbound.restadapter.RestAdapter;
37 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
38 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
39 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
40 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
41 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyInputBuilder;
42 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.DeleteA1PolicyOutput;
43 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyOutput;
45 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyStatusOutput;
47 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.GetA1PolicyTypeOutput;
49 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyInputBuilder;
50 import org.opendaylight.yang.gen.v1.org.o_ran_sc.nonrtric.sdnc_a1.northbound.a1.adapter.rev200122.PutA1PolicyOutput;
51 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54 import org.springframework.http.HttpStatus;
55 import org.springframework.http.ResponseEntity;
56
57 /**
58  * This class Tests all the methods in NonrtRicApiProvider
59  *
60  * @author lathishbabu.ganesan@est.tech
61  *
62  */
63
64 @RunWith(MockitoJUnitRunner.class)
65 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
66
67   protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
68   protected NonrtRicApiProvider nonrtRicApiProvider;
69   protected DataBroker dataBroker;
70   @Mock
71   protected NotificationPublishService mockNotificationPublishService;
72   @Mock
73   protected RpcProviderRegistry mockRpcProviderRegistry;
74   @Mock
75   private RestAdapter restAdapter;
76   private static Uri nearRtRicUrl = new Uri("http://ric1:8085");
77
78   @Before
79   public void setUp() throws Exception {
80     dataBroker = getDataBroker();
81     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry);
82   }
83
84   @Test
85   public void testGetA1Policy() throws InterruptedException, ExecutionException {
86     GetA1PolicyInputBuilder inputBuilder = new GetA1PolicyInputBuilder();
87     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
88     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
89     String returnedBody = "returned body";
90     ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
91     when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
92     GetA1PolicyOutput result = nonrtRicApiProvider.getA1Policy(inputBuilder.build()).get().getResult();
93     Assert.assertEquals(returnedBody, result.getBody());
94     Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
95   }
96
97   @Test
98   public void testGetA1PolicyType() throws InterruptedException, ExecutionException {
99     GetA1PolicyTypeInputBuilder inputBuilder = new GetA1PolicyTypeInputBuilder();
100     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
101     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
102     String returnedBody = "returned body";
103     ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
104     when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
105     GetA1PolicyTypeOutput result = nonrtRicApiProvider.getA1PolicyType(inputBuilder.build()).get().getResult();
106     Assert.assertEquals(returnedBody, result.getBody());
107     Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
108   }
109
110   @Test
111   public void testGetA1PolicyStatus() throws InterruptedException, ExecutionException {
112     GetA1PolicyStatusInputBuilder inputBuilder = new GetA1PolicyStatusInputBuilder();
113     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
114     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
115     String returnedBody = "returned body";
116     ResponseEntity<Object> getResponse = new ResponseEntity<>(returnedBody, HttpStatus.OK);
117     when(restAdapter.get(eq(nearRtRicUrl.getValue()), eq(String.class))).thenReturn(getResponse);
118     GetA1PolicyStatusOutput result = nonrtRicApiProvider.getA1PolicyStatus(inputBuilder.build()).get().getResult();
119     Assert.assertEquals(returnedBody, result.getBody());
120     Assert.assertTrue(HttpStatus.OK.value() == result.getHttpStatus());
121   }
122
123   @Test
124   public void testPutA1Policy() throws InterruptedException, ExecutionException {
125     PutA1PolicyInputBuilder inputBuilder = new PutA1PolicyInputBuilder();
126     String testPolicy = "{}";
127     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
128     inputBuilder.setBody(testPolicy);
129     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
130     String returnedBody = "returned body";
131     ResponseEntity<String> putResponse = new ResponseEntity<>(returnedBody, HttpStatus.CREATED);
132     when(restAdapter.put(eq(nearRtRicUrl.getValue()), eq(testPolicy), eq(String.class))).thenReturn(putResponse);
133     PutA1PolicyOutput result = nonrtRicApiProvider.putA1Policy(inputBuilder.build()).get().getResult();
134     Assert.assertEquals(returnedBody, result.getBody());
135     Assert.assertTrue(HttpStatus.CREATED.value() == result.getHttpStatus());
136   }
137
138   @Test
139   public void testDeleteA1() throws InterruptedException, ExecutionException {
140     DeleteA1PolicyInputBuilder inputBuilder = new DeleteA1PolicyInputBuilder();
141     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
142     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
143
144     ResponseEntity<Object> getResponse = new ResponseEntity<>(HttpStatus.NO_CONTENT);
145     when(restAdapter.delete(nearRtRicUrl.getValue())).thenReturn(getResponse);
146     DeleteA1PolicyOutput result = nonrtRicApiProvider.deleteA1Policy(inputBuilder.build()).get().getResult();
147     Assert.assertTrue(HttpStatus.NO_CONTENT.value() == result.getHttpStatus());
148   }
149
150 }