Merge "Change put/merge methods to be type-safe in WriteTransaction"
[controller.git] / opendaylight / md-sal / sal-inmemory-datastore / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / SchemaUpdateForTransactionTest.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.md.sal.dom.store.impl;
9
10 import static org.junit.Assert.assertNotNull;
11
12 import java.util.concurrent.ExecutionException;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.bi.ba.rpcservice.rev140701.RockTheHouseInput;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
19 import org.opendaylight.yangtools.sal.binding.generator.impl.ModuleInfoBackedContext;
20 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
21 import org.opendaylight.yangtools.yang.binding.util.BindingReflections;
22 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
23 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25
26 import com.google.common.base.Throwables;
27 import com.google.common.util.concurrent.MoreExecutors;
28
29 public class SchemaUpdateForTransactionTest {
30
31     private static final InstanceIdentifier TOP_PATH = InstanceIdentifier.of(Top.QNAME);
32     private SchemaContext schemaContext;
33     private InMemoryDOMDataStore domStore;
34
35     @Before
36     public void setupStore() {
37         domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
38         loadSchemas(RockTheHouseInput.class);
39     }
40
41     public void loadSchemas(final Class<?>... classes) {
42         YangModuleInfo moduleInfo;
43         try {
44             ModuleInfoBackedContext context = ModuleInfoBackedContext.create();
45             for (Class<?> clz : classes) {
46                 moduleInfo = BindingReflections.getModuleInfo(clz);
47
48                 context.registerModuleInfo(moduleInfo);
49             }
50             schemaContext = context.tryToCreateSchemaContext().get();
51             domStore.onGlobalContextUpdated(schemaContext);
52         } catch (Exception e) {
53             Throwables.propagateIfPossible(e);
54         }
55     }
56
57     /**
58      * Test suite tests allocating transaction when schema context
59      * does not contain module necessary for client write,
60      * then triggering update of global schema context
61      * and then performing write (according to new module).
62      *
63      * If transaction between allocation and schema context was
64      * unmodified, it is safe to change its schema context
65      * to new one (e.g. it will be same as if allocated after
66      * schema context update.)
67      *
68      * @throws InterruptedException
69      * @throws ExecutionException
70      */
71     @Test
72     public void testTransactionSchemaUpdate() throws InterruptedException, ExecutionException {
73
74         assertNotNull(domStore);
75
76         // We allocate transaction, initial schema context does not
77         // contain Lists model
78         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
79         assertNotNull(writeTx);
80
81         // we trigger schema context update to contain Lists model
82         loadSchemas(RockTheHouseInput.class, Top.class);
83
84         /**
85          *
86          * Writes /test in writeTx, this write should not fail
87          * with IllegalArgumentException since /test is in
88          * schema context.
89          *
90          */
91         writeTx.write(TOP_PATH, ImmutableNodes.containerNode(Top.QNAME));
92
93     }
94
95 }