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 org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertNotNull;
12 import static org.junit.jupiter.api.Assertions.assertThrows;
14 import com.google.common.collect.ImmutableMap;
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.text.ParseException;
18 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.junit.jupiter.api.BeforeAll;
21 import org.junit.jupiter.api.extension.ExtendWith;
22 import org.junit.jupiter.api.function.Executable;
23 import org.mockito.Mock;
24 import org.mockito.junit.jupiter.MockitoExtension;
25 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
26 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
27 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
28 import org.opendaylight.restconf.api.ApiPath;
29 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
30 import org.opendaylight.restconf.common.errors.RestconfError;
31 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
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;
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"));
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");
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"));
53 static final QName LF11 = QName.create("augment:module:leaf:list", "2014-01-27", "lf11");
54 static final QName LFLST11 = QName.create(LF11, "lflst11");
56 private static DatabindContext DATABIND;
59 DOMDataBroker dataBroker;
61 DOMMountPointService mountPointService;
63 DOMMountPoint mountPoint;
65 private final Function<InputStream, ResourceBody> bodyConstructor;
67 AbstractResourceBodyTest(final Function<InputStream, ResourceBody> bodyConstructor) {
68 assertNotNull(bodyConstructor);
69 this.bodyConstructor = bodyConstructor;
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));
80 final @NonNull NormalizedNode parse(final String uriPath, final String patchBody) throws IOException {
81 final ApiPath apiPath;
83 apiPath = ApiPath.parse(uriPath);
84 } catch (ParseException e) {
85 throw new AssertionError(e);
88 final var strategy = new MdsalRestconfStrategy(DATABIND, dataBroker, ImmutableMap.of(), null, null, null,
90 final var stratAndPath = strategy.resolveStrategyPath(apiPath);
92 try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
93 return body.toNormalizedNode(stratAndPath.path());
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);
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());