Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / DataTreeCandidateValidatorTest2.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 DataTreeCandidateValidatorTest2 {
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 typeText;
51     private static QName devDesc;
52     private static QName sn;
53     private static QName defaultIp;
54
55     private static QName deviceTypeStr;
56     private static QName deviceType;
57     private static QName type;
58     private static QName desc;
59
60     private static final Logger LOG = LoggerFactory.getLogger(DataTreeCandidateValidatorTest2.class);
61
62     @BeforeAll
63     static void init() throws DataValidationFailedException {
64         context = YangParserTestUtils.parseYangResourceDirectory("/leafref-validation");
65
66         for (final var module : context.getModules()) {
67             if (module.getName().equals("leafref-validation2")) {
68                 mainModule = module;
69             }
70         }
71
72         rootModuleQname = mainModule.getQNameModule();
73         rootLeafRefContext = LeafRefContext.create(context);
74
75         chips = QName.create(rootModuleQname, "chips");
76         chip = QName.create(rootModuleQname, "chip");
77         devType = QName.create(rootModuleQname, "dev_type");
78         chipDesc = QName.create(rootModuleQname, "chip_desc");
79
80         devices = QName.create(rootModuleQname, "devices");
81         device = QName.create(rootModuleQname, "device");
82         typeText = QName.create(rootModuleQname, "type_text");
83         devDesc = QName.create(rootModuleQname, "dev_desc");
84         sn = QName.create(rootModuleQname, "sn");
85         defaultIp = QName.create(rootModuleQname, "default_ip");
86
87         deviceTypeStr = QName.create(rootModuleQname, "device_types");
88         deviceType = QName.create(rootModuleQname, "device_type");
89         type = QName.create(rootModuleQname, "type");
90         desc = QName.create(rootModuleQname, "desc");
91
92         inMemoryDataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_OPERATIONAL, context);
93         final var initialDataTreeModification = inMemoryDataTree.takeSnapshot().newModification();
94         initialDataTreeModification.write(YangInstanceIdentifier.of(chips), Builders.containerBuilder()
95             .withNodeIdentifier(new NodeIdentifier(chips))
96             .addChild(Builders.mapBuilder()
97                 .withNodeIdentifier(new NodeIdentifier(chip))
98                 .addChild(createChipsListEntry("dev_type_1", "desc1"))
99                 .addChild(createChipsListEntry("dev_type_2", "desc2"))
100                 .build())
101             .build());
102
103         initialDataTreeModification.write(YangInstanceIdentifier.of(deviceTypeStr), Builders.containerBuilder()
104             .withNodeIdentifier(new NodeIdentifier(deviceTypeStr))
105             .addChild(Builders.mapBuilder()
106                 .withNodeIdentifier(new NodeIdentifier(deviceType))
107                 .addChild(createDevTypeListEntry("dev_type_1", "typedesc1"))
108                 .addChild(createDevTypeListEntry("dev_type_2", "typedesc2"))
109                 .addChild(createDevTypeListEntry("dev_type_3", "typedesc3"))
110                 .build())
111             .build());
112
113         initialDataTreeModification.ready();
114         final var writeChipsCandidate = inMemoryDataTree.prepare(initialDataTreeModification);
115
116         inMemoryDataTree.commit(writeChipsCandidate);
117         LOG.debug("{}", inMemoryDataTree);
118     }
119
120     @AfterAll
121     static void cleanup() {
122         inMemoryDataTree = null;
123         rootLeafRefContext = null;
124         mainModule = null;
125         context = null;
126     }
127
128     @Test
129     void dataTreeCanditateValidationTest2() throws DataValidationFailedException {
130         writeDevices();
131     }
132
133     private static void writeDevices() throws DataValidationFailedException {
134         final var writeModification = inMemoryDataTree.takeSnapshot().newModification();
135         writeModification.write(YangInstanceIdentifier.of(devices), Builders.containerBuilder()
136             .withNodeIdentifier(new NodeIdentifier(devices))
137             .addChild(Builders.mapBuilder()
138                 .withNodeIdentifier(new NodeIdentifier(device))
139                 .addChild(createDeviceListEntry("dev_type_1", "typedesc1", 123456, "192.168.0.1"))
140                 .addChild(createDeviceListEntry("dev_type_2", "typedesc2", 123457, "192.168.0.1"))
141                 .addChild(createDeviceListEntry("dev_type_2", "typedesc3", 123457, "192.168.0.1"))
142                 .addChild(createDeviceListEntry("dev_type_1", "typedesc2", 123458, "192.168.0.1"))
143                 .addChild(createDeviceListEntry("unknown", "unknown", 123457, "192.168.0.1"))
144                 .build())
145             .build());
146
147         writeModification.ready();
148         final var writeDevicesCandidate = inMemoryDataTree.prepare(writeModification);
149
150         LOG.debug("*************************");
151         LOG.debug("Before writeDevices: ");
152         LOG.debug("*************************");
153         LOG.debug("{}", inMemoryDataTree);
154
155         final var ex = assertThrows(LeafRefDataValidationFailedException.class,
156             () -> LeafRefValidation.validate(writeDevicesCandidate, rootLeafRefContext));
157         assertEquals(4, ex.getValidationsErrorsCount());
158
159         inMemoryDataTree.commit(writeDevicesCandidate);
160
161         LOG.debug("*************************");
162         LOG.debug("After write: ");
163         LOG.debug("*************************");
164         LOG.debug("{}", inMemoryDataTree);
165     }
166
167     private static MapEntryNode createDevTypeListEntry(final String typeVal, final String descVal) {
168         return Builders.mapEntryBuilder()
169             .withNodeIdentifier(NodeIdentifierWithPredicates.of(deviceType, type, typeVal))
170             .addChild(ImmutableNodes.leafNode(type, typeVal))
171             .addChild(ImmutableNodes.leafNode(desc, descVal))
172             .build();
173     }
174
175     private static MapEntryNode createChipsListEntry(final String devTypeVal, final String chipDescVal) {
176         return Builders.mapEntryBuilder()
177             .withNodeIdentifier(NodeIdentifierWithPredicates.of(chip, devType, devTypeVal))
178             .addChild(ImmutableNodes.leafNode(devType, devTypeVal))
179             .addChild(ImmutableNodes.leafNode(chipDesc, chipDescVal))
180             .build();
181     }
182
183     private static MapEntryNode createDeviceListEntry(final String typeTextVal, final String descVal, final int snVal,
184             final String defaultIpVal) {
185         return Builders.mapEntryBuilder()
186             .withNodeIdentifier(NodeIdentifierWithPredicates.of(device, Map.of(typeText, typeTextVal, sn, snVal)))
187             .addChild(ImmutableNodes.leafNode(typeText, typeTextVal))
188             .addChild(ImmutableNodes.leafNode(devDesc, descVal))
189             .addChild(ImmutableNodes.leafNode(sn, snVal))
190             .addChild(ImmutableNodes.leafNode(defaultIp, defaultIpVal))
191             .build();
192     }
193 }