114640719398df999e31fd553da291faaac18469
[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.eq;
24 import static org.mockito.Mockito.when;
25 import com.google.common.util.concurrent.ListenableFuture;
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.restadapter.NearRicUrlProvider;
38 import org.onap.sdnc.northbound.restadapter.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.rev200122.GetPolicyIdentitiesInputBuilder;
44 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyIdentitiesOutput;
45 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyStatusOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput;
51 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInputBuilder;
52 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.ResponseEntity;
58
59 /**
60  * This class Tests all the methods in NonrtRicApiProvider
61  *
62  * @author lathishbabu.ganesan@est.tech
63  *
64  */
65
66 @RunWith(MockitoJUnitRunner.class)
67 public class NonrtRicApiProviderTest extends AbstractConcurrentDataBrokerTest {
68
69   protected static final Logger LOG = LoggerFactory.getLogger(NonrtRicApiProviderTest.class);
70   protected NonrtRicApiProvider nonrtRicApiProvider;
71   protected DataBroker dataBroker;
72   @Mock
73   protected NotificationPublishService mockNotificationPublishService;
74   @Mock
75   protected RpcProviderRegistry mockRpcProviderRegistry;
76   @Mock
77   private RestAdapter restAdapter;
78   private NearRicUrlProvider nearRicUrlProvider;
79   private static String nearRtRicUrl = "http://ric1:8085";
80   private static String policyTypeId = "STD_QoSNudging_0.1.0";
81   private static String policyId = "3d2157af-6a8f-4a7c-810f-38c2f824bf12";
82
83   @Before
84   public void setUp() throws Exception {
85     nearRicUrlProvider = new NearRicUrlProvider();
86     dataBroker = getDataBroker();
87     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService, mockRpcProviderRegistry);
88   }
89
90   @Test
91   public void testGetPolicyTypeIdentities() throws InterruptedException, ExecutionException {
92     GetPolicyTypeIdentitiesInputBuilder inputBuilder = new GetPolicyTypeIdentitiesInputBuilder();
93     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
94     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
95     String uri = nearRicUrlProvider.policyTypesUrl(inputBuilder.build().getNearRtRicUrl());
96     List<String> policyTypeIdentities = new ArrayList<>();
97     policyTypeIdentities.add(policyTypeId);
98     ResponseEntity<Object> getPolicyTypeIdentitiesResponse = new ResponseEntity<>(policyTypeIdentities, HttpStatus.OK);
99     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyTypeIdentitiesResponse);
100     ListenableFuture<RpcResult<GetPolicyTypeIdentitiesOutput>> result = nonrtRicApiProvider
101         .getPolicyTypeIdentities(inputBuilder.build());
102     Assert.assertEquals(policyTypeIdentities, result.get().getResult().getPolicyTypeIdList());
103   }
104
105   @Test
106   public void testGetPolicyIdentities() throws InterruptedException, ExecutionException {
107     GetPolicyIdentitiesInputBuilder inputBuilder = new GetPolicyIdentitiesInputBuilder();
108     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
109     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
110     String uri = nearRicUrlProvider.policiesUrl(inputBuilder.build().getNearRtRicUrl());
111     List<String> policyIdentities = new ArrayList<>();
112     policyIdentities.add(policyId);
113     ResponseEntity<Object> getPolicyIdentitiesResponse = new ResponseEntity<>(policyIdentities, HttpStatus.OK);
114     when(restAdapter.get(eq(uri), eq(List.class))).thenReturn(getPolicyIdentitiesResponse);
115     ListenableFuture<RpcResult<GetPolicyIdentitiesOutput>> result = nonrtRicApiProvider
116         .getPolicyIdentities(inputBuilder.build());
117     Assert.assertEquals(policyIdentities, result.get().getResult().getPolicyIdList());
118   }
119
120   @Test
121   public void testGetPolicyType() throws InterruptedException, ExecutionException {
122     GetPolicyTypeInputBuilder inputBuilder = new GetPolicyTypeInputBuilder();
123     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
124     inputBuilder.setPolicyTypeId(policyTypeId);
125     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
126     String uri = nearRicUrlProvider.getPolicyTypeUrl(inputBuilder.build().getNearRtRicUrl(),
127         String.valueOf(inputBuilder.build().getPolicyTypeId()));
128     String testPolicyType = "{}";
129     ResponseEntity<Object> getPolicyTypeResponse = new ResponseEntity<>(testPolicyType, HttpStatus.OK);
130     when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyTypeResponse);
131     ListenableFuture<RpcResult<GetPolicyTypeOutput>> result = nonrtRicApiProvider.getPolicyType(inputBuilder.build());
132     Assert.assertEquals(testPolicyType, result.get().getResult().getPolicyType());
133   }
134
135   @Test
136   public void testPutPolicy() throws InterruptedException, ExecutionException {
137     PutPolicyInputBuilder inputBuilder = new PutPolicyInputBuilder();
138     String testPolicy = "{}";
139     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
140     inputBuilder.setPolicyId(policyId);
141     inputBuilder.setPolicyTypeId(policyTypeId);
142     inputBuilder.setPolicy(testPolicy);
143     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
144     String uri = nearRicUrlProvider.putPolicyUrl(inputBuilder.build().getNearRtRicUrl(), inputBuilder.getPolicyId(),
145         inputBuilder.getPolicyTypeId());
146     ResponseEntity<String> putPolicyResponse = new ResponseEntity<>(testPolicy, HttpStatus.CREATED);
147     when(restAdapter.put(eq(uri), eq(testPolicy), eq(String.class))).thenReturn(putPolicyResponse);
148     ListenableFuture<RpcResult<PutPolicyOutput>> result = nonrtRicApiProvider.putPolicy(inputBuilder.build());
149     Assert.assertEquals(testPolicy, result.get().getResult().getReturnedPolicy());
150   }
151
152   @Test
153   public void testGetPolicyStatus() throws InterruptedException, ExecutionException {
154     GetPolicyStatusInputBuilder inputBuilder = new GetPolicyStatusInputBuilder();
155     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
156     inputBuilder.setPolicyId(policyId);
157     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
158     String uri = nearRicUrlProvider.getPolicyStatusUrl(nearRtRicUrl, policyId);
159     String testPolicyStatus = "STATUS";
160     ResponseEntity<Object> getPolicyStatusResponse = new ResponseEntity<>(testPolicyStatus, HttpStatus.OK);
161     when(restAdapter.get(eq(uri), eq(String.class))).thenReturn(getPolicyStatusResponse);
162     ListenableFuture<RpcResult<GetPolicyStatusOutput>> result = nonrtRicApiProvider
163         .getPolicyStatus(inputBuilder.build());
164     Assert.assertEquals(testPolicyStatus, result.get().getResult().getPolicyStatus());
165   }
166
167 }