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