Migrate yang-data-tree-ri to JUnit5
[yangtools.git] / data / yang-data-tree-ri / src / test / java / org / opendaylight / yangtools / yang / data / tree / leafref / YT892Test.java
1 /*
2  * Copyright (c) 2018 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 com.google.common.collect.ImmutableMap;
11 import org.junit.jupiter.api.BeforeEach;
12 import org.junit.jupiter.api.Test;
13 import org.opendaylight.yangtools.yang.common.QName;
14 import org.opendaylight.yangtools.yang.common.Uint8;
15 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
16 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
17 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
18 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
19 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
20 import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
21 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
22 import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 class YT892Test {
26     private static final QName BGP = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp", "2018-08-14", "bgp");
27     private static final QName PEER_GROUPS = QName.create(BGP, "peer-groups");
28     private static final QName PEER_GROUP = QName.create(BGP, "peer-group");
29     private static final QName PEER_GROUP_NAME = QName.create(BGP, "peer-group-name");
30     private static final YangInstanceIdentifier BGP_ID = YangInstanceIdentifier.of(BGP);
31
32     private static final QName NETWORK_INSTANCES =
33             QName.create("urn:opendaylight:params:xml:ns:yang:test:network:instance", "2018-08-14",
34                 "network-instances");
35     private static final QName NETWORK_INSTANCE = QName.create(NETWORK_INSTANCES, "network-instance");
36     private static final QName NAME = QName.create(NETWORK_INSTANCES, "name");
37     private static final QName CONFIG = QName.create(NETWORK_INSTANCES, "config");
38     private static final QName PROTOCOLS = QName.create(NETWORK_INSTANCES, "protocols");
39     private static final QName PROTOCOL = QName.create(NETWORK_INSTANCES, "protocol");
40     private static final QName IDENTIFIER = QName.create(NETWORK_INSTANCES, "identifier");
41     private static final QName BGP_POLICY = QName.create("urn:opendaylight:params:xml:ns:yang:test:policy:types",
42         "2018-08-14", "BGP");
43     private static final QName TEST_BGP = QName.create("urn:opendaylight:params:xml:ns:yang:bgp:test:extensions",
44         "2018-08-14", "bgp");
45     private static final QName NEIGHBORS = QName.create(TEST_BGP, "neighbors");
46     private static final QName NEIGHBOR = QName.create(TEST_BGP, "neighbor");
47     private static final QName NEIGHBOR_ADDRESS = QName.create(TEST_BGP, "neighbor-address");
48     private static final QName TEST_CONFIG = QName.create(TEST_BGP, "config");
49     private static final QName TEST_PEER_GROUP = QName.create(TEST_BGP, "peer-group");
50     private static final QName AFI_SAFIS = QName.create(TEST_BGP, "afi-safis");
51     private static final QName AFI_SAFI = QName.create(TEST_BGP, "afi-safi");
52     private static final QName AFI_SAFI_NAME = QName.create(TEST_BGP, "afi-safi-name");
53     private static final QName IPV4_UNICAST = QName.create("urn:opendaylight:params:xml:ns:yang:test:bgp:types",
54         "2018-08-14", "IPV4-UNICAST");
55     private static final QName RECEIVE = QName.create(TEST_BGP, "receive");
56     private static final QName SEND_MAX = QName.create(TEST_BGP, "send-max");
57
58     private static final YangInstanceIdentifier NETWORK_INSTANCES_ID = YangInstanceIdentifier.of(NETWORK_INSTANCES);
59
60     private LeafRefContext leafRefContext;
61     private DataTree dataTree;
62
63     @BeforeEach
64     void setup() {
65         final var schemaContext = YangParserTestUtils.parseYangResourceDirectory("/yt892");
66         leafRefContext = LeafRefContext.create(schemaContext);
67         dataTree = new InMemoryDataTreeFactory().create(DataTreeConfiguration.DEFAULT_CONFIGURATION, schemaContext);
68     }
69
70     @Test
71     void testWriteBgpNeighbour() throws Exception {
72         final var writeModification = dataTree.takeSnapshot().newModification();
73         writeModification.write(BGP_ID, Builders.containerBuilder()
74             .withNodeIdentifier(new NodeIdentifier(BGP))
75             .withChild(Builders.containerBuilder()
76                 .withNodeIdentifier(new NodeIdentifier(PEER_GROUPS))
77                 .withChild(Builders.mapBuilder()
78                     .withNodeIdentifier(new NodeIdentifier(PEER_GROUP))
79                     .withChild(Builders.mapEntryBuilder()
80                         .withNodeIdentifier(NodeIdentifierWithPredicates.of(PEER_GROUP,
81                             PEER_GROUP_NAME, "application-peers"))
82                         .withChild(ImmutableNodes.leafNode(PEER_GROUP_NAME, "application-peers"))
83                         .build())
84                     .build())
85                 .build())
86             .build());
87
88         writeModification.write(NETWORK_INSTANCES_ID, Builders.containerBuilder()
89             .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCES))
90             .withChild(Builders.mapBuilder()
91                 .withNodeIdentifier(new NodeIdentifier(NETWORK_INSTANCE))
92                 .withChild(Builders.mapEntryBuilder()
93                     .withNodeIdentifier(NodeIdentifierWithPredicates.of(NETWORK_INSTANCE, NAME, "global-bgp"))
94                     .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
95                     .withChild(Builders.containerBuilder()
96                         .withNodeIdentifier(new NodeIdentifier(CONFIG))
97                         .withChild(ImmutableNodes.leafNode(NAME, "global-bgp"))
98                         .build())
99                     .withChild(Builders.containerBuilder()
100                         .withNodeIdentifier(new NodeIdentifier(PROTOCOLS))
101                         .withChild(Builders.mapBuilder()
102                             .withNodeIdentifier(new NodeIdentifier(PROTOCOL))
103                             .withChild(Builders.mapEntryBuilder()
104                                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(PROTOCOL, ImmutableMap.of(
105                                     IDENTIFIER, BGP_POLICY,
106                                     NAME, "test-bgp-instance")))
107                                 .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
108                                 .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
109                                 .withChild(Builders.containerBuilder()
110                                     .withNodeIdentifier(new NodeIdentifier(CONFIG))
111                                     .withChild(ImmutableNodes.leafNode(IDENTIFIER, BGP_POLICY))
112                                     .withChild(ImmutableNodes.leafNode(NAME, "test-bgp-instance"))
113                                     .build())
114                                 .withChild(Builders.containerBuilder()
115                                     .withNodeIdentifier(new NodeIdentifier(TEST_BGP))
116                                     .withChild(Builders.containerBuilder()
117                                         .withNodeIdentifier(new NodeIdentifier(NEIGHBORS))
118                                         .withChild(Builders.mapBuilder()
119                                             .withNodeIdentifier(new NodeIdentifier(NEIGHBOR))
120                                             .withChild(Builders.mapEntryBuilder()
121                                                 .withNodeIdentifier(NodeIdentifierWithPredicates.of(NEIGHBOR,
122                                                     NEIGHBOR_ADDRESS, "10.25.1.9"))
123                                                 .withChild(ImmutableNodes.leafNode(NEIGHBOR_ADDRESS, "10.25.1.9"))
124                                                 .withChild(Builders.containerBuilder()
125                                                     .withNodeIdentifier(new NodeIdentifier(TEST_CONFIG))
126                                                     .withChild(ImmutableNodes.leafNode(TEST_PEER_GROUP,
127                                                         "application-peers"))
128                                                     .build())
129                                                 .withChild(Builders.containerBuilder()
130                                                     .withNodeIdentifier(new NodeIdentifier(AFI_SAFIS))
131                                                     .withChild(Builders.mapBuilder()
132                                                         .withNodeIdentifier(new NodeIdentifier(AFI_SAFI))
133                                                         .withChild(Builders.mapEntryBuilder()
134                                                             .withNodeIdentifier(NodeIdentifierWithPredicates.of(
135                                                                 AFI_SAFI,
136                                                                 ImmutableMap.of(AFI_SAFI_NAME, IPV4_UNICAST)))
137                                                             .withChild(ImmutableNodes.leafNode(AFI_SAFI_NAME,
138                                                                 IPV4_UNICAST))
139                                                             .withChild(Builders.containerBuilder()
140                                                                 .withNodeIdentifier(
141                                                                     new NodeIdentifier(TEST_CONFIG))
142                                                                 .withChild(ImmutableNodes.leafNode(
143                                                                     AFI_SAFI_NAME, IPV4_UNICAST))
144                                                                 .build())
145                                                             .withChild(ImmutableNodes.leafNode(RECEIVE,
146                                                                 Boolean.TRUE))
147                                                             .withChild(ImmutableNodes.leafNode(SEND_MAX,
148                                                                 Uint8.ZERO))
149                                                             .build())
150                                                         .build())
151                                                     .build())
152                                                 .build())
153                                             .build())
154                                         .build())
155                                     .build())
156                                 .build())
157                             .build())
158                         .build())
159                     .build())
160                 .build())
161             .build());
162
163         writeModification.ready();
164         final var writeContributorsCandidate = dataTree.prepare(writeModification);
165         LeafRefValidation.validate(writeContributorsCandidate, leafRefContext);
166         dataTree.commit(writeContributorsCandidate);
167     }
168 }