Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / DataTreeCandidateValidatorTest3.java
1 /*
2  * Copyright (c) 2016 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.yangtools.yang.data.tree.leafref;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12
13 import java.util.Map;
14 import org.junit.jupiter.api.AfterAll;
15 import org.junit.jupiter.api.BeforeAll;
16 import org.junit.jupiter.api.Test;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.QNameModule;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
22 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
23 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
27 import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
28 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
29 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class DataTreeCandidateValidatorTest3 {
36
37     private static EffectiveModelContext context;
38     private static Module mainModule;
39     private static QNameModule rootModuleQname;
40     private static LeafRefContext rootLeafRefContext;
41     public static DataTree inMemoryDataTree;
42
43     private static QName chips;
44     private static QName chip;
45     private static QName devType;
46     private static QName chipDesc;
47
48     private static QName devices;
49     private static QName device;
50     private static QName typeText1;
51     private static QName typeText2;
52     private static QName typeText3;
53     private static QName devDesc;
54     private static QName sn;
55     private static QName defaultIp;
56
57     private static QName deviceTypeStr;
58     private static QName deviceType;
59     private static QName type1;
60     private static QName type2;
61     private static QName type3;
62     private static QName desc;
63
64     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest3.class);
65     private static final String NEW_LINE = System.getProperty("line.separator");
66
67     @BeforeAll
68     static void init() throws DataValidationFailedException {
69         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
70
71         for (final var module : context.getModules()) {
72             if (module.getName().equals("leafref-validation3")) {
73                 mainModule = module;
74             }
75         }
76
77         rootModuleQname = mainModule.getQNameModule();
78         rootLeafRefContext = LeafRefContext.create(context);
79
80
81         chips = QName.create(rootModuleQname, "chips");
82         chip = QName.create(rootModuleQname, "chip");
83         devType = QName.create(rootModuleQname, "dev_type");
84         chipDesc = QName.create(rootModuleQname, "chip_desc");
85
86         devices = QName.create(rootModuleQname, "devices");
87         device = QName.create(rootModuleQname, "device");
88         typeText1 = QName.create(rootModuleQname, "type_text1");
89         typeText2 = QName.create(rootModuleQname, "type_text2");
90         typeText3 = QName.create(rootModuleQname, "type_text3");
91         devDesc = QName.create(rootModuleQname, "dev_desc");
92         sn = QName.create(rootModuleQname, "sn");
93         defaultIp = QName.create(rootModuleQname, "default_ip");
94
95         deviceTypeStr = QName.create(rootModuleQname, "device_types");
96         deviceType = QName.create(rootModuleQname, "device_type");
97         type1 = QName.create(rootModuleQname, "type1");
98         type2 = QName.create(rootModuleQname, "type2");
99         type3 = QName.create(rootModuleQname, "type3");
100         desc = QName.create(rootModuleQname, "desc");
101
102         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
103
104         final var initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
105
106         initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
107             .withNodeIdentifier(new NodeIdentifier(chips))
108             .addChild(Builders.mapBuilder()
109                 .withNodeIdentifier(new NodeIdentifier(chip))
110                 .addChild(createChipsListEntry("dev_type_1", "desc1"))
111                 .addChild(createChipsListEntry("dev_type_2", "desc2"))
112                 .build())
113             .build());
114
115         initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
116             .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
117             .addChild(Builders.mapBuilder()
118                 .withNodeIdentifier(new NodeIdentifier(deviceType))
119                 .addChild(createDevTypeListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1"))
120                 .addChild(createDevTypeListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc2"))
121                 .addChild(createDevTypeListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3"))
122                 .build())
123             .build());
124
125         initialDataTreeModification.ready();
126         final var writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
127
128         inMemoryDataTree.commit(writeChipsCandidate);
129
130         LOG.debug("{}", inMemoryDataTree);
131     }
132
133     @AfterAll
134     static void cleanup() {
135         inMemoryDataTree = null;
136         rootLeafRefContext = null;
137         mainModule = null;
138         context = null;
139     }
140
141     @Test
142     void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
143         writeDevices();
144         mergeDevices();
145     }
146
147     private static void writeDevices() throws DataValidationFailedException {
148         final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
149         writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
150             .withNodeIdentifier(new NodeIdentifier(devices))
151             .addChild(Builders.mapBuilder()
152                 .withNodeIdentifier(new NodeIdentifier(device))
153                 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_1", "dev_type3_1", "typedesc1", 123456,
154                     "192.168.0.1"))
155                 .addChild(createDeviceListEntry("dev_type1_2", "dev_type2_2", "dev_type3_2", "typedesc1", 123457,
156                     "192.168.0.1"))
157                 .addChild(createDeviceListEntry("dev_type1_1", "dev_type2_2", "dev_type3_3", "typedesc2", 123458,
158                     "192.168.0.1"))
159                 .addChild(createDeviceListEntry("unk11", "unk22", "unk33", "unk_desc2", 123457, "192.168.0.1"))
160                 .build())
161             .build());
162
163         writeModification.ready();
164         final var writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
165
166         LOG.debug("*************************");
167         LOG.debug("Before writeDevices: ");
168         LOG.debug("*************************");
169         LOG.debug("{}", inMemoryDataTree);
170
171         final var ex = assertThrows(LeafRefDataValidationFailedException.class,
172             () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
173         assertEquals(6, ex.getValidationsErrorsCount());
174
175         inMemoryDataTree.commit(writeDevicesCandidate);
176
177         LOG.debug("*************************");
178         LOG.debug("After writeDevices: ");
179         LOG.debug("*************************");
180         LOG.debug("{}", inMemoryDataTree);
181     }
182
183     private static void mergeDevices() throws DataValidationFailedException {
184         final var devicesContainer = Builders.containerBuilder()
185             .withNodeIdentifier(new NodeIdentifier(devices))
186             .addChild(Builders.mapBuilder()
187                 .withNodeIdentifier(new NodeIdentifier(device))
188                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc3", 123459,
189                     "192.168.0.1"))
190                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_3", "dev_type3_3", "typedesc2", 123460,
191                     "192.168.0.1"))
192                 .addChild(createDeviceListEntry("dev_type1_3", "dev_type2_2", "dev_type3_1", "typedesc1", 123461,
193                     "192.168.0.1"))
194                 .addChild(createDeviceListEntry("unk1", "unk2", "unk3", "unk_desc", 123462, "192.168.0.1"))
195                 .build())
196             .build();
197
198         final var devicesPath = YangInstanceIdentifier.of(devices);
199         final var mergeModification = inMemoryDataTree.takeSnapshot().newModification();
200         mergeModification.write(devicesPath, devicesContainer);
201         mergeModification.merge(devicesPath, devicesContainer);
202
203         mergeModification.ready();
204         final var mergeDevicesCandidate = inMemoryDataTree.prepare(mergeModification);
205
206         LOG.debug("*************************");
207         LOG.debug("Before mergeDevices: ");
208         LOG.debug("*************************");
209         LOG.debug("{}", inMemoryDataTree);
210
211         final var ex = assertThrows(LeafRefDataValidationFailedException.class,
212             () -> LeafRefValidation.validate(mergeDevicesCandidate, rootLeafRefContext));
213         // :TODO verify errors count gz
214         assertEquals(6, ex.getValidationsErrorsCount());
215
216         inMemoryDataTree.commit(mergeDevicesCandidate);
217
218         LOG.debug("*************************");
219         LOG.debug("After mergeDevices: ");
220         LOG.debug("*************************");
221         LOG.debug("{}", inMemoryDataTree);
222     }
223
224     private static MapEntryNode createDevTypeListEntry(final String type1Val, final String type2Val,
225             final String type3Val, final String descVal) {
226         return Builders.mapEntryBuilder()
227             .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType,
228                 Map.of(type1, type1Val, type2, type2Val, type3, type3Val)))
229             .addChild(ImmutableNodes.leafNode(type1, type1Val))
230             .addChild(ImmutableNodes.leafNode(type2, type2Val))
231             .addChild(ImmutableNodes.leafNode(type3, type3Val))
232             .addChild(ImmutableNodes.leafNode(desc, descVal))
233             .build();
234     }
235
236     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
237         return Builders.mapEntryBuilder()
238             .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
239             .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
240             .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
241             .build();
242     }
243
244     private static MapEntryNode createDeviceListEntry(final String type1TextVal, final String type2TextVal,
245             final String type3TextVal, final String descVal, final int snVal, final String defaultIpVal) {
246         return Builders.mapEntryBuilder()
247             .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText1, type1TextVal, sn, snVal)))
248             .addChild(ImmutableNodes.leafNode(typeText1, type1TextVal))
249             .addChild(ImmutableNodes.leafNode(typeText2, type2TextVal))
250             .addChild(ImmutableNodes.leafNode(typeText3, type3TextVal))
251             .addChild(ImmutableNodes.leafNode(devDesc, descVal))
252             .addChild(ImmutableNodes.leafNode(sn, snVal))
253             .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))
254             .build();
255     }
256 }