a7f8d614b018f884aada1b0aa0c736f3c1ae4b40
[groupbasedpolicy.git] / renderers / ios-xe / src / test / java / org / opendaylight / groupbasedpolicy / renderer / ios_xe_provider / impl / util / NodeWriterTest.java
1 /*
2  * Copyright (c) 2016 Cisco 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
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
10
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.powermock.api.support.membermodification.MemberMatcher.method;
16 import static org.powermock.api.support.membermodification.MemberModifier.stub;
17
18 import java.util.concurrent.ExecutionException;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
28 import org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.writer.NodeWriter;
29 import org.opendaylight.groupbasedpolicy.util.DataStoreHelper;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.groupbasedpolicy.renderer.rev151103.renderers.renderer.renderer.nodes.RendererNode;
31 import org.powermock.core.classloader.annotations.PrepareForTest;
32 import org.powermock.modules.junit4.PowerMockRunner;
33
34 /**
35  * Test for {@link NodeWriter}.
36  */
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest({DataStoreHelper.class})
39 public class NodeWriterTest {
40
41     private DataBroker dataBroker;
42     private NodeWriter nodeWriter;
43     private RendererNode rendererNode;
44     private WriteTransaction transaction;
45     private CheckedFuture<Void, TransactionCommitFailedException> future;
46
47     @Before
48     @SuppressWarnings("unchecked")
49     public void init() throws Exception {
50         nodeWriter = new NodeWriter();
51         dataBroker = mock(DataBroker.class);
52         rendererNode = mock(RendererNode.class);
53         transaction = mock(WriteTransaction.class);
54         future = mock(CheckedFuture.class);
55     }
56
57     @Test
58     public void commitToDs_emptyCache() {
59         final ListenableFuture<Boolean> result = nodeWriter.commitToDatastore(dataBroker);
60         assertTrue(getFutureResult(result));
61     }
62
63     @Test
64     public void commitToDs_failure() {
65         nodeWriter.cache(rendererNode);
66         stub(method(DataStoreHelper.class, "submitToDs")).toReturn(false);
67         final ListenableFuture<Boolean> result = nodeWriter.commitToDatastore(dataBroker);
68         assertFalse(getFutureResult(result));
69     }
70
71     @Test
72     public void commitToDs_success() {
73         nodeWriter.cache(rendererNode);
74         when(dataBroker.newWriteOnlyTransaction()).thenReturn(transaction);
75         stub(method(DataStoreHelper.class, "submitToDs")).toReturn(true);
76         final ListenableFuture<Boolean> result = nodeWriter.commitToDatastore(dataBroker);
77         assertTrue(getFutureResult(result));
78     }
79
80     @Test
81     public void removeFromDs_emptyCache() {
82         nodeWriter.removeFromDatastore(dataBroker);
83         final ListenableFuture<Boolean> result = nodeWriter.removeFromDatastore(dataBroker);
84         assertTrue(getFutureResult(result));
85     }
86
87     @Test
88     public void removeFromDs_transactionCommitException() throws Exception {
89         nodeWriter.cache(rendererNode);
90         when(dataBroker.newWriteOnlyTransaction()).thenReturn(transaction);
91         when(transaction.submit()).thenReturn(future);
92         when(future.checkedGet()).thenThrow(new TransactionCommitFailedException("exception"));
93         final ListenableFuture<Boolean> result = nodeWriter.removeFromDatastore(dataBroker);
94         assertFalse(getFutureResult(result));
95     }
96
97     @Test
98     public void removeFromDs_otherException() throws Exception {
99         nodeWriter.cache(rendererNode);
100         when(dataBroker.newWriteOnlyTransaction()).thenReturn(transaction);
101         when(transaction.submit()).thenReturn(future);
102         when(future.checkedGet()).thenThrow(new NullPointerException("exception"));
103         final ListenableFuture<Boolean> result = nodeWriter.removeFromDatastore(dataBroker);
104         assertFalse(getFutureResult(result));
105     }
106
107     @Test
108     public void removeFromDs_success() throws Exception {
109         nodeWriter.cache(rendererNode);
110         when(dataBroker.newWriteOnlyTransaction()).thenReturn(transaction);
111         when(transaction.submit()).thenReturn(future);
112         when(future.checkedGet()).thenReturn(null);
113         final ListenableFuture<Boolean> result = nodeWriter.removeFromDatastore(dataBroker);
114         assertTrue(getFutureResult(result));
115     }
116
117     private boolean getFutureResult(final ListenableFuture<Boolean> future) {
118         try {
119             return future.get();
120         } catch (InterruptedException | ExecutionException e) {
121             Assert.fail();
122             return false;
123         }
124     }
125 }