2 * Copyright (c) 2017 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.protocol.util;
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
16 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
17 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentConfiguration;
18 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
19 import static org.opendaylight.protocol.util.CheckUtil.checkPresentConfiguration;
20 import static org.opendaylight.protocol.util.CheckUtil.checkPresentOperational;
21 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
22 import static org.opendaylight.protocol.util.CheckUtil.readDataConfiguration;
23 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
24 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
26 import com.google.common.base.VerifyException;
27 import io.netty.channel.ChannelFuture;
28 import io.netty.util.concurrent.GenericFutureListener;
29 import java.util.concurrent.ExecutionException;
30 import java.util.concurrent.TimeUnit;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.opendaylight.mdsal.binding.api.WriteTransaction;
37 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractConcurrentDataBrokerTest;
38 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
39 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
44 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
45 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
46 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
48 public class CheckUtilTest extends AbstractConcurrentDataBrokerTest {
49 private static final TopologyId TOPOLOGY_ID = new TopologyId("topotest");
50 private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
51 InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
52 new TopologyKey(TOPOLOGY_ID));
53 private static final int TIMEOUT = 1;
55 private ListenerCheck listenerCheck;
57 private ChannelFuture future;
60 public void setUp() throws Exception {
61 MockitoAnnotations.initMocks(this);
64 @Test(expected = VerifyException.class)
65 public void testWaitFutureSuccessFail() {
66 when(this.future.isDone()).thenReturn(false);
67 doReturn(this.future).when(this.future).addListener(any());
68 waitFutureSuccess(this.future, 10L, TimeUnit.MILLISECONDS);
72 public void testWaitFutureSuccess() {
73 when(this.future.isSuccess()).thenReturn(true);
74 doAnswer(invocation -> {
75 ((GenericFutureListener)invocation.getArgument(0)).operationComplete(CheckUtilTest.this.future);
76 return CheckUtilTest.this.future;
77 }).when(this.future).addListener(any());
78 waitFutureSuccess(this.future);
81 @Test(expected = NullPointerException.class)
82 public void testReadDataOperationalNull() throws Exception {
83 readDataOperational(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
86 @Test(expected = NullPointerException.class)
87 public void testReadDataConfigurationNull() throws Exception {
88 readDataConfiguration(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
91 @Test(expected = AssertionError.class)
92 public void testReadDataOperationalFail() throws Exception {
93 storeTopo(LogicalDatastoreType.OPERATIONAL);
94 readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
95 assertNotNull(result.getNode());
100 @Test(expected = AssertionError.class)
101 public void testReadDataConfigurationFail() throws Exception {
102 storeTopo(LogicalDatastoreType.CONFIGURATION);
103 readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
104 assertNotNull(result.getNode());
110 public void testReadDataOperational() throws Exception {
111 storeTopo(LogicalDatastoreType.OPERATIONAL);
112 readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
113 assertNull(result.getNode());
119 public void testReadDataConfiguration() throws Exception {
120 storeTopo(LogicalDatastoreType.CONFIGURATION);
121 readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
122 assertNull(result.getNode());
127 private void storeTopo(final LogicalDatastoreType dsType) throws ExecutionException, InterruptedException {
128 final WriteTransaction wt = getDataBroker().newWriteOnlyTransaction();
129 wt.mergeParentStructurePut(dsType, this.topologyIIdKeyed,
130 new TopologyBuilder()
131 .setTopologyId(TOPOLOGY_ID)
137 public void testCheckPresentConfiguration() throws Exception {
138 storeTopo(LogicalDatastoreType.CONFIGURATION);
139 checkPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
143 public void testCheckPresentOperational() throws Exception {
144 storeTopo(LogicalDatastoreType.OPERATIONAL);
145 checkPresentOperational(getDataBroker(), this.topologyIIdKeyed);
148 @Test(expected = AssertionError.class)
149 public void testCheckNotPresentOperationalFail() throws Exception {
150 storeTopo(LogicalDatastoreType.OPERATIONAL);
151 checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
155 public void testCheckNotPresentOperational() throws Exception {
156 checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
160 public void testCheckNotPresentConfiguration() throws Exception {
161 checkNotPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
164 @Test(expected = AssertionError.class)
165 public void testCheckEquals() throws Exception {
166 checkEquals(Assert::fail, TIMEOUT);
169 @Test(expected = AssertionError.class)
170 public void testCheckReceivedMessagesNotEqual() {
171 doReturn(0).when(this.listenerCheck).getListMessageSize();
172 checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
176 public void testCheckReceivedMessagesEqual() {
177 doReturn(1).when(this.listenerCheck).getListMessageSize();
178 checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);