From 81043465b2a5332d21ac5b806f785ee69d6f62bf Mon Sep 17 00:00:00 2001 From: Niranjana Date: Thu, 3 Mar 2022 12:45:38 +0000 Subject: Optimize cm-handle registration with CPS-DMI Plugin to upload yang model Issue-ID: CCSDK-3597 Signed-off-by: Niranjana Change-Id: I1d9b0434c72650af164acf9d03a1ed1806668b38 --- .../addCMHandle/AddCMHandleProviderTest.java | 154 ++++++++++++++++++--- 1 file changed, 136 insertions(+), 18 deletions(-) (limited to 'sdnr/northbound/addCMHandle/provider/src/test/java/org') diff --git a/sdnr/northbound/addCMHandle/provider/src/test/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProviderTest.java b/sdnr/northbound/addCMHandle/provider/src/test/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProviderTest.java index 0dfe88c1f..f1edd4fae 100644 --- a/sdnr/northbound/addCMHandle/provider/src/test/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProviderTest.java +++ b/sdnr/northbound/addCMHandle/provider/src/test/java/org/onap/ccsdk/features/sdnr/northbound/addCMHandle/AddCMHandleProviderTest.java @@ -1,33 +1,151 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : CCSDK + * ================================================================================ + * Copyright (C) 2021-2022 Wipro Limited. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ package org.onap.ccsdk.features.sdnr.northbound.addCMHandle; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; -import com.sun.jersey.api.client.WebResource; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; -import java.util.concurrent.Future; - -import org.junit.After; -import org.junit.Before; +import org.eclipse.jdt.annotation.NonNull; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener; import org.opendaylight.mdsal.binding.api.DataBroker; +import org.opendaylight.mdsal.binding.api.DataTreeChangeListener; +import org.opendaylight.mdsal.binding.api.MountPointService; +import org.opendaylight.mdsal.binding.api.NotificationPublishService; import org.opendaylight.mdsal.binding.api.RpcProviderService; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.onap.ccsdk.features.sdnr.northbound.addCMHandle.AddCMHandleProvider; -public class AddCMHandleProviderTest { +import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer; +import org.opendaylight.mdsal.dom.api.DOMMountPointService; +import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider; +import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node; +import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AddCMHandleProviderTest extends Mockito { + + private static AddCMHandleProvider addCMHandleProvider; + private static DataBroker dataBrokerNetconf; + private @NonNull static DataTreeChangeListener listener; + private @NonNull static ClusteredDataTreeChangeListener listenerClustered; + + private static final Logger LOG = LoggerFactory.getLogger(AddCMHandleProviderTest.class); + private static List nodeIdList = new ArrayList<>(); + + @SuppressWarnings("unchecked") + @BeforeClass + public static > void before() + throws InterruptedException, IOException { + + LOG.info("Logger: " + LOG.getClass().getName() + " " + LOG.getName()); + dataBrokerNetconf = mock(DataBroker.class); + when(dataBrokerNetconf.registerDataTreeChangeListener(Mockito.any(), Mockito.any())).thenAnswer(invocation -> { + Object pListener = invocation.getArguments()[1]; + LOG.info("Register " + pListener.getClass().getName()); + if (pListener instanceof ClusteredDataTreeChangeListener) { + System.out.println("Clustered listener"); + listenerClustered = (ClusteredDataTreeChangeListener) pListener; + } else if (pListener instanceof DataTreeChangeListener) { + System.out.println("Listener"); + listener = (DataTreeChangeListener) pListener; + } + return new ListenerRegistration() { + @Override + public L getInstance() { + return (L) pListener; + } - private AddCMHandleProvider esProvider; + @Override + public void close() { + } + }; - @Before - public void setUp() throws Exception { - DataBroker dataBroker = mock(DataBroker.class); - RpcProviderService rpcRegistry = mock(RpcProviderService.class); - esProvider = new AddCMHandleProvider(); + }); + + addCMHandleProvider = new AddCMHandleProvider(); + MountPointService mountPointService = mock(MountPointService.class); + DOMMountPointService domMountPointService = mock(DOMMountPointService.class); + RpcProviderService rpcProviderRegistry = mock(RpcProviderService.class); + NotificationPublishService notificationPublishService = mock(NotificationPublishService.class); + ClusterSingletonServiceProvider clusterSingletonServiceProvider = mock(ClusterSingletonServiceProvider.class); + YangParserFactory yangParserFactory = mock(YangParserFactory.class); + BindingNormalizedNodeSerializer bindingNormalizedNodeSerializer = mock(BindingNormalizedNodeSerializer.class); + addCMHandleProvider.setDataBroker(dataBrokerNetconf); + addCMHandleProvider.setMountPointService(mountPointService); + addCMHandleProvider.setDomMountPointService(domMountPointService); + addCMHandleProvider.setNotificationPublishService(notificationPublishService); + addCMHandleProvider.setRpcProviderRegistry(rpcProviderRegistry); + addCMHandleProvider.setClusterSingletonService(clusterSingletonServiceProvider); + addCMHandleProvider.setYangParserFactory(yangParserFactory); + addCMHandleProvider.setBindingNormalizedNodeSerializer(bindingNormalizedNodeSerializer); + addCMHandleProvider.init(); + nodeIdList.add("ncserver1"); + LOG.info("Initialization done"); } - @After - public void tearDown() throws Exception { + @Test + public void initializationTest() { + + LOG.info("Verify init state"); + assertTrue("Devicemanager not initialized", addCMHandleProvider.isInitializationSuccessful()); + } + + @Test + public void sendNotificationToCpsTest() { + + LOG.info("Send notification to Cps test"); + try (MockedStatic utilities = Mockito.mockStatic(HttpRequester.class)) { + utilities.when(() -> HttpRequester.sendPostRequest(any(), any(), any())).thenReturn("Success"); + + assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success"); + } + + } + + @Test + public void sendNotificationToDmaapTest() { + + LOG.info("Send notification to Cps test"); + try (MockedStatic utilities = Mockito.mockStatic(HttpRequester.class)) { + utilities.when(() -> HttpRequester.sendPostRequest(any(), any(), any())).thenReturn("Success"); + + assertEquals(addCMHandleProvider.sendNotificationToCps(nodeIdList), "Success"); + } + + } + + @AfterClass + public static void after() throws InterruptedException, IOException { + LOG.info("Start shutdown"); + addCMHandleProvider.close(); + } } -- cgit 1.2.3-korg