2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.ccsdk.features.sdnr.northbound.addCMHandle;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
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;
52 public class AddCMHandleProviderTest extends Mockito {
54 private static AddCMHandleProvider addCMHandleProvider;
55 private static DataBroker dataBrokerNetconf;
56 private @NonNull static DataTreeChangeListener<Node> listener;
57 private @NonNull static ClusteredDataTreeChangeListener<Node> listenerClustered;
59 private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProviderTest.class);
60 private static List<String> nodeIdList = new ArrayList<>();
62 @SuppressWarnings("unchecked")
64 public static <T extends DataObject, L extends DataTreeChangeListener<T>> void before()
65 throws InterruptedException, IOException {
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;
79 return new ListenerRegistration<L>() {
81 public L getInstance() {
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");
114 public void initializationTest() {
116 LOG.info("Verify init state");
117 assertTrue("Devicemanager not initialized", addCMHandleProvider.isInitializationSuccessful());
121 public void sendNotificationToCpsTest() {
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");
127 assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success");
133 public void sendNotificationToDmaapTest() {
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");
139 assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success");
145 public static void after() throws InterruptedException, IOException {
146 LOG.info("Start shutdown");
147 addCMHandleProvider.close();