fc4da82c502f86b1ca493649a0696e983cfcef56
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractResourceBodyTest.java
1 /*
2  * Copyright (c) 2023 PANTHEON.tech, s.r.o. 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.restconf.nb.rfc8040.databind;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.text.ParseException;
17 import java.util.function.Function;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.junit.jupiter.api.BeforeAll;
20 import org.junit.jupiter.api.extension.ExtendWith;
21 import org.junit.jupiter.api.function.Executable;
22 import org.mockito.Mock;
23 import org.mockito.junit.jupiter.MockitoExtension;
24 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.restconf.api.ApiPath;
28 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
29 import org.opendaylight.restconf.common.errors.RestconfError;
30 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
31 import org.opendaylight.restconf.server.api.DataPutPath;
32 import org.opendaylight.restconf.server.api.DatabindContext;
33 import org.opendaylight.yangtools.yang.common.ErrorTag;
34 import org.opendaylight.yangtools.yang.common.ErrorType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
38 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
39
40 @ExtendWith(MockitoExtension.class)
41 abstract class AbstractResourceBodyTest extends AbstractBodyTest {
42     static final NodeIdentifier CONT_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
43     static final NodeIdentifier CONT1_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont1"));
44
45     static final QName LST11 = QName.create("augment:module", "2014-01-17", "lst11");
46     static final QName KEYVALUE111 = QName.create(LST11, "keyvalue111");
47     static final QName KEYVALUE112 = QName.create(LST11, "keyvalue112");
48
49     static final QName LF111 = QName.create("augment:augment:module", "2014-01-17", "lf111");
50     static final NodeIdentifier LF112_NID = new NodeIdentifier(QName.create(LF111, "lf112"));
51
52     static final QName LF11 = QName.create("augment:module:leaf:list", "2014-01-27", "lf11");
53     static final QName LFLST11 = QName.create(LF11, "lflst11");
54
55     private static DatabindContext DATABIND;
56
57     @Mock
58     DOMDataBroker dataBroker;
59     @Mock
60     DOMMountPointService mountPointService;
61     @Mock
62     DOMMountPoint mountPoint;
63
64     private final Function<InputStream, ResourceBody> bodyConstructor;
65
66     AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
67         assertNotNull(bodyConstructor);
68         this.bodyConstructor = bodyConstructor;
69     }
70
71     @BeforeAll
72     static final void initModelContext() throws Exception {
73         final var testFiles = loadFiles("/instanceidentifier/yang");
74         testFiles.addAll(loadFiles("/modules"));
75         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
76         DATABIND = DatabindContext.ofModel(YangParserTestUtils.parseYangFiles(testFiles));
77     }
78
79     final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
80         final ApiPath apiPath;
81         try {
82             apiPath = ApiPath.parse(uriPath);
83         } catch (ParseException e) {
84             throw new AssertionError(e);
85         }
86
87         final var strategy = new MdsalRestconfStrategy(DATABIND, dataBroker, null, null, null, mountPointService);
88         final var stratAndPath = strategy.resolveStrategyPath(apiPath);
89
90         try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
91             return body.toNormalizedNode(new DataPutPath(stratAndPath.strategy().databind(),
92                 stratAndPath.path().inference(), stratAndPath.path().instance()));
93         }
94     }
95
96     static final RestconfError assertError(final Executable executable) {
97         final var ex = assertThrows(RestconfDocumentedException.class, executable);
98         final var errors = ex.getErrors();
99         assertEquals(1, errors.size());
100         return errors.get(0);
101     }
102
103     static final void assertRangeViolation(final Executable executable) {
104         final var error = assertError(executable);
105         assertEquals(ErrorType.APPLICATION, error.getErrorType());
106         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
107         assertEquals("bar error app tag", error.getErrorAppTag());
108         assertEquals("bar error message", error.getErrorMessage());
109     }
110 }