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.jaxrs;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertInstanceOf;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
18 import java.text.ParseException;
19 import java.util.List;
20 import java.util.function.Consumer;
21 import javax.ws.rs.container.AsyncResponse;
22 import javax.ws.rs.core.Response;
23 import javax.ws.rs.core.UriInfo;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.extension.ExtendWith;
27 import org.mockito.ArgumentCaptor;
28 import org.mockito.Mock;
29 import org.mockito.junit.jupiter.MockitoExtension;
30 import org.opendaylight.mdsal.dom.api.DOMActionService;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
33 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
34 import org.opendaylight.mdsal.dom.api.DOMRpcService;
35 import org.opendaylight.restconf.api.ApiPath;
36 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
37 import org.opendaylight.restconf.common.errors.RestconfError;
38 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
39 import org.opendaylight.restconf.nb.rfc8040.databind.DatabindContext;
40 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
41 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 // FIXME: hide this class
46 @ExtendWith(MockitoExtension.class)
47 public abstract class AbstractRestconfTest extends AbstractJukeboxTest {
48 static final ApiPath JUKEBOX_API_PATH = apiPath("example-jukebox:jukebox");
53 DOMDataBroker dataBroker;
55 DOMActionService actionService;
57 DOMRpcService rpcService;
59 DOMMountPointService mountPointService;
61 DOMMountPoint mountPoint;
63 JaxRsRestconf restconf;
66 final void setupRestconf() {
67 restconf = new JaxRsRestconf(new MdsalRestconfServer(() -> DatabindContext.ofModel(modelContext()), dataBroker,
68 rpcService, actionService, mountPointService));
71 EffectiveModelContext modelContext() {
72 return JUKEBOX_SCHEMA;
75 static final NormalizedNode assertNormalizedNode(final int status, final Consumer<AsyncResponse> invocation) {
76 return assertEntity(NormalizedNodePayload.class, status, invocation).data();
79 // FIXME: hide this method
80 public static final <T> T assertEntity(final Class<T> expectedType, final int expectedStatus,
81 final Consumer<AsyncResponse> invocation) {
82 return assertInstanceOf(expectedType, assertEntity(expectedStatus, invocation));
85 static final Object assertEntity(final int expectedStatus, final Consumer<AsyncResponse> invocation) {
86 return assertResponse(expectedStatus, invocation).getEntity();
89 // FIXME: hide this method
90 public static final RestconfError assertError(final Consumer<AsyncResponse> invocation) {
91 final var errors = assertErrors(invocation);
92 assertEquals(1, errors.size());
93 final var error = errors.get(0);
98 // FIXME: hide this method
99 public static final List<RestconfError> assertErrors(final Consumer<AsyncResponse> invocation) {
100 final var ar = mock(AsyncResponse.class);
101 final var captor = ArgumentCaptor.forClass(RestconfDocumentedException.class);
102 doReturn(true).when(ar).resume(captor.capture());
103 invocation.accept(ar);
104 verify(ar).resume(any(RestconfDocumentedException.class));
105 return captor.getValue().getErrors();
108 static final Response assertResponse(final int expectedStatus, final Consumer<AsyncResponse> invocation) {
109 final var ar = mock(AsyncResponse.class);
110 final var captor = ArgumentCaptor.forClass(Response.class);
111 doReturn(true).when(ar).resume(captor.capture());
112 invocation.accept(ar);
113 verify(ar).resume(any(Response.class));
114 final var response = captor.getValue();
115 assertEquals(expectedStatus, response.getStatus());
119 static final @NonNull ApiPath apiPath(final String str) {
121 return ApiPath.parse(str);
122 } catch (ParseException e) {
123 throw new AssertionError(e);