2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.mdsal.singleton.dom.impl;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.verify;
16 import com.google.common.util.concurrent.Futures;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.mockito.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
23 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
26 * Testing {@link ClusterSingletonServiceRegistrationDelegator}.
28 public class ClusterSingletonServiceRegistrationDelegatorTest {
30 private static final String SERVICE_IDENTIFIER_NAME = "TestServiceIdent";
31 private static final ServiceGroupIdentifier SERVICE_IDENTIFIER = ServiceGroupIdentifier
32 .create(SERVICE_IDENTIFIER_NAME);
35 private ClusterSingletonServiceGroup<?, ?, ?> mockClusterSingletonServiceGroup;
37 private ClusterSingletonService mockClusterSingletonService;
39 private ClusterSingletonServiceRegistrationDelegator delegator;
42 * Initialization functionality for every Tests in this suite.
44 * @throws Exception - unexpected setup exception
47 public void setup() throws Exception {
48 MockitoAnnotations.initMocks(this);
50 doNothing().when(mockClusterSingletonServiceGroup)
51 .unregisterService(any(ClusterSingletonServiceRegistrationDelegator.class));
52 doReturn(SERVICE_IDENTIFIER).when(mockClusterSingletonService).getIdentifier();
53 doNothing().when(mockClusterSingletonService).instantiateServiceInstance();
54 doReturn(Futures.immediateFuture(null)).when(mockClusterSingletonService).closeServiceInstance();
55 delegator = new ClusterSingletonServiceRegistrationDelegator(mockClusterSingletonService,
56 mockClusterSingletonServiceGroup);
60 * Test create input with {@link ClusterSingletonService} as null.
62 @Test(expected = NullPointerException.class)
63 public void testSetupNullService() {
64 delegator = new ClusterSingletonServiceRegistrationDelegator(null, mockClusterSingletonServiceGroup);
68 * Test create input with {@link ClusterSingletonServiceGroupImpl} as null.
70 @Test(expected = NullPointerException.class)
71 public void testSetupNullGroup() {
72 delegator = new ClusterSingletonServiceRegistrationDelegator(mockClusterSingletonService, null);
76 * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}.
79 public void testInstatiateServiceDelegMethod() {
80 delegator.instantiateServiceInstance();
81 verify(mockClusterSingletonService).instantiateServiceInstance();
85 * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}.
88 public void testCloseServiceDelegMethod() {
89 delegator.closeServiceInstance();
90 verify(mockClusterSingletonService).closeServiceInstance();
94 * Test a method delegation {@link ClusterSingletonService#getIdentifier()}.
97 public void testGetServiceIdentifierDelegMethod() {
98 final String serviceIdentifier = delegator.getServiceGroupIdentifier();
99 Assert.assertEquals(SERVICE_IDENTIFIER_NAME, serviceIdentifier);
100 verify(mockClusterSingletonService).getIdentifier();
104 * Test a close method delegation to
105 * {@link ClusterSingletonServiceGroupImpl#unregisterService(ClusterSingletonService)}.
107 * @throws Exception is from AutoclosableInterface
110 public void testCloseMethod() throws Exception {
112 verify(mockClusterSingletonServiceGroup).unregisterService(delegator);