Bug 2682 - Switch sal-binding-dom-it to sal-test-model
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / FlagsSerializationTest.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 import static org.junit.Assert.assertNull;
13
14 import org.junit.Test;
15 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
16 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
17 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.BitFlags;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.TllComplexAugment;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.List1Key;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Builder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.of.migration.test.model.rev150210.aug.grouping.list1.List11Key;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
33
34 import com.google.common.collect.ImmutableSet;
35
36 @SuppressWarnings("deprecation")
37 public class FlagsSerializationTest extends AbstractDataServiceTest {
38     private static final TopLevelListKey TLL_KEY = new TopLevelListKey("foo");
39     private static final List11Key LIST11_KEY = new List11Key(1234);
40     private static final List1Key LIST1_KEY = new List1Key("1");
41
42     private static final InstanceIdentifier<TopLevelList> TLL_INSTANCE_ID_BA = InstanceIdentifier.builder(Top.class) //
43             .child(TopLevelList.class, TLL_KEY).toInstance();
44
45     private static final InstanceIdentifier<? extends DataObject> LIST11_INSTANCE_ID_BA = //
46             TLL_INSTANCE_ID_BA.builder() //
47             .augmentation(TllComplexAugment.class)
48             .child(List1.class,LIST1_KEY)
49             .child(List11.class, LIST11_KEY) //
50             .toInstance();
51     private static final QName LIST11_FLAGS_QNAME = QName.create(List11.QNAME, "flags");
52
53     @Test
54     public void testIndirectGeneration() throws Exception {
55
56         BitFlags checkOverlapFlags = new BitFlags(true,false,false,false,false);
57         ImmutableSet<String> domCheckOverlapFlags = ImmutableSet.<String>of("FLAG_FIVE");
58         testFlags(checkOverlapFlags,domCheckOverlapFlags);
59
60
61
62         BitFlags allFalseFlags = new BitFlags(false,false,false,false,false);
63         ImmutableSet<String> domAllFalseFlags = ImmutableSet.<String>of();
64         testFlags(allFalseFlags,domAllFalseFlags);
65
66         BitFlags allTrueFlags = new BitFlags(true,true,true,true,true);
67         ImmutableSet<String> domAllTrueFlags = ImmutableSet.<String>of("FLAG_ONE","FLAG_TWO","FLAG_THREE","FLAG_FOUR","FLAG_FIVE");
68         testFlags(allTrueFlags,domAllTrueFlags);
69
70         testFlags(null,null);
71
72
73
74     }
75
76     private void testFlags(final BitFlags flagsToTest, final ImmutableSet<String> domFlags) throws Exception {
77         List11 list11 = createList11(flagsToTest);
78         assertNotNull(list11);
79
80         CompositeNode domList11 = biDataService.readConfigurationData(mappingService.toDataDom(LIST11_INSTANCE_ID_BA));
81
82         assertNotNull(domList11);
83         org.opendaylight.yangtools.yang.data.api.Node<?> readedFlags = domList11.getFirstSimpleByName(LIST11_FLAGS_QNAME);
84
85         if(domFlags != null) {
86             assertNotNull(readedFlags);
87             assertEquals(domFlags,readedFlags.getValue());
88         } else {
89             assertNull(readedFlags);
90         }
91         assertEquals(flagsToTest, list11.getFlags());
92
93         DataModificationTransaction transaction = baDataService.beginTransaction();
94         transaction.removeConfigurationData(LIST11_INSTANCE_ID_BA);
95         RpcResult<TransactionStatus> result = transaction.commit().get();
96         assertEquals(TransactionStatus.COMMITED, result.getResult());
97
98     }
99
100     private List11 createList11(final BitFlags flagsToTest) throws Exception {
101
102         DataModificationTransaction modification = baDataService.beginTransaction();
103
104         List11Builder list11b = new List11Builder();
105
106         list11b.setKey(LIST11_KEY);
107         list11b.setAttrStr("list:1:1");
108
109         list11b.setFlags(flagsToTest);
110
111         modification.putConfigurationData(LIST11_INSTANCE_ID_BA, list11b.build());
112         RpcResult<TransactionStatus> ret = modification.commit().get();
113         assertNotNull(ret);
114         assertEquals(TransactionStatus.COMMITED, ret.getResult());
115         return (List11) baDataService.readConfigurationData(LIST11_INSTANCE_ID_BA);
116     }
117 }

©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.