Fix checkstyle issues in module sal-dom-broker
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / clustering / impl / LegacyEntityOwnershipServiceAdapterTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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 package org.opendaylight.controller.md.sal.dom.clustering.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.verify;
19
20 import com.google.common.base.Optional;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.clustering.CandidateAlreadyRegisteredException;
27 import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
28 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipChange;
30 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListener;
31 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipListenerRegistration;
32 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipChangeState;
33 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
34 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
35 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipCandidateRegistration;
36 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipChange;
37 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
38 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListenerRegistration;
39 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
40
41 /**
42  * Unit tests for PreBoronEntityOwnershipServiceAdapter.
43  *
44  * @author Thomas Pantelis
45  */
46 @Deprecated
47 public class LegacyEntityOwnershipServiceAdapterTest {
48     static Entity LEGACY_ENTITY = new Entity("foo", "bar");
49     static DOMEntity DOM_ENTITY = new DOMEntity("foo", LEGACY_ENTITY.getId());
50
51     @Mock
52     private DOMEntityOwnershipService mockDOMService;
53
54     private LegacyEntityOwnershipServiceAdapter adapter;
55
56     @Before
57     public void setup() {
58         MockitoAnnotations.initMocks(this);
59         adapter = new LegacyEntityOwnershipServiceAdapter(mockDOMService);
60     }
61
62     @Test
63     public void testRegisterCandidate() throws Exception {
64         DOMEntityOwnershipCandidateRegistration mockDOMReg = mock(DOMEntityOwnershipCandidateRegistration.class);
65         doNothing().when(mockDOMReg).close();
66         doReturn(mockDOMReg).when(mockDOMService).registerCandidate(DOM_ENTITY);
67
68         EntityOwnershipCandidateRegistration reg = adapter.registerCandidate(LEGACY_ENTITY);
69
70         assertNotNull("registerCandidate returned null", reg);
71         assertEquals("getInstance", LEGACY_ENTITY, reg.getInstance());
72
73         reg.close();
74         verify(mockDOMReg).close();
75     }
76
77     @Test(expected = CandidateAlreadyRegisteredException.class)
78     public void testAlreadyRegisteredCandidate() throws Exception {
79         doThrow(new org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException(DOM_ENTITY))
80                 .when(mockDOMService).registerCandidate(DOM_ENTITY);
81
82         adapter.registerCandidate(LEGACY_ENTITY);
83     }
84
85     @Test
86     public void testRegisterListener() {
87         DOMEntityOwnershipListenerRegistration mockDOMReg = mock(DOMEntityOwnershipListenerRegistration.class);
88         doNothing().when(mockDOMReg).close();
89         doReturn(mockDOMReg).when(mockDOMService)
90                 .registerListener(eq(DOM_ENTITY.getType()), any(DOMEntityOwnershipListener.class));
91         EntityOwnershipListener mockListener = mock(EntityOwnershipListener.class);
92         doNothing().when(mockListener).ownershipChanged(any(EntityOwnershipChange.class));
93
94         EntityOwnershipListenerRegistration reg = adapter.registerListener(LEGACY_ENTITY.getType(), mockListener);
95
96         assertNotNull("registerListener returned null", reg);
97         assertEquals("getInstance", mockListener, reg.getInstance());
98         assertEquals("getEntityType", LEGACY_ENTITY.getType(), reg.getEntityType());
99
100         ArgumentCaptor<DOMEntityOwnershipListener> domListenerCaptor = ArgumentCaptor
101                 .forClass(DOMEntityOwnershipListener.class);
102         verify(mockDOMService).registerListener(eq(DOM_ENTITY.getType()), domListenerCaptor.capture());
103
104         DOMEntityOwnershipChange domOwnershipChange = new DOMEntityOwnershipChange(DOM_ENTITY,
105                                                                                    EntityOwnershipChangeState
106                                                                                            .LOCAL_OWNERSHIP_GRANTED);
107         domListenerCaptor.getValue().ownershipChanged(domOwnershipChange);
108
109         ArgumentCaptor<EntityOwnershipChange> ownershipChangeCaptor = ArgumentCaptor
110                 .forClass(EntityOwnershipChange.class);
111         verify(mockListener).ownershipChanged(ownershipChangeCaptor.capture());
112
113         EntityOwnershipChange change = ownershipChangeCaptor.getValue();
114         assertEquals("getEntity", LEGACY_ENTITY, change.getEntity());
115         assertEquals("wasOwner", false, change.wasOwner());
116         assertEquals("isOwner", true, change.isOwner());
117         assertEquals("hasOwner", true, change.hasOwner());
118
119         reg.close();
120         verify(mockDOMReg).close();
121     }
122
123     @Test
124     public void testGetOwnershipState() {
125         testGetOwnershipState(EntityOwnershipState.IS_OWNER, true, true);
126         testGetOwnershipState(EntityOwnershipState.OWNED_BY_OTHER, false, true);
127         testGetOwnershipState(EntityOwnershipState.NO_OWNER, false, false);
128
129         doReturn(Optional.absent()).when(mockDOMService).getOwnershipState(DOM_ENTITY);
130         assertEquals("isPresent", false, adapter.getOwnershipState(LEGACY_ENTITY).isPresent());
131     }
132
133     private void testGetOwnershipState(EntityOwnershipState state, boolean expIsOwner, boolean expHasOwner) {
134         doReturn(Optional.of(state)).when(mockDOMService).getOwnershipState(DOM_ENTITY);
135
136         Optional<org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipState> actualState = adapter
137                 .getOwnershipState(LEGACY_ENTITY);
138
139         assertEquals("isPresent", true, actualState.isPresent());
140         assertEquals("isOwner", expIsOwner, actualState.get().isOwner());
141         assertEquals("hasOwner", expHasOwner, actualState.get().hasOwner());
142
143     }
144
145     @Test
146     public void testIsCandidateRegistered() {
147         doReturn(true).when(mockDOMService).isCandidateRegistered(DOM_ENTITY);
148         assertEquals("isCandidateRegistered", true, adapter.isCandidateRegistered(LEGACY_ENTITY));
149     }
150 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.