33cf71c4a4e51d888cc0c42447ffd60ee1ea3977
[mdsal.git] / binding / mdsal-binding-dom-adapter / src / test / java / org / opendaylight / mdsal / binding / dom / adapter / BindingDOMDataTreeProducerAdapterTest.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 package org.opendaylight.mdsal.binding.dom.adapter;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.doThrow;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.reset;
16 import static org.mockito.Mockito.verify;
17
18 import com.google.common.collect.ImmutableSet;
19 import com.google.common.util.concurrent.MoreExecutors;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.mdsal.binding.api.DataTreeProducerException;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingBrokerTestFactory;
27 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingTestContext;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
31 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
32
33 @RunWith(MockitoJUnitRunner.StrictStubs.class)
34 public class BindingDOMDataTreeProducerAdapterTest {
35     private BindingDOMDataTreeProducerAdapter bindingDOMDataTreeProducerAdapter;
36     private AdapterContext codec;
37
38     @Mock
39     private DOMDataTreeProducer delegate;
40
41     @Before
42     public void setUp() {
43         final BindingBrokerTestFactory testFactory = new BindingBrokerTestFactory();
44         testFactory.setExecutor(MoreExecutors.newDirectExecutorService());
45         final BindingTestContext testContext = testFactory.getTestContext();
46         testContext.start();
47         codec = testContext.getCodec();
48         bindingDOMDataTreeProducerAdapter = new BindingDOMDataTreeProducerAdapter(codec, delegate);
49     }
50
51     @Test
52     public void createTransactionTest() {
53         doReturn(mock(DOMDataTreeCursorAwareTransaction.class)).when(delegate).createTransaction(true);
54         assertNotNull(bindingDOMDataTreeProducerAdapter.createTransaction(true));
55         verify(delegate).createTransaction(true);
56     }
57
58     @Test
59     public void createProducerTest() {
60         doReturn(mock(DOMDataTreeProducer.class)).when(delegate).createProducer(any());
61         assertNotNull(bindingDOMDataTreeProducerAdapter.createProducer(ImmutableSet.of()));
62         verify(delegate).createProducer(any());
63     }
64
65     @Test
66     public void closeTest() throws DataTreeProducerException, DOMDataTreeProducerException {
67         reset(delegate);
68         bindingDOMDataTreeProducerAdapter.close();
69         verify(delegate).close();
70     }
71
72     @Test(expected = DataTreeProducerException.class)
73     public void closeTestWithException1() throws DataTreeProducerException, DOMDataTreeProducerException {
74         doThrow(new DOMDataTreeProducerBusyException("test")).when(delegate).close();
75         bindingDOMDataTreeProducerAdapter.close();
76     }
77
78     @Test(expected = DataTreeProducerException.class)
79     public void closeTestWithException2() throws DataTreeProducerException, DOMDataTreeProducerException {
80         doThrow(new DOMDataTreeProducerException("test")).when(delegate).close();
81         bindingDOMDataTreeProducerAdapter.close();
82     }
83 }