Code clean up
[bgpcep.git] / testtool-util / src / test / java / org / opendaylight / protocol / util / CheckUtilTest.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.protocol.util;
9
10 import static junit.framework.TestCase.assertNull;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doAnswer;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.when;
17 import static org.opendaylight.protocol.util.CheckUtil.checkEquals;
18 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentConfiguration;
19 import static org.opendaylight.protocol.util.CheckUtil.checkNotPresentOperational;
20 import static org.opendaylight.protocol.util.CheckUtil.checkPresentConfiguration;
21 import static org.opendaylight.protocol.util.CheckUtil.checkPresentOperational;
22 import static org.opendaylight.protocol.util.CheckUtil.checkReceivedMessages;
23 import static org.opendaylight.protocol.util.CheckUtil.readDataConfiguration;
24 import static org.opendaylight.protocol.util.CheckUtil.readDataOperational;
25 import static org.opendaylight.protocol.util.CheckUtil.waitFutureSuccess;
26
27 import com.google.common.base.VerifyException;
28 import io.netty.channel.ChannelFuture;
29 import io.netty.util.concurrent.GenericFutureListener;
30 import java.util.concurrent.ExecutionException;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
36 import org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest;
37 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
38 import org.opendaylight.protocol.util.CheckUtil.ListenerCheck;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TopologyId;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyBuilder;
43 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
46
47 public class CheckUtilTest extends AbstractConcurrentDataBrokerTest {
48     private static final TopologyId TOPOLOGY_ID = new TopologyId("topotest");
49     private final KeyedInstanceIdentifier<Topology, TopologyKey> topologyIIdKeyed =
50             InstanceIdentifier.create(NetworkTopology.class).child(Topology.class,
51                     new TopologyKey(TOPOLOGY_ID));
52     private static final int TIMEOUT = 1;
53     @Mock
54     private ListenerCheck listenerCheck;
55     @Mock
56     private ChannelFuture future;
57
58     @Before
59     public void setUp() throws Exception {
60         MockitoAnnotations.initMocks(this);
61     }
62
63     @Test(expected = VerifyException.class)
64     public void testWaitFutureSuccessFail() {
65         when(this.future.isDone()).thenReturn(false);
66         doReturn(this.future).when(this.future).addListener(any());
67         waitFutureSuccess(this.future);
68     }
69
70     @Test
71     public void testWaitFutureSuccess() {
72         when(this.future.isSuccess()).thenReturn(true);
73         doAnswer(invocation -> {
74             invocation.getArgumentAt(0, GenericFutureListener.class).operationComplete(CheckUtilTest.this.future);
75             return CheckUtilTest.this.future;
76         }).when(this.future).addListener(any());
77         waitFutureSuccess(this.future);
78     }
79
80     @Test(expected = NullPointerException.class)
81     public void testReadDataOperationalNull() throws Exception {
82         readDataOperational(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
83     }
84
85     @Test(expected = NullPointerException.class)
86     public void testReadDataConfigurationNull() throws Exception {
87         readDataConfiguration(getDataBroker(), topologyIIdKeyed, test -> false, TIMEOUT);
88     }
89
90     @Test(expected = AssertionError.class)
91     public void testReadDataOperationalFail() throws Exception {
92         storeTopo(LogicalDatastoreType.OPERATIONAL);
93         readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
94             assertNotNull(result.getNode());
95             return result;
96         }, TIMEOUT);
97     }
98
99     @Test(expected = AssertionError.class)
100     public void testReadDataConfigurationFail() throws Exception {
101         storeTopo(LogicalDatastoreType.CONFIGURATION);
102         readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
103             assertNotNull(result.getNode());
104             return result;
105         }, TIMEOUT);
106     }
107
108     @Test
109     public void testReadDataOperational() throws Exception {
110         storeTopo(LogicalDatastoreType.OPERATIONAL);
111         readDataOperational(getDataBroker(), this.topologyIIdKeyed, result -> {
112             assertNull(result.getNode());
113             return result;
114         }, TIMEOUT);
115     }
116
117     @Test
118     public void testReadDataConfiguration() throws Exception {
119         storeTopo(LogicalDatastoreType.CONFIGURATION);
120         readDataConfiguration(getDataBroker(), this.topologyIIdKeyed, result -> {
121             assertNull(result.getNode());
122             return result;
123         }, TIMEOUT);
124     }
125
126     private void storeTopo(final LogicalDatastoreType dsType) throws ExecutionException, InterruptedException {
127         final WriteTransaction wt = getDataBroker().newWriteOnlyTransaction();
128         wt.put(dsType, this.topologyIIdKeyed,
129                 new TopologyBuilder()
130                         .setTopologyId(TOPOLOGY_ID)
131                         .build(), true);
132         wt.commit().get();
133     }
134
135     @Test
136     public void testCheckPresentConfiguration() throws Exception {
137         storeTopo(LogicalDatastoreType.CONFIGURATION);
138         checkPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
139     }
140
141     @Test
142     public void testCheckPresentOperational() throws Exception {
143         storeTopo(LogicalDatastoreType.OPERATIONAL);
144         checkPresentOperational(getDataBroker(), this.topologyIIdKeyed);
145     }
146
147     @Test(expected = AssertionError.class)
148     public void testCheckNotPresentOperationalFail() throws Exception {
149         storeTopo(LogicalDatastoreType.OPERATIONAL);
150         checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
151     }
152
153     @Test
154     public void testCheckNotPresentOperational() throws Exception {
155         checkNotPresentOperational(getDataBroker(), this.topologyIIdKeyed);
156     }
157
158     @Test
159     public void testCheckNotPresentConfiguration() throws Exception {
160         checkNotPresentConfiguration(getDataBroker(), this.topologyIIdKeyed);
161     }
162
163     @Test(expected = AssertionError.class)
164     public void testCheckEquals() throws Exception {
165         checkEquals(() -> fail(), TIMEOUT);
166     }
167
168     @Test(expected = AssertionError.class)
169     public void testCheckReceivedMessagesNotEqual() {
170         doReturn(0).when(this.listenerCheck).getListMessageSize();
171         checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
172     }
173
174     @Test
175     public void testCheckReceivedMessagesEqual() {
176         doReturn(1).when(this.listenerCheck).getListMessageSize();
177         checkReceivedMessages(this.listenerCheck, 1, TIMEOUT);
178     }
179 }