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