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 com.google.common.collect.ImmutableMap;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.text.ParseException;
19 import java.util.Optional;
20 import java.util.function.Function;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.Before;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.mdsal.dom.api.DOMActionService;
28 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
29 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
30 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
31 import org.opendaylight.mdsal.dom.api.DOMRpcService;
32 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
33 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
34 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
35 import org.opendaylight.restconf.api.ApiPath;
36 import org.opendaylight.restconf.common.patch.PatchContext;
37 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
38 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
39 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy.StrategyAndPath;
40 import org.opendaylight.restconf.server.api.PatchBody;
41 import org.opendaylight.restconf.server.api.ServerException;
42 import org.opendaylight.restconf.server.spi.DefaultResourceContext;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
45 @RunWith(MockitoJUnitRunner.Silent.class)
46 abstract class AbstractPatchBodyTest extends AbstractInstanceIdentifierTest {
47 private final Function<InputStream, PatchBody> bodyConstructor;
50 private DOMDataBroker dataBroker;
52 DOMMountPointService mountPointService;
54 DOMMountPoint mountPoint;
56 AbstractPatchBodyTest(final Function<InputStream, PatchBody> bodyConstructor) {
57 this.bodyConstructor = requireNonNull(bodyConstructor);
61 public final void before() {
62 doReturn(Optional.of(mountPoint)).when(mountPointService).getMountPoint(any(YangInstanceIdentifier.class));
63 doReturn(Optional.of(new FixedDOMSchemaService(IID_SCHEMA))).when(mountPoint)
64 .getService(DOMSchemaService.class);
65 doReturn(Optional.of(dataBroker)).when(mountPoint).getService(DOMDataBroker.class);
66 doReturn(Optional.empty()).when(mountPoint).getService(DOMActionService.class);
67 doReturn(Optional.empty()).when(mountPoint).getService(DOMRpcService.class);
68 doReturn(Optional.empty()).when(mountPoint).getService(DOMMountPointService.class);
69 doReturn(Optional.empty()).when(mountPoint).getService(NetconfDataTreeService.class);
72 @NonNull String mountPrefix() {
76 @Nullable DOMMountPoint mountPoint() {
80 static final void checkPatchContext(final PatchContext patchContext) {
81 assertNotNull(patchContext.patchId());
82 assertNotNull(patchContext.entities());
85 final @NonNull PatchContext parse(final String prefix, final String suffix, final String patchBody)
88 if (prefix.isEmpty()) {
90 } else if (suffix.isEmpty()) {
93 uriPath = prefix + '/' + suffix;
95 final ApiPath apiPath;
97 apiPath = ApiPath.parse(uriPath);
98 } catch (ParseException e) {
99 throw new AssertionError(e);
102 final var strategy = new MdsalRestconfStrategy(IID_DATABIND, dataBroker, ImmutableMap.of(), null, null, null,
104 final StrategyAndPath stratAndPath;
106 stratAndPath = strategy.resolveStrategyPath(apiPath);
107 } catch (ServerException e) {
108 throw new AssertionError(e);
111 try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
112 return body.toPatchContext(new DefaultResourceContext(stratAndPath.path()));