2 * Copyright (c) 2016 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
9 package org.opendaylight.groupbasedpolicy.renderer.ios_xe_provider.impl.util;
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;
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;
35 * Test for {@link NodeWriter}.
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest({DataStoreHelper.class})
39 public class NodeWriterTest {
41 private DataBroker dataBroker;
42 private NodeWriter nodeWriter;
43 private RendererNode rendererNode;
44 private WriteTransaction transaction;
45 private CheckedFuture<Void, TransactionCommitFailedException> future;
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);
58 public void commitToDs_emptyCache() {
59 final ListenableFuture<Boolean> result = nodeWriter.commitToDatastore(dataBroker);
60 assertTrue(getFutureResult(result));
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));
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));
81 public void removeFromDs_emptyCache() {
82 nodeWriter.removeFromDatastore(dataBroker);
83 final ListenableFuture<Boolean> result = nodeWriter.removeFromDatastore(dataBroker);
84 assertTrue(getFutureResult(result));
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));
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));
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));
117 private boolean getFutureResult(final ListenableFuture<Boolean> future) {
120 } catch (InterruptedException | ExecutionException e) {