Adapt tests for OpenApi
[netconf.git] / restconf / restconf-openapi / src / test / java / org / opendaylight / restconf / openapi / impl / AbstractDocumentTest.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.openapi.impl;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import java.net.URI;
16 import java.util.Optional;
17 import javax.ws.rs.core.UriBuilder;
18 import javax.ws.rs.core.UriInfo;
19 import org.glassfish.jersey.internal.util.collection.ImmutableMultivaluedMap;
20 import org.mockito.ArgumentCaptor;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.restconf.openapi.api.OpenApiService;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
28 import org.skyscreamer.jsonassert.JSONCompareMode;
29
30 public abstract class AbstractDocumentTest {
31     protected static final ObjectMapper MAPPER = new ObjectMapper();
32     /**
33      * We want flexibility in comparing the resulting JSONs by not enforcing strict ordering of array contents.
34      * This comparison mode allows us to do that and also to restrict extensibility (extensibility = additional fields)
35      */
36     protected static final JSONCompareMode IGNORE_ORDER = JSONCompareMode.NON_EXTENSIBLE;
37     private static final String URI = "http://localhost:8181/openapi/api/v3/";
38     private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
39         .node(QName.create("", "nodes"))
40         .node(QName.create("", "node"))
41         .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
42
43     private static OpenApiService openApiService;
44
45     protected static void initializeClass(final String yangPath) {
46         final var schemaService = mock(DOMSchemaService.class);
47         final var context = YangParserTestUtils.parseYangResourceDirectory(yangPath);
48         when(schemaService.getGlobalContext()).thenReturn(context);
49
50         final var mountPoint = mock(DOMMountPoint.class);
51         when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
52
53         final var service = mock(DOMMountPointService.class);
54         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
55
56         final var mountPointRFC8040 = new MountPointOpenApiGeneratorRFC8040(schemaService, service);
57         final var openApiGeneratorRFC8040 = new OpenApiGeneratorRFC8040(schemaService);
58         mountPointRFC8040.getMountPointOpenApi().onMountPointCreated(INSTANCE_ID);
59         openApiService = new OpenApiServiceImpl(mountPointRFC8040, openApiGeneratorRFC8040);
60     }
61
62     protected static String getExpectedDoc(final String jsonPath) throws Exception {
63         return MAPPER.writeValueAsString(MAPPER.readTree(
64             AbstractDocumentTest.class.getClassLoader().getResourceAsStream(jsonPath)));
65     }
66
67     protected static String getAllModulesDoc() throws Exception {
68         final var getAllController = createMockUriInfo(URI + "single");
69         final var controllerDocAll = openApiService.getAllModulesDoc(getAllController).getEntity();
70
71         return MAPPER.writeValueAsString(controllerDocAll);
72     }
73
74     protected static String getDocByModule(final String moduleName, final String revision) throws Exception {
75         var uri = URI + moduleName;
76         if (revision != null) {
77             uri = uri + "(" + revision + ")";
78         }
79         final var getModuleController = createMockUriInfo(uri);
80         final var controllerDocModule = openApiService.getDocByModule(moduleName, revision, getModuleController);
81
82         return MAPPER.writeValueAsString(controllerDocModule.getEntity());
83     }
84
85
86     protected static String getMountDoc() throws Exception {
87         final var getAllDevice = createMockUriInfo(URI + "mounts/1");
88         when(getAllDevice.getQueryParameters()).thenReturn(ImmutableMultivaluedMap.empty());
89         final var deviceDocAll = openApiService.getMountDoc("1", getAllDevice);
90
91         return MAPPER.writeValueAsString(deviceDocAll.getEntity());
92     }
93
94
95     protected static String getMountDocByModule(final String moduleName, final String revision) throws Exception {
96         final var getDevice = createMockUriInfo(URI + "mounts/1/" + moduleName);
97         final var deviceDoc = openApiService.getMountDocByModule("1", moduleName, revision, getDevice);
98
99         return MAPPER.writeValueAsString(deviceDoc.getEntity());
100     }
101
102     public static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
103         final java.net.URI uri = new URI(urlPrefix);
104         final UriBuilder mockBuilder = mock(UriBuilder.class);
105
106         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
107         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
108         when(mockBuilder.build()).then(invocation -> java.net.URI.create(uri + "/" + subStringCapture.getValue()));
109
110         final UriInfo info = mock(UriInfo.class);
111         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
112         when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
113         when(info.getBaseUri()).thenReturn(uri);
114
115         return info;
116     }
117 }