Support multiple nearRtRICs and fix unit-tests
[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 com.google.common.base.Optional;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.concurrent.ExecutionException;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.internal.util.reflection.Whitebox;
37 import org.mockito.runners.MockitoJUnitRunner;
38 import org.onap.sdnc.northbound.provider.NonrtRicApiProvider;
39 import org.onap.sdnc.northbound.restadpter.NearRicUrlProvider;
40 import org.onap.sdnc.northbound.restadpter.RestAdapter;
41 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
42 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
43 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
44 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
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.GetHealthCheckInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetHealthCheckOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceInputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstanceOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesInputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyInstancesOutput;
53 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeInputBuilder;
54 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypeOutput;
55 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesInputBuilder;
56 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetPolicyTypesOutput;
57 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusInputBuilder;
58 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev191002.GetStatusOutput;
59 import org.opendaylight.yangtools.yang.common.RpcResult;
60 import org.slf4j.Logger;
61 import org.slf4j.LoggerFactory;
62
63 /**
64  * This class Tests all the methods in NonrtRicApiProvider
65  *
66  * @author lathishbabu.ganesan@est.tech
67  *
68  */
69
70 @RunWith(MockitoJUnitRunner.class)
71 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
72
73   protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
74   protected NonrtRicApiProvider nonrtRicApiProvider;
75   protected DataBroker dataBroker;
76   @Mock
77   protected NotificationPublishService mockNotificationPublishService;
78   @Mock
79   protected RpcProviderRegistry mockRpcProviderRegistry;
80   @Mock
81   private RestAdapter restAdapter;
82   private NearRicUrlProvider nearRicUrlProvider;
83   private static String nearRtRicId = "NearRtRic1";
84   private static Long policyTypeId = 11L;
85   private static String policyTypeInstanceId = "12";
86
87
88   @Before
89   public void setUp() throws Exception {
90     nearRicUrlProvider = new NearRicUrlProvider();
91     dataBroker = getDataBroker();
92     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService,
93         mockRpcProviderRegistry);
94   }
95
96   @Test
97   public void testCreatePolicyType() throws InterruptedException, ExecutionException {
98     CreatePolicyTypeInputBuilder inputBuilder = new CreatePolicyTypeInputBuilder();
99     inputBuilder.setNearRtRicId(nearRtRicId);
100     inputBuilder.setPolicyTypeId(policyTypeId);
101     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
102     String uri = nearRicUrlProvider.getPolicyTypeId(inputBuilder.build().getNearRtRicId(),
103                 String.valueOf(inputBuilder.build().getPolicyTypeId()));
104     Optional<Object> createPolicyTyperesponse = null;
105     when(restAdapter.put(eq(uri), anyObject())).thenReturn(createPolicyTyperesponse);
106     ListenableFuture<RpcResult<CreatePolicyTypeOutput>> result =
107         nonrtRicApiProvider.createPolicyType(inputBuilder.build());
108     Assert.assertEquals("Success", result.get().getResult().getStatus());
109   }
110
111   @Test
112   public void testGetPolicyType() throws InterruptedException, ExecutionException {
113     GetPolicyTypeInputBuilder inputBuilder = new GetPolicyTypeInputBuilder();
114     inputBuilder.setNearRtRicId(nearRtRicId);
115     inputBuilder.setPolicyTypeId(policyTypeId);
116     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
117     String uri = nearRicUrlProvider.getPolicyTypeId(inputBuilder.build().getNearRtRicId(),
118             String.valueOf(inputBuilder.build().getPolicyTypeId()));
119     String policyType =
120             "{\"name\":\"Policy type 1\",\"description\":\"PT 1\",\"policy_type_id\":1,\"create_schema\":{}}";
121     when(restAdapter.get(eq(uri), anyObject())).thenReturn(Optional.of(policyType));
122     ListenableFuture<RpcResult<GetPolicyTypeOutput>> result =
123         nonrtRicApiProvider.getPolicyType(inputBuilder.build());
124     Assert.assertEquals("Policy type 1", result.get().getResult().getName());
125   }
126
127   @Test
128   public void testGetPolicyTypes() throws InterruptedException, ExecutionException {
129     GetPolicyTypesInputBuilder inputBuilder = new GetPolicyTypesInputBuilder();
130     inputBuilder.setNearRtRicId(nearRtRicId);
131     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
132     String uri = nearRicUrlProvider.getPolicyTypes(inputBuilder.build().getNearRtRicId());
133     List<Integer> policyTypesInteger = new ArrayList<>();
134     policyTypesInteger.add(20001);
135     List<Long> policyTypesLong = new ArrayList<>();
136     policyTypesLong.add(20001L);
137     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(policyTypesInteger));
138     ListenableFuture<RpcResult<GetPolicyTypesOutput>> result =
139         nonrtRicApiProvider.getPolicyTypes(inputBuilder.build());
140     Assert.assertEquals(policyTypesLong, result.get().getResult().getPolicyTypeIdList());
141   }
142
143   @Test
144   public void testGetPolicyInstance() throws InterruptedException, ExecutionException {
145     GetPolicyInstanceInputBuilder inputBuilder = new GetPolicyInstanceInputBuilder();
146     inputBuilder.setNearRtRicId(nearRtRicId);
147     inputBuilder.setPolicyTypeId(policyTypeId);
148     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
149     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
150     String uri = nearRicUrlProvider.getPolicyInstanceId(inputBuilder.build().getNearRtRicId(),
151         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
152     String policyInstance =
153             "{\"scope\":{\"ue_id\":\"2\"},\"statement\":{\"priority_level\":\"1\"},\"policy_id\":\"pi12\"}";
154     when(restAdapter.get(eq(uri), eq(String.class)))
155         .thenReturn(Optional.of(policyInstance));
156     ListenableFuture<RpcResult<GetPolicyInstanceOutput>> result =
157         nonrtRicApiProvider.getPolicyInstance(inputBuilder.build());
158     Assert.assertEquals(policyInstance, result.get().getResult().getPolicyInstance());
159   }
160
161   @Test
162   public void testGetPolicyInstances() throws InterruptedException, ExecutionException {
163     GetPolicyInstancesInputBuilder inputBuilder = new GetPolicyInstancesInputBuilder();
164     inputBuilder.setNearRtRicId(nearRtRicId);
165     inputBuilder.setPolicyTypeId(policyTypeId);
166     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
167     String uri = nearRicUrlProvider.getPolicyInstances(inputBuilder.build().getNearRtRicId(),
168             String.valueOf(inputBuilder.build().getPolicyTypeId()));
169     List<String> policyInstances = new ArrayList<>();
170     policyInstances.add("3d2157af-6a8f-4a7c-810f-38c2f824bf12");
171     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(Optional.of(policyInstances));
172     ListenableFuture<RpcResult<GetPolicyInstancesOutput>> result =
173         nonrtRicApiProvider.getPolicyInstances(inputBuilder.build());
174     Assert.assertEquals(policyInstances, result.get().getResult().getPolicyInstanceIdList());
175   }
176
177   @Test
178   public void testGetStatus() throws InterruptedException, ExecutionException {
179     GetStatusInputBuilder inputBuilder = new GetStatusInputBuilder();
180     inputBuilder.setNearRtRicId(nearRtRicId);
181     inputBuilder.setPolicyTypeId(policyTypeId);
182     inputBuilder.setPolicyInstanceId(policyTypeInstanceId);
183     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
184     String uri = nearRicUrlProvider.getPolicyInstanceIdStatus(inputBuilder.build().getNearRtRicId(),
185         String.valueOf(inputBuilder.build().getPolicyTypeId()), inputBuilder.getPolicyInstanceId());
186     String policyInstanceStatus = "{\"status\":\"enforced\"}";
187     when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(Optional.of(policyInstanceStatus));
188     ListenableFuture<RpcResult<GetStatusOutput>> result =
189         nonrtRicApiProvider.getStatus(inputBuilder.build());
190     Assert.assertEquals("enforced", result.get().getResult().getStatus());
191   }
192
193   @Test
194   public void testHealthCheck() throws InterruptedException, ExecutionException {
195     GetHealthCheckInputBuilder inputBuilder = new GetHealthCheckInputBuilder();
196     inputBuilder.setNearRtRicId(nearRtRicId);
197     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
198     String uri = nearRicUrlProvider.getHealthCheck(inputBuilder.build().getNearRtRicId());
199     String healthCheckStatus = "";
200     when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(null);
201     ListenableFuture<RpcResult<GetHealthCheckOutput>> result =
202         nonrtRicApiProvider.getHealthCheck(inputBuilder.build());
203     Assert.assertEquals(true, result.get().getResult().isHealthStatus());
204   }
205 }