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.Context;
33 import io.fd.honeycomb.v3po.translate.util.write.TransactionWriteContext;
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.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.Vpp;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.VppState;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.BridgeDomains;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.v3po.rev150105.vpp.bridge.domains.BridgeDomain;
45 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
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;
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 TransactionWriteContext transactionWriteContext;
71 transactionWriteContext = new TransactionWriteContext(serializer, beforeTx, afterTx);
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);
80 final InstanceIdentifier<BridgeDomain> instanceId =
81 InstanceIdentifier.create(Vpp.class).child(BridgeDomains.class).child(BridgeDomain.class);
83 final Optional<DataObject> dataObjects = transactionWriteContext.readBefore(instanceId);
84 assertNotNull(dataObjects);
85 assertFalse(dataObjects.isPresent());
87 verify(serializer).toYangInstanceIdentifier(instanceId);
88 verify(serializer, never()).fromNormalizedNode(any(YangInstanceIdentifier.class), any(NormalizedNode.class));
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);
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));
106 final Optional<DataObject> dataObjects = transactionWriteContext.readBefore(instanceId);
107 assertNotNull(dataObjects);
108 assertTrue(dataObjects.isPresent());
110 verify(serializer).toYangInstanceIdentifier(instanceId);
111 verify(serializer).fromNormalizedNode(eq(yangId), any(NormalizedNode.class));
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));
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));
129 public void testGetContext() throws Exception {
130 assertNotNull(transactionWriteContext.getContext());
134 public void testClose() throws Exception {
135 final Context context = transactionWriteContext.getContext();
136 transactionWriteContext.close();
137 // TODO verify context was closed