2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.databind;
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;
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.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.QName;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
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;
40 abstract class AbstractResourceBodyTest extends AbstractBodyTest {
41 static final NodeIdentifier CONT_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont"));
42 static final NodeIdentifier CONT1_NID = new NodeIdentifier(QName.create(INSTANCE_IDENTIFIER_MODULE_QNAME, "cont1"));
44 static final QName LST11 = QName.create("augment:module", "2014-01-17", "lst11");
45 static final QName KEYVALUE111 = QName.create(LST11, "keyvalue111");
46 static final QName KEYVALUE112 = QName.create(LST11, "keyvalue112");
48 static final QName LF111 = QName.create("augment:augment:module", "2014-01-17", "lf111");
49 static final NodeIdentifier LF112_NID = new NodeIdentifier(QName.create(LF111, "lf112"));
51 static final QName LF11 = QName.create("augment:module:leaf:list", "2014-01-27", "lf11");
52 static final QName LFLST11 = QName.create(LF11, "lflst11");
54 private static DatabindContext DATABIND;
56 private final Function<InputStream, ResourceBody> bodyConstructor;
57 private final DOMMountPointService mountPointService;
58 private final DOMMountPoint mountPoint;
60 AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
61 this.bodyConstructor = requireNonNull(bodyConstructor);
63 mountPointService = mock(DOMMountPointService.class);
64 mountPoint = mock(DOMMountPoint.class);
65 doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
66 doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint)
67 .getService(DOMSchemaService.class);
72 public 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));
79 // FIXME: migrate callers to use string literals
81 final @NonNull NormalizedNode parseResource(final String uriPath, final String resourceName) throws IOException {
82 return parse(uriPath, AbstractResourceBodyTest.class.getResourceAsStream(resourceName));
85 final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
86 return parse(uriPath, stringInputStream(patchBody));
89 private @NonNull NormalizedNode parse(final String uriPath, final InputStream patchBody) throws IOException {
90 final ApiPath apiPath;
92 apiPath = ApiPath.parse(uriPath);
93 } catch (ParseException e) {
94 throw new AssertionError(e);
97 try (var body = bodyConstructor.apply(patchBody)) {
98 final var context = InstanceIdentifierContext.ofApiPath(apiPath, DATABIND, mountPointService);
99 return body.toNormalizedNode(context.getInstanceIdentifier(), context.inference(), context.getSchemaNode());
103 static final void assertRangeViolation(final ThrowingRunnable runnable) {
104 final var ex = assertThrows(RestconfDocumentedException.class, runnable);
105 final var errors = ex.getErrors();
106 assertEquals(1, errors.size());
108 final var error = errors.get(0);
109 assertEquals(ErrorType.APPLICATION, error.getErrorType());
110 assertEquals(ErrorTag.INVALID_VALUE, error.getErrorTag());
111 assertEquals("bar error app tag", error.getErrorAppTag());
112 assertEquals("bar error message", error.getErrorMessage());