2 * Copyright (c) 2017 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
8 package org.opendaylight.mdsal.eos.dom.simple;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertFalse;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import static org.junit.jupiter.api.Assertions.assertTrue;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.mock;
18 import java.util.Optional;
19 import java.util.UUID;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.mockito.junit.jupiter.MockitoExtension;
23 import org.opendaylight.mdsal.eos.common.api.CandidateAlreadyRegisteredException;
24 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipState;
25 import org.opendaylight.mdsal.eos.common.api.EntityOwnershipStateChange;
26 import org.opendaylight.mdsal.eos.dom.api.DOMEntity;
27 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipListener;
28 import org.opendaylight.mdsal.eos.dom.api.DOMEntityOwnershipService;
30 @ExtendWith(MockitoExtension.class)
31 class SimpleDOMEntityOwnershipServiceTest {
32 private static final String FOO_TYPE = "foo";
33 private static final String BAR_TYPE = "bar";
34 private static final DOMEntity FOO_FOO_ENTITY = new DOMEntity(FOO_TYPE, "foo");
35 private static final DOMEntity FOO_BAR_ENTITY = new DOMEntity(FOO_TYPE, "bar");
37 final DOMEntityOwnershipService service = new SimpleDOMEntityOwnershipService();
40 void testNonExistingEntity() {
41 assertFalse(service.isCandidateRegistered(FOO_FOO_ENTITY));
42 assertEquals(Optional.empty(), service.getOwnershipState(FOO_FOO_ENTITY));
46 void testExistingEntity() throws Exception {
47 try (var reg = service.registerCandidate(FOO_FOO_ENTITY)) {
50 assertTrue(service.isCandidateRegistered(FOO_FOO_ENTITY));
51 assertFalse(service.isCandidateRegistered(FOO_BAR_ENTITY));
53 assertEquals(Optional.of(EntityOwnershipState.IS_OWNER), service.getOwnershipState(FOO_FOO_ENTITY));
55 assertFalse(service.isCandidateRegistered(FOO_FOO_ENTITY));
59 void testDuplicateRegistration() throws Exception {
60 assertNotNull(service.registerCandidate(FOO_FOO_ENTITY));
63 assertThrows(CandidateAlreadyRegisteredException.class, () -> service.registerCandidate(FOO_FOO_ENTITY));
67 void testListener() throws Exception {
68 final var entityReg = service.registerCandidate(FOO_FOO_ENTITY);
69 assertNotNull(entityReg);
71 // Mismatched type, not triggered
72 final var barListener = mock(DOMEntityOwnershipListener.class);
73 try (var barReg = service.registerListener(BAR_TYPE, barListener)) {
74 // Matching type should be triggered
75 final var fooListener = mock(DOMEntityOwnershipListener.class);
76 doNothing().when(fooListener)
77 .ownershipChanged(FOO_FOO_ENTITY, EntityOwnershipStateChange.LOCAL_OWNERSHIP_GRANTED, true);
78 try (var fooReg = service.registerListener(FOO_TYPE, fooListener)) {
79 doNothing().when(fooListener)
80 .ownershipChanged(FOO_FOO_ENTITY, EntityOwnershipStateChange.LOCAL_OWNERSHIP_LOST_NO_OWNER, false);
87 void testToString() throws Exception {
88 final var uuid = UUID.randomUUID();
89 final var expected = String.format("SimpleDOMEntityOwnershipService{uuid=%s, entities={}, listeners={}}", uuid);
90 assertEquals(expected, new SimpleDOMEntityOwnershipService(uuid).toString());