8a3bfbdd934b3bd887f9a43fb293d2c0920bd564
[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.MappingContext;
33 import io.fd.honeycomb.v3po.translate.ModificationCache;
34 import io.fd.honeycomb.v3po.translate.util.write.TransactionWriteContext;
35 import java.util.Map;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
40 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
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.binding.data.codec.api.BindingNormalizedNodeSerializer;
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     @Mock
66     private MappingContext contextBroker;
67
68     private TransactionWriteContext transactionWriteContext;
69
70     @Before
71     public void setUp() {
72         initMocks(this);
73         transactionWriteContext = new TransactionWriteContext(serializer, beforeTx, afterTx, contextBroker);
74     }
75
76     @Test
77     public void testReadBeforeNoData() throws Exception {
78         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
79         when(future.checkedGet()).thenReturn(optional);
80         when(optional.isPresent()).thenReturn(false);
81
82         final InstanceIdentifier<BridgeDomain> instanceId =
83                 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
84
85         final Optional<BridgeDomain> dataObjects = transactionWriteContext.readBefore(instanceId);
86         assertNotNull(dataObjects);
87         assertFalse(dataObjects.isPresent());
88
89         verify(serializer).toYangInstanceIdentifier(instanceId);
90         verify(serializer, never()).fromNormalizedNode(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
91     }
92
93
94     @Test
95     public void testReadBefore() throws Exception {
96         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
97         when(future.checkedGet()).thenReturn(optional);
98         when(optional.isPresent()).thenReturn(true);
99
100         final InstanceIdentifier<BridgeDomain> instanceId =
101                 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
102         final YangInstanceIdentifier yangId = YangInstanceIdentifier.builder().node(VppState.QNAME).node(
103                 BridgeDomains.QNAME).node(BridgeDomain.QNAME).build();
104         when(serializer.toYangInstanceIdentifier(any(InstanceIdentifier.class))).thenReturn(yangId);
105         when(serializer.fromNormalizedNode(eq(yangId), any(NormalizedNode.class))).thenReturn(entry);
106         when(entry.getValue()).thenReturn(mock(BridgeDomain.class));
107
108         final Optional<BridgeDomain> dataObjects = transactionWriteContext.readBefore(instanceId);
109         assertNotNull(dataObjects);
110         assertTrue(dataObjects.isPresent());
111
112         verify(serializer).toYangInstanceIdentifier(instanceId);
113         verify(serializer).fromNormalizedNode(eq(yangId), any(NormalizedNode.class));
114     }
115
116     @Test(expected = IllegalStateException.class)
117     public void testReadBeforeFailed() throws Exception {
118         when(beforeTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
119         when(future.checkedGet()).thenThrow(ReadFailedException.class);
120         transactionWriteContext.readBefore(mock(InstanceIdentifier.class));
121     }
122
123     @Test(expected = IllegalStateException.class)
124     public void testReadAfterFailed() throws Exception {
125         when(afterTx.read(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class))).thenReturn(future);
126         when(future.checkedGet()).thenThrow(ReadFailedException.class);
127         transactionWriteContext.readAfter(mock(InstanceIdentifier.class));
128     }
129
130     @Test
131     public void testGetContext() throws Exception {
132         assertNotNull(transactionWriteContext.getModificationCache());
133     }
134
135     @Test
136     public void testClose() throws Exception {
137         final ModificationCache context = transactionWriteContext.getModificationCache();
138         transactionWriteContext.close();
139         // TODO verify context was closed
140     }
141 }