Fixes from local testing
[ccsdk/features.git] / sdnr / northbound / addCMHandle / provider / src / test / java / org / onap / ccsdk / features / sdnr / northbound / addCMHandle / AddCMHandleProviderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2021-2022 Wipro Limited.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.features.sdnr.northbound.addCMHandle;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.eclipse.jdt.annotation.NonNull;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.mockito.MockedStatic;
35 import org.mockito.Mockito;
36 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
37 import org.opendaylight.mdsal.binding.api.DataBroker;
38 import org.opendaylight.mdsal.binding.api.DataTreeChangeListener;
39 import org.opendaylight.mdsal.binding.api.MountPointService;
40 import org.opendaylight.mdsal.binding.api.NotificationPublishService;
41 import org.opendaylight.mdsal.binding.api.RpcProviderService;
42 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
43 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
44 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
45 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
46 import org.opendaylight.yangtools.concepts.ListenerRegistration;
47 import org.opendaylight.yangtools.yang.binding.DataObject;
48 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class AddCMHandleProviderTest extends Mockito {
53
54     private static AddCMHandleProvider addCMHandleProvider;
55     private static DataBroker dataBrokerNetconf;
56     private @NonNull static DataTreeChangeListener<Node> listener;
57     private @NonNull static ClusteredDataTreeChangeListener<Node> listenerClustered;
58
59     private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProviderTest.class);
60     private static List<String> nodeIdList = new ArrayList<>();
61
62     @SuppressWarnings("unchecked")
63     @BeforeClass
64     public static <T extends DataObject, L extends DataTreeChangeListener<T>> void before()
65             throws InterruptedException, IOException {
66
67         LOG.info("Logger: " + LOG.getClass().getName() + " " + LOG.getName());
68         dataBrokerNetconf = mock(DataBroker.class);
69         when(dataBrokerNetconf.registerDataTreeChangeListener(Mockito.any(), Mockito.any())).thenAnswer(invocation -> {
70             Object pListener = invocation.getArguments()[1];
71             LOG.info("Register " + pListener.getClass().getName());
72             if (pListener instanceof ClusteredDataTreeChangeListener) {
73                 System.out.println("Clustered listener");
74                 listenerClustered = (ClusteredDataTreeChangeListener<Node>) pListener;
75             } else if (pListener instanceof DataTreeChangeListener) {
76                 System.out.println("Listener");
77                 listener = (DataTreeChangeListener<Node>) pListener;
78             }
79             return new ListenerRegistration<L>() {
80                 @Override
81                 public L getInstance() {
82                     return (L) pListener;
83                 }
84
85                 @Override
86                 public void close() {
87                 }
88             };
89
90         });
91
92         addCMHandleProvider = new AddCMHandleProvider();
93         MountPointService mountPointService = mock(MountPointService.class);
94         DOMMountPointService domMountPointService = mock(DOMMountPointService.class);
95         RpcProviderService rpcProviderRegistry = mock(RpcProviderService.class);
96         NotificationPublishService notificationPublishService = mock(NotificationPublishService.class);
97         ClusterSingletonServiceProvider clusterSingletonServiceProvider = mock(ClusterSingletonServiceProvider.class);
98         YangParserFactory yangParserFactory = mock(YangParserFactory.class);
99         BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer = mock(BindingNormalizedNodeSerializer.class);
100         addCMHandleProvider.setDataBroker(dataBrokerNetconf);
101         addCMHandleProvider.setMountPointService(mountPointService);
102         addCMHandleProvider.setDomMountPointService(domMountPointService);
103         addCMHandleProvider.setNotificationPublishService(notificationPublishService);
104         addCMHandleProvider.setRpcProviderRegistry(rpcProviderRegistry);
105         addCMHandleProvider.setClusterSingletonService(clusterSingletonServiceProvider);
106         addCMHandleProvider.setYangParserFactory(yangParserFactory);
107         addCMHandleProvider.setBindingNormalizedNodeSerializer(bindingNormalizedNodeSerializer);
108         addCMHandleProvider.init();
109         nodeIdList.add("ncserver1");
110         LOG.info("Initialization done");
111     }
112
113     @Test
114     public void initializationTest() {
115
116         LOG.info("Verify init state");
117         assertTrue("Devicemanager not initialized", addCMHandleProvider.isInitializationSuccessful());
118     }
119
120     @Test
121     public void sendNotificationToCpsTest() {
122
123         LOG.info("Send notification to Cps test");
124         try (MockedStatic<HttpRequester> utilities = Mockito.mockStatic(HttpRequester.class)) {
125             utilities.when(() -> HttpRequester.sendPostRequest(any(), any(), any())).thenReturn("Success");
126
127             assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success");
128         }
129
130     }
131
132     @Test
133     public void sendNotificationToDmaapTest() {
134
135         LOG.info("Send notification to Cps test");
136         try (MockedStatic<HttpRequester> utilities = Mockito.mockStatic(HttpRequester.class)) {
137             utilities.when(() -> HttpRequester.sendPostRequest(any(), any(), any())).thenReturn("Success");
138
139             assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success");
140         }
141
142     }
143
144     @AfterClass
145     public static void after() throws InterruptedException, IOException {
146         LOG.info("Start shutdown");
147         addCMHandleProvider.close();
148
149     }
150
151 }