NonRT-RIC A1 Northbound API
[nonrtric.git] / sdnc-a1-controller / northbound / nonrt-ric-api / provider / src / test / java / org / onap / sdnc / 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.onap.sdnc.northbound;
22
23 import static org.mockito.Matchers.anyObject;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.when;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.concurrent.ExecutionException;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.internal.util.reflection.Whitebox;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.sdnc.northbound.provider.NonrtRicApiProvider;
37 import org.onap.sdnc.northbound.restadpter.NearRicUrlProvider;
38 import org.onap.sdnc.northbound.restadpter.RestAdapter;
39 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
40 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
41 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
42 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
43 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyInstanceOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.CreatePolicyTypeOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyInstanceOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeInputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.DeletePolicyTypeOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckInputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceInputBuilder;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceOutput;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesInputBuilder;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesOutput;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeInputBuilder;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeOutput;
59 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesInputBuilder;
60 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesOutput;
61 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusInputBuilder;
62 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusOutput;
63 import org.opendaylight.yangtools.yang.common.RpcResult;
64 import org.oransc.ric.a1med.client.model.PolicyTypeSchema;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67 import com.google.common.base.Optional;
68 import com.google.common.util.concurrent.ListenableFuture;
69
70 /**
71  * This class Tests all the methods in NonrtRicApiProvider
72  * 
73  * @author lathishbabu.ganesan@est.tech
74  *
75  */
76
77 @RunWith(MockitoJUnitRunner.class)
78 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
79
80   protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
81   protected NonrtRicApiProvider nonrtRicApiProvider;
82   protected DataBroker dataBroker;
83   @Mock
84   protected NotificationPublishService mockNotificationPublishService;
85   @Mock
86   protected RpcProviderRegistry mockRpcProviderRegistry;
87   @Mock
88   private RestAdapter restAdapter;
89   private NearRicUrlProvider nearRicUrlProvider;
90   private static Long policyTypeId = 11L;
91   private static String policyTypeInstanceId = "12";
92
93
94   @Before
95   public void setUp() throws Exception {
96     nearRicUrlProvider = new NearRicUrlProvider();
97     dataBroker = getDataBroker();
98     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService,
99         mockRpcProviderRegistry);
100   }
101
102   @Test
103   public void testCreatePolicyType() throws InterruptedException, ExecutionException {
104     CreatePolicyTypeInputBuilder inputBuilder = new CreatePolicyTypeInputBuilder();
105     inputBuilder.setPolicyTypeId(policyTypeId);
106     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
107     String uri =
108         nearRicUrlProvider.getPolicyTypeId(String.valueOf(inputBuilder.build().getPolicyTypeId()));
109     Optional<Object> createPolicyTyperesponse = null;
110     when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyTyperesponse);
111     ListenableFuture<RpcResult<CreatePolicyTypeOutput>> result =
112         nonrtRicApiProvider.createPolicyType(inputBuilder.build());
113     Assert.assertEquals("Success", result.get().getResult().getStatus());
114   }
115
116   @Test
117   public void testGetPolicyType() throws InterruptedException, ExecutionException {
118     GetPolicyTypeInputBuilder inputBuilder = new GetPolicyTypeInputBuilder();
119     inputBuilder.setPolicyTypeId(policyTypeId);
120     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
121     String uri =
122         nearRicUrlProvider.getPolicyTypeId(String.valueOf(inputBuilder.build().getPolicyTypeId()));
123     PolicyTypeSchema policyTypeSchema = new PolicyTypeSchema();
124     policyTypeSchema.setName("AdmissionControlPolicy");
125     policyTypeSchema.setCreateSchema("{}");
126     when(restAdapter.get(eq(uri), anyObject())).thenReturn(Optional.of(policyTypeSchema));
127     ListenableFuture<RpcResult<GetPolicyTypeOutput>> result =
128         nonrtRicApiProvider.getPolicyType(inputBuilder.build());
129     Assert.assertEquals(policyTypeSchema.getName(), result.get().getResult().getName());
130   }
131
132   @Test
133   public void testGetPolicyTypes() throws InterruptedException, ExecutionException {
134     GetPolicyTypesInputBuilder inputBuilder = new GetPolicyTypesInputBuilder();
135     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
136     String uri = nearRicUrlProvider.getPolicyTypes();
137     List<Long> policyTypes = new ArrayList<>();
138     policyTypes.add(20001L);
139     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(policyTypes));
140     ListenableFuture<RpcResult<GetPolicyTypesOutput>> result =
141         nonrtRicApiProvider.getPolicyTypes(inputBuilder.build());
142     Assert.assertEquals(policyTypes, result.get().getResult().getPolicyTypeIdList());
143   }
144
145   @Test
146   public void testDeletePolicyType() throws InterruptedException, ExecutionException {
147     DeletePolicyTypeInputBuilder inputBuilder = new DeletePolicyTypeInputBuilder();
148     inputBuilder.setPolicyTypeId(policyTypeId);
149     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
150     String uri =
151         nearRicUrlProvider.getPolicyTypeId(String.valueOf(inputBuilder.build().getPolicyTypeId()));
152     Optional<Object> deletePolicyTyperesponse = null;
153     when(restAdapter.delete(uri)).thenReturn(deletePolicyTyperesponse);
154     ListenableFuture<RpcResult<DeletePolicyTypeOutput>> result =
155         nonrtRicApiProvider.deletePolicyType(inputBuilder.build());
156   }
157
158   @Test
159   public void testCreatePolicyInstance() throws InterruptedException, ExecutionException {
160     CreatePolicyInstanceInputBuilder inputBuilder = new CreatePolicyInstanceInputBuilder();
161     inputBuilder.setPolicyTypeId(policyTypeId);
162     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
163     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
164     String uri = nearRicUrlProvider.getPolicyInstanceId(
165         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
166     Optional<Object> createPolicyInstanceresponse = null;
167     when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyInstanceresponse);
168     ListenableFuture<RpcResult<CreatePolicyInstanceOutput>> result =
169         nonrtRicApiProvider.createPolicyInstance(inputBuilder.build());
170   }
171
172   @Test
173   public void testDeletePolicyInstance() throws InterruptedException, ExecutionException {
174     DeletePolicyInstanceInputBuilder inputBuilder = new DeletePolicyInstanceInputBuilder();
175     inputBuilder.setPolicyTypeId(policyTypeId);
176     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
177     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
178     String uri = nearRicUrlProvider.getPolicyInstanceId(
179         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
180     Optional<Object> deletePolicyInstanceresponse = null;
181     when(restAdapter.delete(uri)).thenReturn(deletePolicyInstanceresponse);
182     ListenableFuture<RpcResult<DeletePolicyInstanceOutput>> result =
183         nonrtRicApiProvider.deletePolicyInstance(inputBuilder.build());
184   }
185
186   @Test
187   public void testGetPolicyInstance() throws InterruptedException, ExecutionException {
188     GetPolicyInstanceInputBuilder inputBuilder = new GetPolicyInstanceInputBuilder();
189     inputBuilder.setPolicyTypeId(policyTypeId);
190     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
191     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
192     String uri = nearRicUrlProvider.getPolicyInstanceId(
193         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
194     String getPolicyInstanceresponse = "{}";
195     when(restAdapter.get(eq(uri), eq(String.class)))
196         .thenReturn(Optional.of(getPolicyInstanceresponse));
197     ListenableFuture<RpcResult<GetPolicyInstanceOutput>> result =
198         nonrtRicApiProvider.getPolicyInstance(inputBuilder.build());
199   }
200
201   @Test
202   public void testGetPolicyInstances() throws InterruptedException, ExecutionException {
203     GetPolicyInstancesInputBuilder inputBuilder = new GetPolicyInstancesInputBuilder();
204     inputBuilder.setPolicyTypeId(policyTypeId);
205     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
206     String uri = nearRicUrlProvider
207         .getPolicyInstances(String.valueOf(inputBuilder.build().getPolicyTypeId()));
208     List<String> getPolicyInstances = new ArrayList<>();
209     getPolicyInstances.add("3d2157af-6a8f-4a7c-810f-38c2f824bf12");
210     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(getPolicyInstances));
211     ListenableFuture<RpcResult<GetPolicyInstancesOutput>> result =
212         nonrtRicApiProvider.getPolicyInstances(inputBuilder.build());
213   }
214
215   @Test
216   public void testGetStatus() throws InterruptedException, ExecutionException {
217     GetStatusInputBuilder inputBuilder = new GetStatusInputBuilder();
218     inputBuilder.setPolicyTypeId(policyTypeId);
219     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
220     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
221     String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(
222         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
223     List<String> getPolicyInstanceIdStatus = new ArrayList<>();
224     getPolicyInstanceIdStatus.add("");
225     when(restAdapter.get(eq(uri), eq(List.class)))
226         .thenReturn(Optional.of(getPolicyInstanceIdStatus));
227     ListenableFuture<RpcResult<GetStatusOutput>> result =
228         nonrtRicApiProvider.getStatus(inputBuilder.build());
229     // TODO: Define the proper response message for get policy instance status
230     Assert.assertEquals("Success", result.get().getResult().getStatus());
231   }
232
233   @Test
234   public void testHealthCheck() throws InterruptedException, ExecutionException {
235     GetHealthCheckInputBuilder inputBuilder = new GetHealthCheckInputBuilder();
236     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
237     String uri = nearRicUrlProvider.getHealthCheck();
238     String healthCheckStatus = "";
239     when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(Optional.of(healthCheckStatus));
240     ListenableFuture<RpcResult<GetHealthCheckOutput>> result =
241         nonrtRicApiProvider.getHealthCheck(inputBuilder.build());
242     Assert.assertEquals(true, result.get().getResult().isHealthStatus());
243   }
244 }