Fix outstanding issuess after moving code base
[mdsal.git] / singleton-service / mdsal-singleton-dom-impl / src / test / java / org / opendaylight / mdsal / singleton / dom / impl / ClusterSingletonServiceRegistrationDelegatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.singleton.dom.impl;
10
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;
15 import com.google.common.util.concurrent.Futures;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.mockito.Mock;
20 import org.mockito.MockitoAnnotations;
21 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonService;
22 import org.opendaylight.mdsal.singleton.common.api.ServiceGroupIdentifier;
23 import org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceGroup;
24 import org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceGroupImpl;
25 import org.opendaylight.mdsal.singleton.dom.impl.ClusterSingletonServiceRegistrationDelegator;
26
27 /**
28  * Testing {@link ClusterSingletonServiceRegistrationDelegator}
29  */
30 public class ClusterSingletonServiceRegistrationDelegatorTest {
31
32     private static final String SERVICE_IDENTIFIER_NAME = "TestServiceIdent";
33     private static final ServiceGroupIdentifier SERVICE_IDENTIFIER = ServiceGroupIdentifier
34             .create(SERVICE_IDENTIFIER_NAME);
35
36     @Mock
37     private ClusterSingletonServiceGroup<?, ?, ?> mockClusterSingletonServiceGroup;
38     @Mock
39     private ClusterSingletonService mockClusterSingletonService;
40
41     private ClusterSingletonServiceRegistrationDelegator delegator;
42
43     /**
44      * Initialization functionality for every Tests in this suite
45      *
46      * @throws Exception - unexpected setup exception
47      */
48     @Before
49     public void setup() throws Exception {
50         MockitoAnnotations.initMocks(this);
51
52         doNothing().when(mockClusterSingletonServiceGroup)
53                 .unregisterService(any(ClusterSingletonServiceRegistrationDelegator.class));
54         doReturn(SERVICE_IDENTIFIER).when(mockClusterSingletonService).getIdentifier();
55         doNothing().when(mockClusterSingletonService).instantiateServiceInstance();
56         doReturn(Futures.immediateFuture(null)).when(mockClusterSingletonService).closeServiceInstance();
57         delegator = new ClusterSingletonServiceRegistrationDelegator(mockClusterSingletonService,
58                 mockClusterSingletonServiceGroup);
59     }
60
61     /**
62      * Test create input with {@link ClusterSingletonService} as null
63      */
64     @Test(expected = NullPointerException.class)
65     public void testSetupNullService() {
66         delegator = new ClusterSingletonServiceRegistrationDelegator(null, mockClusterSingletonServiceGroup);
67     }
68
69     /**
70      * Test create input with {@link ClusterSingletonServiceGroupImpl} as null
71      */
72     @Test(expected = NullPointerException.class)
73     public void testSetupNullGroup() {
74         delegator = new ClusterSingletonServiceRegistrationDelegator(mockClusterSingletonService, null);
75     }
76
77     /**
78      * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}
79      */
80     @Test
81     public void testInstatiateServiceDelegMethod() {
82         delegator.instantiateServiceInstance();
83         verify(mockClusterSingletonService).instantiateServiceInstance();
84     }
85
86     /**
87      * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}
88      */
89     @Test
90     public void testCloseServiceDelegMethod() {
91         delegator.closeServiceInstance();
92         verify(mockClusterSingletonService).closeServiceInstance();
93     }
94
95     /**
96      * Test a method delegation {@link ClusterSingletonService#getIdentifier()}
97      */
98     @Test
99     public void testGetServiceIdentifierDelegMethod() {
100         final String serviceIdentifier = delegator.getServiceGroupIdentifier();
101         Assert.assertEquals(SERVICE_IDENTIFIER_NAME, serviceIdentifier);
102         verify(mockClusterSingletonService).getIdentifier();
103     }
104
105     /**
106      * Test a close method delegation to {@link ClusterSingletonServiceGroupImpl#unregisterService(ClusterSingletonService)}
107      *
108      * @throws Exception is from AutoclosableInterface
109      */
110     @Test
111     public void testCloseMethod() throws Exception {
112         delegator.close();
113         verify(mockClusterSingletonServiceGroup).unregisterService(delegator);
114     }
115 }