2 * Copyright (c) 2015 Pantheon Technologies s.r.o. 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.groupbasedpolicy.renderer.ofoverlay.endpoint;
10 import static org.mockito.Matchers.any;
11 import static org.mockito.Matchers.eq;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
17 import java.util.Collection;
18 import java.util.Collections;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.ArgumentCaptor;
22 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
23 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
24 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
25 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
26 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.groupbasedpolicy.util.IidFactory;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.Endpoint;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.endpoint.rev140421.endpoints.EndpointL3;
31 import org.opendaylight.yangtools.concepts.ListenerRegistration;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
34 public class EndpointManagerListenerTest {
36 private EndpointManagerListener endpointManagerListener;
37 private EndpointManager endpointManager;
38 private DataBroker mockBroker;
40 @SuppressWarnings("unchecked")
43 endpointManager = mock(EndpointManager.class);
44 mockBroker = mock(DataBroker.class);
45 endpointManagerListener = new EndpointManagerListener(mockBroker, endpointManager);
47 doReturn(mock(ListenerRegistration.class)).when(mockBroker).registerDataTreeChangeListener(
48 any(DataTreeIdentifier.class), any(DataTreeChangeListener.class));
51 @SuppressWarnings({ "unchecked", "rawtypes" })
53 public void testEndpointCreated() {
54 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
55 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
56 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class))),
57 dtclCaptor.capture());
59 Endpoint endpoint = mock(Endpoint.class);
61 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(null, endpoint,
62 DataObjectModification.ModificationType.WRITE));
64 verify(endpointManager).processEndpoint(null, endpoint);
65 verify(endpointManager, never()).processL3Endpoint(any(EndpointL3.class), any(EndpointL3.class));
68 @SuppressWarnings({ "unchecked", "rawtypes" })
70 public void testEndpointUpdated() {
71 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
72 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
73 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class))),
74 dtclCaptor.capture());
76 Endpoint endpoint = mock(Endpoint.class);
78 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(endpoint, endpoint,
79 DataObjectModification.ModificationType.WRITE));
81 verify(endpointManager).processEndpoint(endpoint, endpoint);
82 verify(endpointManager, never()).processL3Endpoint(any(EndpointL3.class), any(EndpointL3.class));
85 @SuppressWarnings({ "unchecked", "rawtypes" })
87 public void testEndpointDeleted() {
88 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
89 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
90 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(Endpoint.class))),
91 dtclCaptor.capture());
93 Endpoint endpoint = mock(Endpoint.class);
95 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(endpoint, null,
96 DataObjectModification.ModificationType.DELETE));
98 verify(endpointManager).processEndpoint(endpoint, null);
99 verify(endpointManager, never()).processL3Endpoint(any(EndpointL3.class), any(EndpointL3.class));
102 @SuppressWarnings({ "unchecked", "rawtypes" })
104 public void testEndpointL3Created() {
105 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
106 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
107 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(EndpointL3.class))),
108 dtclCaptor.capture());
110 EndpointL3 endpoint = mock(EndpointL3.class);
112 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(null, endpoint,
113 DataObjectModification.ModificationType.WRITE));
115 verify(endpointManager).processL3Endpoint(null, endpoint);
116 verify(endpointManager, never()).processEndpoint(any(Endpoint.class), any(Endpoint.class));
119 @SuppressWarnings({ "unchecked", "rawtypes" })
121 public void testEndpointL3Updated() {
122 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
123 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
124 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(EndpointL3.class))),
125 dtclCaptor.capture());
127 EndpointL3 endpoint = mock(EndpointL3.class);
129 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(endpoint, endpoint,
130 DataObjectModification.ModificationType.WRITE));
132 verify(endpointManager).processL3Endpoint(endpoint, endpoint);
133 verify(endpointManager, never()).processEndpoint(any(Endpoint.class), any(Endpoint.class));
136 @SuppressWarnings({ "unchecked", "rawtypes" })
138 public void testEndpointL3Deleted() {
139 ArgumentCaptor<DataTreeChangeListener> dtclCaptor = ArgumentCaptor.forClass(DataTreeChangeListener.class);
140 verify(mockBroker).registerDataTreeChangeListener(eq(new DataTreeIdentifier<>(
141 LogicalDatastoreType.OPERATIONAL, IidFactory.endpointsIidWildcard().child(EndpointL3.class))),
142 dtclCaptor.capture());
144 EndpointL3 endpoint = mock(EndpointL3.class);
146 dtclCaptor.getValue().onDataTreeChanged(newMockDataTreeModification(endpoint, null,
147 DataObjectModification.ModificationType.DELETE));
149 verify(endpointManager).processL3Endpoint(endpoint, null);
150 verify(endpointManager, never()).processEndpoint(any(Endpoint.class), any(Endpoint.class));
153 @SuppressWarnings("unchecked")
154 private static <T extends DataObject> Collection<DataTreeModification<T>> newMockDataTreeModification(T dataBefore,
155 T dataAfter, DataObjectModification.ModificationType type) {
156 DataTreeModification<T> mockDataTreeModification = mock(DataTreeModification.class);
157 DataObjectModification<T> mockModification = mock(DataObjectModification.class);
158 doReturn(type).when(mockModification).getModificationType();
159 doReturn(dataBefore).when(mockModification).getDataBefore();
160 doReturn(dataAfter).when(mockModification).getDataAfter();
161 doReturn(mockModification).when(mockDataTreeModification).getRootNode();
163 return Collections.singletonList(mockDataTreeModification);