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