From 0b673b8375cb14bb7c41ab4bf8f92edb3b992f55 Mon Sep 17 00:00:00 2001 From: Andrej Mak Date: Wed, 3 Feb 2016 13:44:27 +0100 Subject: [PATCH] Add AggregatedNetconfOperationServiceFactory unit test. Change-Id: I59f89e6977119c2c45b16544252729a12052bbef Signed-off-by: Andrej Mak --- ...tedNetconfOperationServiceFactoryTest.java | 120 ++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java diff --git a/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java new file mode 100644 index 0000000000..400f4b6010 --- /dev/null +++ b/opendaylight/netconf/netconf-impl/src/test/java/org/opendaylight/netconf/impl/osgi/AggregatedNetconfOperationServiceFactoryTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.netconf.impl.osgi; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.collect.Sets; +import java.util.HashSet; +import java.util.Set; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.opendaylight.controller.config.util.capability.BasicCapability; +import org.opendaylight.controller.config.util.capability.Capability; +import org.opendaylight.netconf.api.monitoring.CapabilityListener; +import org.opendaylight.netconf.mapping.api.NetconfOperationServiceFactory; + + +public class AggregatedNetconfOperationServiceFactoryTest { + + private Set factory1Caps = new HashSet<>(); + private Set factory2Caps = new HashSet<>(); + + private final CapabilityListener listener1 = mock(CapabilityListener.class); + private final CapabilityListener listener2 = mock(CapabilityListener.class); + private final NetconfOperationServiceFactory factory1 = getMock(NetconfOperationServiceFactory.class);; + private final NetconfOperationServiceFactory factory2 = getMock(NetconfOperationServiceFactory.class);; + private final AutoCloseable autoCloseable1 = getMock(AutoCloseable.class); + private final AutoCloseable autoCloseable2 = getMock(AutoCloseable.class); + + private AggregatedNetconfOperationServiceFactory aggregatedFactory; + + @Before + public void setUp() throws Exception { + factory1Caps.add(new BasicCapability("AAA")); + factory1Caps.add(new BasicCapability("BBB")); + + factory2Caps.add(new BasicCapability("CCC")); + factory2Caps.add(new BasicCapability("DDD")); + + aggregatedFactory = new AggregatedNetconfOperationServiceFactory(); + + aggregatedFactory.registerCapabilityListener(listener1); + aggregatedFactory.registerCapabilityListener(listener2); + + when(factory1.registerCapabilityListener(listener1)).thenReturn(autoCloseable1); + when(factory1.registerCapabilityListener(listener2)).thenReturn(autoCloseable2); + when(factory1.getCapabilities()).thenReturn(factory1Caps); + + when(factory2.registerCapabilityListener(listener1)).thenReturn(autoCloseable1); + when(factory2.registerCapabilityListener(listener2)).thenReturn(autoCloseable2); + when(factory2.getCapabilities()).thenReturn(factory2Caps); + + } + + @Test + public void testOnAddAndOnRemove() throws Exception { + aggregatedFactory.onAddNetconfOperationServiceFactory(factory1); + aggregatedFactory.onAddNetconfOperationServiceFactory(factory2); + + verify(factory1).registerCapabilityListener(listener1); + verify(factory2).registerCapabilityListener(listener1); + verify(factory1).registerCapabilityListener(listener2); + verify(factory2).registerCapabilityListener(listener2); + + aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory1); + aggregatedFactory.onRemoveNetconfOperationServiceFactory(factory2); + + verify(autoCloseable1, times(2)).close(); + verify(autoCloseable2, times(2)).close(); + } + + @Test + public void testGetCapabilities() throws Exception { + aggregatedFactory.onAddNetconfOperationServiceFactory(factory1); + aggregatedFactory.onAddNetconfOperationServiceFactory(factory2); + final Set actual = aggregatedFactory.getCapabilities(); + Set expected = Sets.union(factory1Caps, factory2Caps); + Assert.assertEquals(expected, actual); + } + + @Test + public void testRegisterCapabilityListener() throws Exception { + aggregatedFactory.onAddNetconfOperationServiceFactory(factory1); + aggregatedFactory.onAddNetconfOperationServiceFactory(factory2); + CapabilityListener listener = mock(CapabilityListener.class); + aggregatedFactory.registerCapabilityListener(listener); + + verify(factory1).registerCapabilityListener(listener); + verify(factory2).registerCapabilityListener(listener); + } + + @Test + public void testClose() throws Exception { + aggregatedFactory.onAddNetconfOperationServiceFactory(factory1); + aggregatedFactory.onAddNetconfOperationServiceFactory(factory2); + aggregatedFactory.close(); + verify(autoCloseable1, times(2)).close(); + verify(autoCloseable2, times(2)).close(); + } + + private T getMock(Class cls) { + return mock(cls, new Answer() { + @Override + public T answer(InvocationOnMock invocation) throws Throwable { + return null; + } + }); + } +} \ No newline at end of file -- 2.36.6