2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.netconf.sal.rest.doc.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
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;
19 * These tests are designed to verify that the specified path contains corresponding references in post requests
20 * which contains lists and containers.
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.
27 public class PostPayloadTest extends AbstractApiDocTest {
28 private static final String CONTENT_KEY = "content";
29 private static final String SCHEMA_KEY = "schema";
31 private static OpenApiObject containerDoc;
32 private static OpenApiObject listDoc;
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,
39 assertNotNull(containerDoc);
40 listDoc = (OpenApiObject) generator.getApiDeclaration("list-test", "2023-07-31", URI_INFO, OAversion.V3_0);
41 assertNotNull(listDoc);
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\"}}",
51 final var xmlRef1 = getXmlRef(containerDoc, path1);
52 assertEquals("#/components/schemas/container-test_cont_config_cont1_xml", xmlRef1);
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);
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);
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);
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);
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);
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();
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();