Adapt A1 controller to new interface
[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.GetPolicyTypeIdentitiesInputBuilder;
46 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeIdentitiesOutput;
47 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeInputBuilder;
48 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.GetPolicyTypeOutput;
49 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyInputBuilder;
50 import org.opendaylight.yang.gen.v1.org.onap.sdnc.northbound.a1.adapter.rev200122.PutPolicyOutput;
51 import org.opendaylight.yangtools.yang.common.RpcResult;
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 NearRicUrlProvider nearRicUrlProvider;
77   private static String nearRtRicUrl = "http://ric1:8085";
78   private static String policyTypeId = "STD_QoSNudging_0.1.0";
79   private static String policyId = "3d2157af-6a8f-4a7c-810f-38c2f824bf12";
80
81
82   @Before
83   public void setUp() throws Exception {
84     nearRicUrlProvider = new NearRicUrlProvider();
85     dataBroker = getDataBroker();
86     nonrtRicApiProvider = new NonrtRicApiProvider(dataBroker, mockNotificationPublishService,
87         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.getPolicyTypeIdentitiesUrl(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 =
101         nonrtRicApiProvider.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.getPolicyIdentitiesUrl(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 =
116         nonrtRicApiProvider.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 =
132         nonrtRicApiProvider.getPolicyType(inputBuilder.build());
133     Assert.assertEquals(testPolicyType, result.get().getResult().getPolicyType());
134   }
135
136   @Test
137   public void testPutPolicy() throws InterruptedException, ExecutionException {
138     PutPolicyInputBuilder inputBuilder = new PutPolicyInputBuilder();
139     String testPolicy = "{}";
140     inputBuilder.setNearRtRicUrl(nearRtRicUrl);
141     inputBuilder.setPolicyId(policyId);
142     inputBuilder.setPolicy(testPolicy);
143     Whitebox.setInternalState(nonrtRicApiProvider, "restAdapter", restAdapter);
144     String uri = nearRicUrlProvider.getPolicyUrl(inputBuilder.build().getNearRtRicUrl(),
145             inputBuilder.getPolicyId());
146     ResponseEntity<Object> putPolicyResponse = new ResponseEntity<>(testPolicy, HttpStatus.CREATED);
147     when(restAdapter.put(eq(uri), eq(testPolicy))).thenReturn(putPolicyResponse);
148     ListenableFuture<RpcResult<PutPolicyOutput>> result =
149         nonrtRicApiProvider.putPolicy(inputBuilder.build());
150     Assert.assertEquals(testPolicy, result.get().getResult().getReturnedPolicy());
151   }
152 }