Bump upstreams
[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.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;
40
41 @RunWith(MockitoJUnitRunner.Silent.class)
42 abstract class AbstractPatchBodyTest extends AbstractInstanceIdentifierTest {
43     private final Function<InputStream, PatchBody> bodyConstructor;
44
45     @Mock
46     private DOMDataBroker dataBroker;
47     @Mock
48     DOMMountPointService mountPointService;
49     @Mock
50     DOMMountPoint mountPoint;
51
52     AbstractPatchBodyTest(final Function<InputStream, PatchBody> bodyConstructor) {
53         this.bodyConstructor = requireNonNull(bodyConstructor);
54     }
55
56     @Before
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);
66     }
67
68     @NonNull String mountPrefix() {
69         return "";
70     }
71
72     @Nullable DOMMountPoint mountPoint() {
73         return null;
74     }
75
76     static final void checkPatchContext(final PatchContext patchContext) {
77         assertNotNull(patchContext.patchId());
78         assertNotNull(patchContext.entities());
79     }
80
81     final @NonNull PatchContext parse(final String prefix, final String suffix, final String patchBody)
82             throws IOException {
83         final String uriPath;
84         if (prefix.isEmpty()) {
85             uriPath = suffix;
86         } else if (suffix.isEmpty()) {
87             uriPath = prefix;
88         } else {
89             uriPath = prefix + '/' + suffix;
90         }
91         final ApiPath apiPath;
92         try {
93             apiPath = ApiPath.parse(uriPath);
94         } catch (ParseException e) {
95             throw new AssertionError(e);
96         }
97
98         final var strategy = new MdsalRestconfStrategy(IID_DATABIND, dataBroker, null, null, null, mountPointService);
99         final var stratAndPath = strategy.resolveStrategyPath(apiPath);
100
101         try (var body = bodyConstructor.apply(stringInputStream(patchBody))) {
102             return body.toPatchContext(new DataPatchPath(stratAndPath.strategy().databind(),
103                 stratAndPath.path().instance()));
104         }
105     }
106 }