Promote DatabindContext to restconf.server.api
[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 java.util.Objects.requireNonNull;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertThrows;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.text.ParseException;
20 import java.util.Optional;
21 import java.util.function.Function;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.junit.BeforeClass;
24 import org.junit.function.ThrowingRunnable;
25 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
26 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
27 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
28 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
29 import org.opendaylight.restconf.api.ApiPath;
30 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
31 import org.opendaylight.restconf.nb.rfc8040.legacy.InstanceIdentifierContext;
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;
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 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     private final Function<InputStream, ResourceBody> bodyConstructor;
58     private final DOMMountPointService mountPointService;
59     private final DOMMountPoint mountPoint;
60
61     AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
62         this.bodyConstructor = requireNonNull(bodyConstructor);
63
64         mountPointService = mock(DOMMountPointService.class);
65         mountPoint = mock(DOMMountPoint.class);
66         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
67         doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
68             .getService(DOMSchemaService.class);
69
70     }
71
72     @BeforeClass
73     public 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     // FIXME: migrate callers to use string literals
81     @Deprecated
82     final @NonNull NormalizedNode parseResource(final String uriPath, final String resourceName) throws IOException {
83         return parse(uriPath, AbstractResourceBodyTest.class.getResourceAsStream(resourceName));
84     }
85
86     final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
87         return parse(uriPath, stringInputStream(patchBody));
88     }
89
90     private @NonNull NormalizedNode parse(final String uriPath, final InputStream patchBody) throws IOException {
91         final ApiPath apiPath;
92         try {
93             apiPath = ApiPath.parse(uriPath);
94         } catch (ParseException e) {
95             throw new AssertionError(e);
96         }
97
98         try (var body = bodyConstructor.apply(patchBody)) {
99             final var context = InstanceIdentifierContext.ofApiPath(apiPath, DATABIND, mountPointService);
100             return body.toNormalizedNode(context.getInstanceIdentifier(), context.inference(), context.getSchemaNode());
101         }
102     }
103
104     static final void assertRangeViolation(final ThrowingRunnable runnable) {
105         final var ex = assertThrows(RestconfDocumentedException.class, runnable);
106         final var errors = ex.getErrors();
107         assertEquals(1, errors.size());
108
109         final var error = errors.get(0);
110         assertEquals(ErrorType.APPLICATION, error.getErrorType());
111         assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
112         assertEquals("bar error app tag", error.getErrorAppTag());
113         assertEquals("bar error message", error.getErrorMessage());
114     }
115 }