5a8740b2d506fdda1d9d618ae8f260ec8e7442d7
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.translate.impl.write.util;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Matchers.any;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28 import static org.mockito.MockitoAnnotations.initMocks;
29
30 import com.google.common.base.Optional;
31 import com.google.common.util.concurrent.CheckedFuture;
32 import io.fd.honeycomb.v3po.translate.Context;
33 import io.fd.honeycomb.v3po.translate.util.write.TransactionWriteContext;
34 import java.util.Map;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.mockito.Mock;
38 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
39 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
41 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Vpp;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
49 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
50
51 public class TransactionWriteContextTest {
52
53     @Mock
54     private BindingNormalizedNodeSerializer serializer;
55     @Mock
56     private DOMDataReadOnlyTransaction beforeTx;
57     @Mock
58     private DOMDataReadOnlyTransaction afterTx;
59     @Mock
60     private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future;
61     @Mock
62     private Optional<org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode<?, ?>> optional;
63     @Mock
64     private Map.Entry entry;
65
66     private TransactionWriteContext transactionWriteContext;
67
68     @Before
69     public void setUp() {
70         initMocks(this);
71         transactionWriteContext = new TransactionWriteContext(serializer, beforeTx, afterTx);
72     }
73
74     @Test
75     public void testReadBeforeNoData() throws Exception {
76         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
77         when(future.checkedGet()).thenReturn(optional);
78         when(optional.isPresent()).thenReturn(false);
79
80         final InstanceIdentifier<BridgeDomain> instanceId =
81                 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
82
83         final Optional<DataObject> dataObjects = transactionWriteContext.readBefore(instanceId);
84         assertNotNull(dataObjects);
85         assertFalse(dataObjects.isPresent());
86
87         verify(serializer).toYangInstanceIdentifier(instanceId);
88         verify(serializer, never()).fromNormalizedNode(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
89     }
90
91
92     @Test
93     public void testReadBefore() throws Exception {
94         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
95         when(future.checkedGet()).thenReturn(optional);
96         when(optional.isPresent()).thenReturn(true);
97
98         final InstanceIdentifier<BridgeDomain> instanceId =
99                 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
100         final YangInstanceIdentifier yangId = YangInstanceIdentifier.builder().node(VppState.QNAME).node(
101                 BridgeDomains.QNAME).node(BridgeDomain.QNAME).build();
102         when(serializer.toYangInstanceIdentifier(any(InstanceIdentifier.class))).thenReturn(yangId);
103         when(serializer.fromNormalizedNode(eq(yangId), any(NormalizedNode.class))).thenReturn(entry);
104         when(entry.getValue()).thenReturn(mock(DataObject.class));
105
106         final Optional<DataObject> dataObjects = transactionWriteContext.readBefore(instanceId);
107         assertNotNull(dataObjects);
108         assertTrue(dataObjects.isPresent());
109
110         verify(serializer).toYangInstanceIdentifier(instanceId);
111         verify(serializer).fromNormalizedNode(eq(yangId), any(NormalizedNode.class));
112     }
113
114     @Test(expected = IllegalStateException.class)
115     public void testReadBeforeFailed() throws Exception {
116         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
117         when(future.checkedGet()).thenThrow(ReadFailedException.class);
118         transactionWriteContext.readBefore(mock(InstanceIdentifier.class));
119     }
120
121     @Test(expected = IllegalStateException.class)
122     public void testReadAfterFailed() throws Exception {
123         when(afterTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
124         when(future.checkedGet()).thenThrow(ReadFailedException.class);
125         transactionWriteContext.readAfter(mock(InstanceIdentifier.class));
126     }
127
128     @Test
129     public void testGetContext() throws Exception {
130         assertNotNull(transactionWriteContext.getContext());
131     }
132
133     @Test
134     public void testClose() throws Exception {
135         final Context context = transactionWriteContext.getContext();
136         transactionWriteContext.close();
137         // TODO verify context was closed
138     }
139 }