223cfb878937860393e524843a66cb49a96c0ad2
[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.Matchers.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 import static org.mockito.MockitoAnnotations.initMocks;
18
19 import com.google.common.collect.ImmutableSet;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.opendaylight.mdsal.binding.api.DataTreeProducerException;
25 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingBrokerTestFactory;
26 import org.opendaylight.mdsal.binding.dom.adapter.test.util.BindingTestContext;
27 import org.opendaylight.mdsal.dom.api.DOMDataTreeCursorAwareTransaction;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducer;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerBusyException;
30 import org.opendaylight.mdsal.dom.api.DOMDataTreeProducerException;
31
32 public class BindingDOMDataTreeProducerAdapterTest {
33
34     private BindingDOMDataTreeProducerAdapter bindingDOMDataTreeProducerAdapter;
35     private BindingToNormalizedNodeCodec codec;
36
37     @Mock
38     private DOMDataTreeProducer delegate;
39
40     @Before
41     public void setUp() {
42         initMocks(this);
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 createTest() {
60         assertNotNull(BindingDOMDataTreeProducerAdapter.create(delegate, codec));
61     }
62
63     @Test
64     public void createProducerTest() {
65         doReturn(mock(DOMDataTreeProducer.class)).when(delegate).createProducer(any());
66         assertNotNull(bindingDOMDataTreeProducerAdapter.createProducer(ImmutableSet.of()));
67         verify(delegate).createProducer(any());
68     }
69
70     @Test
71     public void closeTest() throws DataTreeProducerException, DOMDataTreeProducerException {
72         reset(delegate);
73         bindingDOMDataTreeProducerAdapter.close();
74         verify(delegate).close();
75     }
76
77     @Test(expected = DataTreeProducerException.class)
78     public void closeTestWithException1() throws DataTreeProducerException, DOMDataTreeProducerException {
79         doThrow(new DOMDataTreeProducerBusyException("test")).when(delegate).close();
80         bindingDOMDataTreeProducerAdapter.close();
81     }
82
83     @Test(expected = DataTreeProducerException.class)
84     public void closeTestWithException2() throws DataTreeProducerException, DOMDataTreeProducerException {
85         doThrow(new DOMDataTreeProducerException("test")).when(delegate).close();
86         bindingDOMDataTreeProducerAdapter.close();
87     }
88 }