Fix docgen for action types
[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.databind.ObjectMapper;
14 import java.util.Optional;
15 import javax.ws.rs.core.UriInfo;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
20 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
23 import org.opendaylight.netconf.sal.rest.doc.impl.AllModulesDocGenerator;
24 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
25 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040;
26 import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
27 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
28 import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorRFC8040;
29 import org.opendaylight.yangtools.yang.common.QName;
30 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33
34 public final 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
41     private ApiDocService apiDocService;
42
43     @Before
44     public void setUp() {
45         final EffectiveModelContext context = YangParserTestUtils.parseYangResourceDirectory("/yang");
46         final DOMSchemaService schemaService = DocGenTestHelper.createMockSchemaService(context);
47
48         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
49         when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(schemaService));
50
51         final DOMMountPointService service = mock(DOMMountPointService.class);
52         when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
53         final MountPointSwaggerGeneratorDraft02 mountPointDraft02 =
54                 new MountPointSwaggerGeneratorDraft02(schemaService, service);
55         final MountPointSwaggerGeneratorRFC8040 mountPointRFC8040 =
56                 new MountPointSwaggerGeneratorRFC8040(schemaService, service);
57         final ApiDocGeneratorDraftO2 apiDocGeneratorDraftO2 = new ApiDocGeneratorDraftO2(schemaService);
58         final ApiDocGeneratorRFC8040 apiDocGeneratorRFC8040 = new ApiDocGeneratorRFC8040(schemaService);
59         mountPointDraft02.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
60         final AllModulesDocGenerator allModulesDocGenerator = new AllModulesDocGenerator(apiDocGeneratorDraftO2,
61                 apiDocGeneratorRFC8040);
62         apiDocService = new ApiDocServiceImpl(mountPointDraft02, mountPointRFC8040, apiDocGeneratorDraftO2,
63                 apiDocGeneratorRFC8040, allModulesDocGenerator);
64     }
65
66     @Test
67     public void getListOfMounts() throws Exception {
68         final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
69         // simulate the behavior of JacksonJaxbJsonProvider
70         final ObjectMapper mapper = new ObjectMapper();
71         final String result = mapper.writer().writeValueAsString(apiDocService.getListOfMounts(mockInfo).getEntity());
72         Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);
73     }
74 }