RestconfServer requires ApiPath identifiers
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / databind / AbstractPatchBodyTest.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.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.util.Optional;
18 import java.util.function.Function;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.junit.Before;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
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.common.patch.PatchContext;
30 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
31 import org.opendaylight.restconf.nb.rfc8040.utils.parser.ParserIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33
34 @RunWith(MockitoJUnitRunner.Silent.class)
35 abstract class AbstractPatchBodyTest extends AbstractInstanceIdentifierTest {
36     private final Function<InputStream, PatchBody> bodyConstructor;
37
38     @Mock
39     DOMMountPointService mountPointService;
40     @Mock
41     DOMMountPoint mountPoint;
42
43     AbstractPatchBodyTest(final Function<InputStream, PatchBody> bodyConstructor) {
44         this.bodyConstructor = requireNonNull(bodyConstructor);
45     }
46
47     @Before
48     public final void before() {
49         doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
50         doReturn(Optional.of(FixedDOMSchemaService.of(IID_SCHEMA))).when(mountPoint).getService(DOMSchemaService.class);
51     }
52
53     @NonNull String mountPrefix() {
54         return "";
55     }
56
57     @Nullable DOMMountPoint mountPoint() {
58         return null;
59     }
60
61     static final void checkPatchContext(final PatchContext patchContext) {
62         assertNotNull(patchContext.patchId());
63         assertNotNull(patchContext.entities());
64     }
65
66     final @NonNull PatchContext parse(final String prefix, final String suffix, final String patchBody)
67             throws IOException {
68         final String uriPath;
69         if (prefix.isEmpty()) {
70             uriPath = suffix;
71         } else if (suffix.isEmpty()) {
72             uriPath = prefix;
73         } else {
74             uriPath = prefix + '/' + suffix;
75         }
76
77         final var iid = ParserIdentifier.toInstanceIdentifier(uriPath, IID_SCHEMA, mountPointService);
78
79         try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
80             return body.toPatchContext(iid.getSchemaContext(), iid.getInstanceIdentifier());
81         }
82     }
83 }