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