bc6f567c2bd01fd3346cc0c8d9769fd20f9cfdc8
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / DOMCodecBug02Test.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.test.bugfix;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import java.util.Collections;
14 import java.util.Map;
15 import java.util.concurrent.Callable;
16 import java.util.concurrent.ExecutorService;
17 import java.util.concurrent.Executors;
18 import java.util.concurrent.Future;
19
20 import org.junit.Test;
21 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
24 import org.opendaylight.controller.sal.binding.test.util.BindingBrokerTestFactory;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34
35 import com.google.common.util.concurrent.ListeningExecutorService;
36 import com.google.common.util.concurrent.MoreExecutors;
37
38 public class DOMCodecBug02Test extends AbstractDataServiceTest {
39
40     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
41     private static final String NODE_ID = "node:1";
42
43     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
44
45     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
46             NODE_ID);
47
48     private static final InstanceIdentifier<Nodes> NODES_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
49             .toInstance();
50
51     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODES_INSTANCE_ID_BI = //
52     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
53             .node(Nodes.QNAME) //
54             .toInstance();
55
56     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
57             .child(Node.class, NODE_KEY).toInstance();
58
59     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
60     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
61             .node(Nodes.QNAME) //
62             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
63             .toInstance();
64     private static final NodeRef NODE_REF = new NodeRef(NODE_INSTANCE_ID_BA);
65
66     /**
67      * This test is ignored, till found out better way to test generation of
68      * classes without leaking of instances from previous run
69      *
70      * @throws Exception
71      */
72
73     @Override
74     public void setUp() {
75         ListeningExecutorService executor = MoreExecutors.sameThreadExecutor();
76         BindingBrokerTestFactory factory = new BindingBrokerTestFactory();
77         factory.setExecutor(executor);
78         factory.setStartWithParsedSchema(getStartWithSchema());
79         testContext = factory.getTestContext();
80         testContext.start();
81
82         baDataService = testContext.getBindingDataBroker();
83         biDataService = testContext.getDomDataBroker();
84         dataStore = testContext.getDomDataStore();
85         mappingService = testContext.getBindingToDomMappingService();
86     };
87
88     @Test
89     public void testSchemaContextNotAvailable() throws Exception {
90
91         ExecutorService testExecutor = Executors.newFixedThreadPool(1);
92         testContext.loadYangSchemaFromClasspath();
93         Future<Future<RpcResult<TransactionStatus>>> future = testExecutor
94                 .submit(new Callable<Future<RpcResult<TransactionStatus>>>() {
95                     @Override
96                     public Future<RpcResult<TransactionStatus>> call() throws Exception {
97                         NodesBuilder nodesBuilder = new NodesBuilder();
98                         nodesBuilder.setNode(Collections.<Node> emptyList());
99                         DataModificationTransaction transaction = baDataService.beginTransaction();
100                         transaction.putOperationalData(NODES_INSTANCE_ID_BA, nodesBuilder.build());
101                         return transaction.commit();
102                     }
103                 });
104
105         RpcResult<TransactionStatus> result = future.get().get();
106         assertEquals(TransactionStatus.COMMITED, result.getResult());
107
108         Nodes nodes = checkForNodes();
109         assertNotNull(nodes);
110
111     }
112
113     private Nodes checkForNodes() {
114         return (Nodes) baDataService.readOperationalData(NODES_INSTANCE_ID_BA);
115
116     }
117
118     @Override
119     protected boolean getStartWithSchema() {
120         return false;
121     }
122
123 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.