ApiDocServiceImpl.getListOfMounts should return an Object instead of JSON string
[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.google.gson.Gson;
14 import com.google.gson.GsonBuilder;
15
16 import javax.ws.rs.core.UriInfo;
17
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.dom.api.DOMMountPoint;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
24 import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
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.SchemaContext;
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 static final String INSTANCE_URL = "/nodes/node/123/";
41     private DocGenTestHelper helper;
42     private ApiDocService apiDocService;
43
44
45     @SuppressWarnings("resource")
46     @Before
47     public void setUp() throws Exception {
48         this.helper = new DocGenTestHelper();
49         this.helper.setUp();
50
51         final SchemaContext context = this.helper.createMockSchemaContext();
52         final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
53
54         final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
55         when(mountPoint.getSchemaContext()).thenReturn(context);
56
57         final DOMMountPointService service = mock(DOMMountPointService.class);
58         when(service.getMountPoint(INSTANCE_ID)).thenReturn(java.util.Optional.of(mountPoint));
59         MountPointSwaggerGeneratorDraft02 swagger =
60                 new MountPointSwaggerGeneratorDraft02(schemaService, service);
61         swagger.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
62         this.apiDocService = new ApiDocServiceImpl(
63                 swagger,
64                 new MountPointSwaggerGeneratorRFC8040(schemaService, service),
65                 new ApiDocGeneratorDraftO2(schemaService),
66                 new ApiDocGeneratorRFC8040(schemaService));
67     }
68
69     @Test
70     public void getListOfMounts() throws java.net.URISyntaxException {
71         final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
72         // simulate the behavior of GsonProvider
73         Gson gson = (new GsonBuilder()).serializeNulls()
74                 .enableComplexMapKeySerialization().create();
75         String result = gson.toJson(apiDocService.getListOfMounts(mockInfo).getEntity());
76         Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);
77     }
78 }