Fix logic of POST schema reference creation
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / netconf / sal / rest / doc / impl / PostPayloadTest.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.netconf.sal.rest.doc.impl;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.OAversion;
16 import org.opendaylight.netconf.sal.rest.doc.swagger.OpenApiObject;
17
18 /**
19  * These tests are designed to verify that the specified path contains corresponding references in post requests
20  * which contains lists and containers.
21  *
22  * <p>
23  * The purpose of this test is to ensure that the specified path in the document contains the necessary references
24  * in its post requests for both JSON and XML content types. It verifies that the expected structure is in place,
25  * and the application can successfully retrieve the corresponding references for further processing.
26  */
27 public class PostPayloadTest extends AbstractApiDocTest {
28     private static final String CONTENT_KEY = "content";
29     private static final String SCHEMA_KEY = "schema";
30
31     private static OpenApiObject containerDoc;
32     private static OpenApiObject listDoc;
33
34     @BeforeClass
35     public static void startUp() {
36         final var generator = new ApiDocGeneratorRFC8040(SCHEMA_SERVICE);
37         containerDoc = (OpenApiObject) generator.getApiDeclaration("container-test", "2023-07-31", URI_INFO,
38             OAversion.V3_0);
39         assertNotNull(containerDoc);
40         listDoc = (OpenApiObject) generator.getApiDeclaration("list-test", "2023-07-31", URI_INFO, OAversion.V3_0);
41         assertNotNull(listDoc);
42     }
43
44     @Test
45     public void testContainersPostPayloads() {
46         final var path1 = "/rests/data/container-test:cont";
47         assertNotNull(containerDoc.getPaths().get(path1));
48         final var jsonRef1 = getJsonRef(containerDoc, path1);
49         assertEquals("{\"cont1\":{\"$ref\":\"#/components/schemas/container-test_cont_config_cont1\"}}",
50             jsonRef1);
51         final var xmlRef1 = getXmlRef(containerDoc, path1);
52         assertEquals("#/components/schemas/container-test_cont_config_cont1_xml", xmlRef1);
53
54         final var path2 = "/rests/data/container-test:cont/cont1";
55         assertNotNull(containerDoc.getPaths().get(path2));
56         final var jsonRef2 = getJsonRef(containerDoc, path2);
57         assertEquals("{\"list4\":{\"type\":\"array\",\"items\":{\"$ref\":\""
58             + "#/components/schemas/container-test_cont_cont1_config_list4\"}}}", jsonRef2);
59         final var xmlRef2 = getXmlRef(containerDoc, path2);
60         assertEquals("#/components/schemas/container-test_cont_cont1_config_list4_xml", xmlRef2);
61
62         final var path3 = "/rests/data/container-test:cont/cont1/list4={key4}";
63         assertNotNull(containerDoc.getPaths().get(path3));
64         final var jsonRef3 = getJsonRef(containerDoc, path3);
65         assertEquals("{\"cont2\":{\"$ref\":\"#/components/schemas/"
66                 + "container-test_cont_cont1_list4_config_cont2\"}}", jsonRef3);
67         final var xmlRef3 = getXmlRef(containerDoc, path3);
68         assertEquals("#/components/schemas/container-test_cont_cont1_list4_config_cont2_xml", xmlRef3);
69
70         final var path4 = "/rests/data/container-test:cont/cont1/list4={key4}/cont2";
71         assertNotNull(containerDoc.getPaths().get(path4));
72         final var jsonRef4 = getJsonRef(containerDoc, path4);
73         assertEquals("{\"list5\":{\"type\":\"array\",\"items\":{\"$ref\":\""
74                 + "#/components/schemas/container-test_cont_cont1_list4_cont2_config_list5\"}}}", jsonRef4);
75         final var xmlRef4 = getXmlRef(containerDoc, path4);
76         assertEquals("#/components/schemas/container-test_cont_cont1_list4_cont2_config_list5_xml", xmlRef4);
77     }
78
79     @Test
80     public void testListsPostPayloads() {
81         final var path1 = "/rests/data/list-test:cont";
82         assertNotNull(listDoc.getPaths().get(path1));
83         final var jsonRef1 = getJsonRef(listDoc, path1);
84         assertEquals("{\"list1\":{\"type\":\"array\",\"items\":{\"$ref\":\""
85             + "#/components/schemas/list-test_cont_config_list1\"}}}", jsonRef1);
86         final var xmlRef1 = getXmlRef(listDoc, path1);
87         assertEquals("#/components/schemas/list-test_cont_config_list1_xml", xmlRef1);
88
89         final var path2 = "/rests/data/list-test:cont/list2={key2}";
90         assertNotNull(listDoc.getPaths().get(path2));
91         final var jsonRef2 = getJsonRef(listDoc, path2);
92         assertEquals("{\"list3\":{\"type\":\"array\",\"items\":{\"$ref\":\""
93             + "#/components/schemas/list-test_cont_list2_config_list3\"}}}", jsonRef2);
94         final var xmlRef2 = getXmlRef(listDoc, path2);
95         assertEquals("#/components/schemas/list-test_cont_list2_config_list3_xml", xmlRef2);
96     }
97
98     private static String getJsonRef(final OpenApiObject openApiObject, final String path) {
99         return openApiObject.getPaths().get(path).get("post").get("requestBody").get(CONTENT_KEY)
100             .get("application/json").get(SCHEMA_KEY).get("properties").toString();
101     }
102
103     private static String getXmlRef(final OpenApiObject openApiObject, final String path) {
104         return openApiObject.getPaths().get(path).get("post").get("requestBody").get(CONTENT_KEY)
105             .get("application/xml").get(SCHEMA_KEY).get("$ref").asText();
106     }
107 }