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.assertNotNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
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;
34 @RunWith(MockitoJUnitRunner.Silent.class)
35 abstract class AbstractPatchBodyTest extends AbstractInstanceIdentifierTest {
36 private final Function<InputStream, PatchBody> bodyConstructor;
39 DOMMountPointService mountPointService;
41 DOMMountPoint mountPoint;
43 AbstractPatchBodyTest(final Function<InputStream, PatchBody> bodyConstructor) {
44 this.bodyConstructor = requireNonNull(bodyConstructor);
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);
53 @NonNull String mountPrefix() {
57 @Nullable DOMMountPoint mountPoint() {
61 static final void checkPatchContext(final PatchContext patchContext) {
62 assertNotNull(patchContext.patchId());
63 assertNotNull(patchContext.entities());
66 final @NonNull PatchContext parse(final String prefix, final String suffix, final String patchBody)
69 if (prefix.isEmpty()) {
71 } else if (suffix.isEmpty()) {
74 uriPath = prefix + '/' + suffix;
77 final var iid = ParserIdentifier.toInstanceIdentifier(uriPath, IID_SCHEMA, mountPointService);
79 try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
80 return body.toPatchContext(iid.getSchemaContext(), iid.getInstanceIdentifier());