Move AbstractBody et al.
[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.restconf.server.api.ResourceBody;
34 import org.opendaylight.yangtools.yang.common.ErrorTag;
35 import org.opendaylight.yangtools.yang.common.ErrorType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
39 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
40
41 @ExtendWith(MockitoExtension.class)
42 abstract class AbstractResourceBodyTest extends AbstractBodyTest {
43     static final NodeIdentifier CONT_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
44     static final NodeIdentifier CONT1_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont1"));
45
46     static final QName LST11 = QName.create("augment:module", "2014-01-17", "lst11");
47     static final QName KEYVALUE111 = QName.create(LST11, "keyvalue111");
48     static final QName KEYVALUE112 = QName.create(LST11, "keyvalue112");
49
50     static final QName LF111 = QName.create("augment:augment:module", "2014-01-17", "lf111");
51     static final NodeIdentifier LF112_NID = new NodeIdentifier(QName.create(LF111, "lf112"));
52
53     static final QName LF11 = QName.create("augment:module:leaf:list", "2014-01-27", "lf11");
54     static final QName LFLST11 = QName.create(LF11, "lflst11");
55
56     private static DatabindContext DATABIND;
57
58     @Mock
59     DOMDataBroker dataBroker;
60     @Mock
61     DOMMountPointService mountPointService;
62     @Mock
63     DOMMountPoint mountPoint;
64
65     private final Function<InputStream, ResourceBody> bodyConstructor;
66
67     AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
68         assertNotNull(bodyConstructor);
69         this.bodyConstructor = bodyConstructor;
70     }
71
72     @BeforeAll
73     static final void initModelContext() throws Exception {
74         final var testFiles = loadFiles("/instanceidentifier/yang");
75         testFiles.addAll(loadFiles("/modules"));
76         testFiles.addAll(loadFiles("/foo-xml-test/yang"));
77         DATABIND = DatabindContext.ofModel(YangParserTestUtils.parseYangFiles(testFiles));
78     }
79
80     final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
81         final ApiPath apiPath;
82         try {
83             apiPath = ApiPath.parse(uriPath);
84         } catch (ParseException e) {
85             throw new AssertionError(e);
86         }
87
88         final var strategy = new MdsalRestconfStrategy(DATABIND, dataBroker, null, null, null, mountPointService);
89         final var stratAndPath = strategy.resolveStrategyPath(apiPath);
90
91         try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
92             return body.toNormalizedNode(new DataPutPath(stratAndPath.strategy().databind(),
93                 stratAndPath.path().inference(), stratAndPath.path().instance()));
94         }
95     }
96
97     static final RestconfError assertError(final Executable executable) {
98         final var ex = assertThrows(RestconfDocumentedException.class, executable);
99         final var errors = ex.getErrors();
100         assertEquals(1, errors.size());
101         return errors.get(0);
102     }
103
104     static final void assertRangeViolation(final Executable executable) {
105         final var error = assertError(executable);
106         assertEquals(ErrorType.APPLICATION, error.getErrorType());
107         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
108         assertEquals("bar error app tag", error.getErrorAppTag());
109         assertEquals("bar error message", error.getErrorMessage());
110     }
111 }