Introduce HttpStatusCode
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / jaxrs / AbstractRestconfTest.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.jaxrs;
9
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;
17
18 import java.io.ByteArrayOutputStream;
19 import java.io.IOException;
20 import java.nio.charset.StandardCharsets;
21 import java.text.ParseException;
22 import java.util.List;
23 import java.util.function.Consumer;
24 import javax.ws.rs.container.AsyncResponse;
25 import javax.ws.rs.core.Response;
26 import javax.ws.rs.core.UriInfo;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.extension.ExtendWith;
30 import org.mockito.ArgumentCaptor;
31 import org.mockito.Mock;
32 import org.mockito.junit.jupiter.MockitoExtension;
33 import org.opendaylight.mdsal.dom.api.DOMActionService;
34 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
35 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
36 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
37 import org.opendaylight.mdsal.dom.api.DOMRpcService;
38 import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
39 import org.opendaylight.restconf.api.ApiPath;
40 import org.opendaylight.restconf.api.FormatParameters;
41 import org.opendaylight.restconf.api.FormattableBody;
42 import org.opendaylight.restconf.api.query.PrettyPrintParam;
43 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
44 import org.opendaylight.restconf.common.errors.RestconfError;
45 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
46 import org.opendaylight.restconf.nb.rfc8040.ErrorTagMapping;
47 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
48 import org.opendaylight.restconf.server.mdsal.MdsalDatabindProvider;
49 import org.opendaylight.restconf.server.mdsal.MdsalRestconfServer;
50 import org.opendaylight.restconf.server.spi.OperationOutputBody;
51 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
52 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
53 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
54
55 @ExtendWith(MockitoExtension.class)
56 abstract class AbstractRestconfTest extends AbstractJukeboxTest {
57     static final ApiPath JUKEBOX_API_PATH = apiPath("example-jukebox:jukebox");
58
59     @Mock
60     UriInfo uriInfo;
61     @Mock
62     DOMDataBroker dataBroker;
63     @Mock
64     DOMActionService actionService;
65     @Mock
66     DOMRpcService rpcService;
67     @Mock
68     DOMMountPointService mountPointService;
69     @Mock
70     DOMMountPoint mountPoint;
71
72     JaxRsRestconf restconf;
73
74     @BeforeEach
75     final void setupRestconf() {
76         restconf = new JaxRsRestconf(
77             new MdsalRestconfServer(new MdsalDatabindProvider(new FixedDOMSchemaService(modelContext())),
78                 dataBroker, rpcService, actionService, mountPointService),
79             ErrorTagMapping.RFC8040, PrettyPrintParam.FALSE);
80     }
81
82     EffectiveModelContext modelContext() {
83         return JUKEBOX_SCHEMA;
84     }
85
86     static final void assertJson(final String expectedJson, final OperationOutputBody payload) {
87         final var baos = new ByteArrayOutputStream();
88         try {
89             payload.formatToJSON(FormatParameters.COMPACT, baos);
90         } catch (IOException e) {
91             throw new AssertionError(e);
92         }
93         assertEquals(expectedJson, baos.toString(StandardCharsets.UTF_8));
94     }
95
96     static final void assertXml(final String expectedXml, final OperationOutputBody payload) {
97         final var baos = new ByteArrayOutputStream();
98         try {
99             payload.formatToXML(FormatParameters.COMPACT, baos);
100         } catch (IOException e) {
101             throw new AssertionError(e);
102         }
103         assertEquals(expectedXml, baos.toString(StandardCharsets.UTF_8));
104     }
105
106     static final FormattableBody assertFormattableBody(final int status, final Consumer<AsyncResponse> invocation) {
107         return assertEntity(JaxRsFormattableBody.class, status, invocation).body();
108     }
109
110     static final ContainerNode assertOperationOutput(final int status, final Consumer<AsyncResponse> invocation) {
111         return assertOperationOutputBody(status, invocation).output();
112     }
113
114     static final OperationOutputBody assertOperationOutputBody(final int status,
115             final Consumer<AsyncResponse> invocation) {
116         return assertEntity(OperationOutputBody.class, status, invocation);
117     }
118
119     static final NormalizedNode assertNormalizedNode(final int status, final Consumer<AsyncResponse> invocation) {
120         return assertNormalizedNodePayload(status, invocation).data();
121     }
122
123     static final NormalizedNodePayload assertNormalizedNodePayload(final int status,
124             final Consumer<AsyncResponse> invocation) {
125         return assertEntity(NormalizedNodePayload.class, status, invocation);
126     }
127
128     static final <T> T assertEntity(final Class<T> expectedType, final int expectedStatus,
129             final Consumer<AsyncResponse> invocation) {
130         return assertInstanceOf(expectedType, assertEntity(expectedStatus, invocation));
131     }
132
133     static final Object assertEntity(final int expectedStatus, final Consumer<AsyncResponse> invocation) {
134         return assertResponse(expectedStatus, invocation).getEntity();
135     }
136
137     static final RestconfError assertError(final Consumer<AsyncResponse> invocation) {
138         final var errors = assertErrors(invocation);
139         assertEquals(1, errors.size());
140         final var error = errors.get(0);
141         assertNotNull(error);
142         return error;
143     }
144
145     static final List<RestconfError> assertErrors(final Consumer<AsyncResponse> invocation) {
146         final var ar = mock(AsyncResponse.class);
147         doReturn(true).when(ar).resume(any(RestconfDocumentedException.class));
148
149         invocation.accept(ar);
150
151         final var captor = ArgumentCaptor.forClass(RestconfDocumentedException.class);
152         verify(ar).resume(captor.capture());
153         return captor.getValue().getErrors();
154     }
155
156     static final Response assertResponse(final int expectedStatus, final Consumer<AsyncResponse> invocation) {
157         final var ar = mock(AsyncResponse.class);
158         doReturn(true).when(ar).resume(any(Response.class));
159
160         invocation.accept(ar);
161
162         final var captor = ArgumentCaptor.forClass(Response.class);
163         verify(ar).resume(captor.capture());
164         final var response = captor.getValue();
165         assertEquals(expectedStatus, response.getStatus());
166         return response;
167     }
168
169     static final @NonNull ApiPath apiPath(final String str) {
170         try {
171             return ApiPath.parse(str);
172         } catch (ParseException e) {
173             throw new AssertionError(e);
174         }
175     }
176 }