2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.singleton.impl.tx;
11 import akka.actor.ActorSystem;
12 import akka.testkit.JavaTestKit;
13 import akka.testkit.TestProbe;
14 import akka.util.Timeout;
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import java.net.InetSocketAddress;
18 import java.util.concurrent.TimeUnit;
19 import org.junit.After;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.opendaylight.controller.config.util.xml.DocumentedException;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
27 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
31 import org.opendaylight.yangtools.yang.common.QName;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
34 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
35 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
37 public class ProxyReadTransactionTest {
38 private static final YangInstanceIdentifier PATH = YangInstanceIdentifier.EMPTY;
39 private static final LogicalDatastoreType STORE = LogicalDatastoreType.CONFIGURATION;
41 private ActorSystem system;
42 private TestProbe masterActor;
43 private ContainerNode node;
44 private ProxyReadTransaction tx;
47 public void setUp() throws Exception {
48 system = ActorSystem.apply();
49 masterActor = new TestProbe(system);
50 final RemoteDeviceId id = new RemoteDeviceId("dev1", InetSocketAddress.createUnresolved("localhost", 17830));
51 node = Builders.containerBuilder()
52 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(QName.create("cont")))
54 tx = new ProxyReadTransaction(masterActor.ref(), id, system, Timeout.apply(5, TimeUnit.SECONDS));
58 public void tearDown() throws Exception {
59 JavaTestKit.shutdownActorSystem(system, null, true);
63 public void testRead() throws Exception {
64 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(STORE, PATH);
65 masterActor.expectMsgClass(ReadRequest.class);
66 masterActor.reply(new NormalizedNodeMessage(PATH, node));
67 final Optional<NormalizedNode<?, ?>> result = read.checkedGet();
68 Assert.assertTrue(result.isPresent());
69 Assert.assertEquals(node, result.get());
73 public void testReadEmpty() throws Exception {
74 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(STORE, PATH);
75 masterActor.expectMsgClass(ReadRequest.class);
76 masterActor.reply(new EmptyReadResponse());
77 final Optional<NormalizedNode<?, ?>> result = read.checkedGet();
78 Assert.assertFalse(result.isPresent());
81 @Test(expected = ReadFailedException.class)
82 public void testReadFail() throws Exception {
83 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(STORE, PATH);
84 masterActor.expectMsgClass(ReadRequest.class);
85 masterActor.reply(new RuntimeException("fail"));
90 public void testExists() throws Exception {
91 final CheckedFuture<Boolean, ReadFailedException> read = tx.exists(STORE, PATH);
92 masterActor.expectMsgClass(ExistsRequest.class);
93 masterActor.reply(true);
94 final Boolean result = read.checkedGet();
95 Assert.assertTrue(result);
98 @Test(expected = ReadFailedException.class)
99 public void testExistsFail() throws Exception {
100 final CheckedFuture<Boolean, ReadFailedException> read = tx.exists(STORE, PATH);
101 masterActor.expectMsgClass(ExistsRequest.class);
102 masterActor.reply(new RuntimeException("fail"));
107 public void testMasterDownRead() throws Exception {
108 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read = tx.read(STORE, PATH);
109 masterActor.expectMsgClass(ReadRequest.class);
110 //master doesn't reply
113 Assert.fail("Exception should be thrown");
114 } catch (final ReadFailedException e) {
115 final Throwable cause = e.getCause();
116 Assert.assertTrue(cause instanceof DocumentedException);
117 final DocumentedException de = (DocumentedException) cause;
118 Assert.assertEquals(DocumentedException.ErrorSeverity.WARNING, de.getErrorSeverity());
119 Assert.assertEquals(DocumentedException.ErrorTag.OPERATION_FAILED, de.getErrorTag());
120 Assert.assertEquals(DocumentedException.ErrorType.APPLICATION, de.getErrorType());