Re-implement logic for security and securitySchemes
[netconf.git] / restconf / restconf-openapi / src / test / java / org / opendaylight / restconf / openapi / impl / KeysMappingTest.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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15 import static org.opendaylight.restconf.openapi.OpenApiTestUtils.getPathGetParameters;
16
17 import java.util.List;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
21 import org.opendaylight.restconf.openapi.DocGenTestHelper;
22 import org.opendaylight.restconf.openapi.model.OpenApiObject;
23 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
24
25 public class KeysMappingTest {
26     private static OpenApiObject doc;
27
28     @BeforeClass
29     public static void startUp() throws Exception {
30         final var context = YangParserTestUtils.parseYang("""
31             module keys-mapping {
32               namespace "mapping";
33               prefix keys-mapping;
34                 list multiple-key-list {
35                   key "name name2";
36                   leaf name {
37                     type string;
38                   }
39                   leaf name2 {
40                     type string;
41                   }
42                   list multiple-key-list2 {
43                     key "name name3";
44                     leaf name {
45                       type string;
46                     }
47                     leaf name3 {
48                       type string;
49                     }
50                     list multiple-key-list3 {
51                       key "name3 name";
52                       leaf name3 {
53                         type string;
54                       }
55                       leaf name {
56                         type string;
57                       }
58                       list multiple-key-list4 {
59                         key name;
60                         leaf name {
61                           type string;
62                         }
63                       }
64                     }
65                   }
66                 }
67               }""");
68         final var schemaService = mock(DOMSchemaService.class);
69         when(schemaService.getGlobalContext()).thenReturn(context);
70         final var generator = new OpenApiGeneratorRFC8040(schemaService);
71         final var uriInfo = DocGenTestHelper.createMockUriInfo("http://localhost/path");
72         doc = generator.getApiDeclaration("keys-mapping", null, uriInfo);
73         assertNotNull(doc);
74     }
75
76     /**
77      * This test is designed to verify if the request parameters for nested lists with multiple keys are being
78      * enumerated properly.
79      *
80      * <p>
81      * This would mean that we will have name, name1, etc., when the same parameter appears multiple times in the path.
82      */
83     @Test
84     public void testKeysMapping() {
85         final var pathToMultipleKeyList4 = "/rests/data/keys-mapping:multiple-key-list={name},{name2}"
86             + "/multiple-key-list2={name1},{name3}/multiple-key-list3={name31},{name4}/multiple-key-list4={name5}";
87         assertTrue(doc.paths().containsKey(pathToMultipleKeyList4));
88         assertEquals(List.of("name","name2", "name1", "name3", "name31", "name4", "name5"),
89             getPathGetParameters(doc.paths(), pathToMultipleKeyList4));
90     }
91 }