Refactor OpenApiTestUtils methods
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / AbstractApiDocTest.java
1 /*
2  * Copyright (c) 2022 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.netconf.sal.rest.doc.impl;
9
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
12
13 import com.fasterxml.jackson.databind.node.ObjectNode;
14 import java.util.ArrayList;
15 import java.util.List;
16 import javax.ws.rs.core.UriInfo;
17 import org.junit.BeforeClass;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
20 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
21
22 public abstract class AbstractApiDocTest {
23     static EffectiveModelContext CONTEXT;
24     static DOMSchemaService SCHEMA_SERVICE;
25     static UriInfo URI_INFO;
26
27     @BeforeClass
28     public static void beforeClass() throws Exception {
29         CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/yang");
30         SCHEMA_SERVICE = mock(DOMSchemaService.class);
31         when(SCHEMA_SERVICE.getGlobalContext()).thenReturn(CONTEXT);
32         URI_INFO = DocGenTestHelper.createMockUriInfo("http://localhost/path");
33     }
34
35     protected static List<String> getPathPostParameters(final ObjectNode paths, final String path) {
36         final var params = new ArrayList<String>();
37         paths.get(path).get("post").get("parameters").elements()
38             .forEachRemaining(p -> params.add(p.get("name").asText()));
39         return params;
40     }
41
42     /**
43      * Get path parameters names for {@code path} for GET operation.
44      *
45      * @return {@link List} of parameters excluding `content` parameter
46      */
47     public static List<String> getPathGetParameters(final ObjectNode paths, final String path) {
48         final var params = new ArrayList<String>();
49         paths.get(path).get("get").get("parameters").elements()
50                 .forEachRemaining(p -> {
51                     final String name = p.get("name").asText();
52                     if (!"content".equals(name)) {
53                         params.add(name);
54                     }
55                 });
56         return params;
57     }
58 }