2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.impl.write.util;
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;
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;
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;
51 public class TransactionWriteContextTest {
54 private BindingNormalizedNodeSerializer serializer;
56 private DOMDataReadOnlyTransaction beforeTx;
58 private DOMDataReadOnlyTransaction afterTx;
60 private CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> future;
62 private Optional<org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode<?, ?>> optional;
64 private Map.Entry entry;
66 private MappingContext contextBroker;
68 private TransactionWriteContext transactionWriteContext;
73 transactionWriteContext = new TransactionWriteContext(serializer, beforeTx, afterTx, contextBroker);
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);
82 final InstanceIdentifier<BridgeDomain> instanceId =
83 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
85 final Optional<BridgeDomain> dataObjects = transactionWriteContext.readBefore(instanceId);
86 assertNotNull(dataObjects);
87 assertFalse(dataObjects.isPresent());
89 verify(serializer).toYangInstanceIdentifier(instanceId);
90 verify(serializer, never()).fromNormalizedNode(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
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);
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));
108 final Optional<BridgeDomain> dataObjects = transactionWriteContext.readBefore(instanceId);
109 assertNotNull(dataObjects);
110 assertTrue(dataObjects.isPresent());
112 verify(serializer).toYangInstanceIdentifier(instanceId);
113 verify(serializer).fromNormalizedNode(eq(yangId), any(NormalizedNode.class));
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));
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));
131 public void testGetContext() throws Exception {
132 assertNotNull(transactionWriteContext.getModificationCache());
136 public void testClose() throws Exception {
137 final ModificationCache context = transactionWriteContext.getModificationCache();
138 transactionWriteContext.close();
139 // TODO verify context was closed