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.text.ParseException;
18 import java.util.Optional;
19 import java.util.function.Function;
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.junit.Before;
23 import org.junit.runner.RunWith;
24 import org.mockito.Mock;
25 import org.mockito.junit.MockitoJUnitRunner;
26 import org.opendaylight.mdsal.dom.api.DOMActionService;
27 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
28 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
29 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
30 import org.opendaylight.mdsal.dom.api.DOMRpcService;
31 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
32 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.restconf.api.ApiPath;
35 import org.opendaylight.restconf.common.patch.PatchContext;
36 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
37 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
38 import org.opendaylight.restconf.server.api.DataPatchPath;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
41 @RunWith(MockitoJUnitRunner.Silent.class)
42 abstract class AbstractPatchBodyTest extends AbstractInstanceIdentifierTest {
43 private final Function<InputStream, PatchBody> bodyConstructor;
46 private DOMDataBroker dataBroker;
48 DOMMountPointService mountPointService;
50 DOMMountPoint mountPoint;
52 AbstractPatchBodyTest(final Function<InputStream, PatchBody> bodyConstructor) {
53 this.bodyConstructor = requireNonNull(bodyConstructor);
57 public final void before() {
58 doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
59 doReturn(Optional.of(new FixedDOMSchemaService(IID_SCHEMA))).when(mountPoint)
60 .getService(DOMSchemaService.class);
61 doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
62 doReturn(Optional.empty()).when(mountPoint).getService(DOMActionService.class);
63 doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
64 doReturn(Optional.empty()).when(mountPoint).getService(DOMMountPointService.class);
65 doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
68 @NonNull String mountPrefix() {
72 @Nullable DOMMountPoint mountPoint() {
76 static final void checkPatchContext(final PatchContext patchContext) {
77 assertNotNull(patchContext.patchId());
78 assertNotNull(patchContext.entities());
81 final @NonNull PatchContext parse(final String prefix, final String suffix, final String patchBody)
84 if (prefix.isEmpty()) {
86 } else if (suffix.isEmpty()) {
89 uriPath = prefix + '/' + suffix;
91 final ApiPath apiPath;
93 apiPath = ApiPath.parse(uriPath);
94 } catch (ParseException e) {
95 throw new AssertionError(e);
98 final var strategy = new MdsalRestconfStrategy(IID_DATABIND, dataBroker, null, null, null, mountPointService);
99 final var stratAndPath = strategy.resolveStrategyPath(apiPath);
101 try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
102 return body.toPatchContext(new DataPatchPath(stratAndPath.strategy().databind(),
103 stratAndPath.path().instance()));