Update swagger generator to OpenAPI 3.0
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / ApiDocServiceImplTest.java
1 /*
2  * Copyright (c) 2018 ZTE Corporation 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.controller.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.core.JsonProcessingException;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15 import java.util.Optional;
16 import javax.ws.rs.core.UriInfo;
17 import org.junit.Assert;
18 import org.junit.Before;
19 import org.junit.Test;
20 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
23 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
24 import org.opendaylight.netconf.sal.rest.doc.impl.AllModulesDocGenerator;
25 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
26 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040;
27 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
28 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
29 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorRFC8040;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33
34 public class ApiDocServiceImplTest {
35     private static final String HTTP_URL = "http://localhost/path";
36     private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
37             .node(QName.create("", "nodes"))
38             .node(QName.create("", "node"))
39             .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
40     private DocGenTestHelper helper;
41     private ApiDocService apiDocService;
42
43
44     @SuppressWarnings("resource")
45     @Before
46     public void setUp() throws Exception {
47         this.helper = new DocGenTestHelper();
48         this.helper.setUp();
49
50         final EffectiveModelContext context = this.helper.createMockSchemaContext();
51         final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
52
53         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
54         when(mountPoint.getSchemaContext()).thenReturn(context);
55
56         final DOMMountPointService service = mock(DOMMountPointService.class);
57         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
58         final MountPointSwaggerGeneratorDraft02 mountPointDraft02 =
59                 new MountPointSwaggerGeneratorDraft02(schemaService, service);
60         final MountPointSwaggerGeneratorRFC8040 mountPointRFC8040 =
61                 new MountPointSwaggerGeneratorRFC8040(schemaService, service);
62         final ApiDocGeneratorDraftO2 apiDocGeneratorDraftO2 = new ApiDocGeneratorDraftO2(schemaService);
63         final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040 = new ApiDocGeneratorRFC8040(schemaService);
64         mountPointDraft02.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
65         final AllModulesDocGenerator allModulesDocGenerator = new AllModulesDocGenerator(apiDocGeneratorDraftO2,
66                 apiDocGeneratorRFC8040);
67         this.apiDocService = new ApiDocServiceImpl(mountPointDraft02, mountPointRFC8040, apiDocGeneratorDraftO2,
68                 apiDocGeneratorRFC8040, allModulesDocGenerator);
69     }
70
71     @Test
72     public void getListOfMounts() throws java.net.URISyntaxException, JsonProcessingException {
73         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
74         // simulate the behavior of JacksonJaxbJsonProvider
75         final ObjectMapper mapper = new ObjectMapper();
76         final String result = mapper.writer().writeValueAsString(
77                 apiDocService.getListOfMounts(mockInfo).getEntity());
78         Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);
79     }
80 }