Bug 8910 - Binding v2 generator exception: Failed to find leafref target
[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
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;
24
25 /**
26  * Testing {@link ClusterSingletonServiceRegistrationDelegator}.
27  */
28 public class ClusterSingletonServiceRegistrationDelegatorTest {
29
30     private static final String SERVICE_IDENTIFIER_NAME = "TestServiceIdent";
31     private static final ServiceGroupIdentifier SERVICE_IDENTIFIER = ServiceGroupIdentifier
32             .create(SERVICE_IDENTIFIER_NAME);
33
34     @Mock
35     private ClusterSingletonServiceGroup<?, ?, ?> mockClusterSingletonServiceGroup;
36     @Mock
37     private ClusterSingletonService mockClusterSingletonService;
38
39     private ClusterSingletonServiceRegistrationDelegator delegator;
40
41     /**
42      * Initialization functionality for every Tests in this suite.
43      *
44      * @throws Exception - unexpected setup exception
45      */
46     @Before
47     public void setup() throws Exception {
48         MockitoAnnotations.initMocks(this);
49
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);
57     }
58
59     /**
60      * Test create input with {@link ClusterSingletonService} as null.
61      */
62     @Test(expected = NullPointerException.class)
63     public void testSetupNullService() {
64         delegator = new ClusterSingletonServiceRegistrationDelegator(null, mockClusterSingletonServiceGroup);
65     }
66
67     /**
68      * Test create input with {@link ClusterSingletonServiceGroupImpl} as null.
69      */
70     @Test(expected = NullPointerException.class)
71     public void testSetupNullGroup() {
72         delegator = new ClusterSingletonServiceRegistrationDelegator(mockClusterSingletonService, null);
73     }
74
75     /**
76      * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}.
77      */
78     @Test
79     public void testInstatiateServiceDelegMethod() {
80         delegator.instantiateServiceInstance();
81         verify(mockClusterSingletonService).instantiateServiceInstance();
82     }
83
84     /**
85      * Test a method delegation {@link ClusterSingletonService#instantiateServiceInstance()}.
86      */
87     @Test
88     public void testCloseServiceDelegMethod() {
89         delegator.closeServiceInstance();
90         verify(mockClusterSingletonService).closeServiceInstance();
91     }
92
93     /**
94      * Test a method delegation {@link ClusterSingletonService#getIdentifier()}.
95      */
96     @Test
97     public void testGetServiceIdentifierDelegMethod() {
98         final String serviceIdentifier = delegator.getServiceGroupIdentifier();
99         Assert.assertEquals(SERVICE_IDENTIFIER_NAME, serviceIdentifier);
100         verify(mockClusterSingletonService).getIdentifier();
101     }
102
103     /**
104      * Test a close method delegation to
105      *     {@link ClusterSingletonServiceGroupImpl#unregisterService(ClusterSingletonService)}.
106      *
107      * @throws Exception is from AutoclosableInterface
108      */
109     @Test
110     public void testCloseMethod() throws Exception {
111         delegator.close();
112         verify(mockClusterSingletonServiceGroup).unregisterService(delegator);
113     }
114 }