2 * Copyright (c) 2018 ZTE Corporation 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.controller.sal.rest.doc.impl;
10 import static org.mockito.Mockito.mock;
11 import static org.mockito.Mockito.when;
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.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;
33 public class ApiDocServiceImplTest {
34 private static final String HTTP_URL = "http://localhost/path";
35 private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
36 .node(QName.create("", "nodes"))
37 .node(QName.create("", "node"))
38 .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
39 private static final String INSTANCE_URL = "/nodes/node/123/";
40 private DocGenTestHelper helper;
41 private ApiDocService apiDocService;
44 @SuppressWarnings("resource")
46 public void setUp() throws Exception {
47 this.helper = new DocGenTestHelper();
50 final EffectiveModelContext context = this.helper.createMockSchemaContext();
51 final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
53 final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
54 when(mountPoint.getSchemaContext()).thenReturn(context);
56 final DOMMountPointService service = mock(DOMMountPointService.class);
57 when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint));
58 MountPointSwaggerGeneratorDraft02 swagger =
59 new MountPointSwaggerGeneratorDraft02(schemaService, service);
60 swagger.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
61 this.apiDocService = new ApiDocServiceImpl(
63 new MountPointSwaggerGeneratorRFC8040(schemaService, service),
64 new ApiDocGeneratorDraftO2(schemaService),
65 new ApiDocGeneratorRFC8040(schemaService));
69 public void getListOfMounts() throws java.net.URISyntaxException, JsonProcessingException {
70 final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
71 // simulate the behavior of JacksonJaxbJsonProvider
72 ObjectMapper mapper = new ObjectMapper();
73 String result = mapper.writer().writeValueAsString(
74 apiDocService.getListOfMounts(mockInfo).getEntity());
75 Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);