*/
package org.opendaylight.netconf.sal.rest.doc.impl;
-import com.fasterxml.jackson.core.JsonFactory;
-import com.fasterxml.jackson.core.JsonGenerator;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.StandardCharsets;
-import java.util.Map.Entry;
+import java.util.List;
import java.util.Objects;
+import java.util.stream.Collectors;
+
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
+
import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger;
import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration;
+import org.opendaylight.netconf.sal.rest.doc.swagger.MountPointInstance;
import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList;
/**
@Override
public synchronized Response getListOfMounts(final UriInfo uriInfo) {
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try (OutputStreamWriter streamWriter = new OutputStreamWriter(baos, StandardCharsets.UTF_8)) {
- JsonGenerator writer = new JsonFactory().createGenerator(streamWriter);
- writer.writeStartArray();
- for (final Entry<String, Long> entry : mountPointSwaggerDraft02.getInstanceIdentifiers()
- .entrySet()) {
- writer.writeStartObject();
- writer.writeObjectField("instance", entry.getKey());
- writer.writeObjectField("id", entry.getValue());
- writer.writeEndObject();
- }
- writer.writeEndArray();
- writer.flush();
- } catch (IOException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
- }
-
- try {
- String responseStr = baos.toString(StandardCharsets.UTF_8.name());
- return Response.status(Response.Status.OK).entity(responseStr).build();
- } catch (UnsupportedEncodingException e) {
- return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
+ final MountPointSwagger mountPointSwagger;
+ if (isNew(uriInfo)) {
+ mountPointSwagger = mountPointSwaggerRFC8040;
+ } else {
+ mountPointSwagger = mountPointSwaggerDraft02;
}
+ final List<MountPointInstance> entity = mountPointSwagger
+ .getInstanceIdentifiers().entrySet().stream()
+ .map(MountPointInstance::new).collect(Collectors.toList());
+ return Response.ok(entity).build();
}
@Override
--- /dev/null
+/*
+ * Copyright (c) 2018 ZTE Corporation and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.rest.doc.impl;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+
+import javax.ws.rs.core.UriInfo;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.mdsal.dom.api.DOMMountPoint;
+import org.opendaylight.mdsal.dom.api.DOMMountPointService;
+import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.netconf.sal.rest.doc.api.ApiDocService;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorDraftO2;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocGeneratorRFC8040;
+import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl;
+import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02;
+import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorRFC8040;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class ApiDocServiceImplTest {
+ private static final String HTTP_URL = "http://localhost/path";
+ private static final YangInstanceIdentifier INSTANCE_ID = YangInstanceIdentifier.builder()
+ .node(QName.create("", "nodes"))
+ .node(QName.create("", "node"))
+ .nodeWithKey(QName.create("", "node"), QName.create("", "id"), "123").build();
+ private static final String INSTANCE_URL = "/nodes/node/123/";
+ private DocGenTestHelper helper;
+ private ApiDocService apiDocService;
+
+
+ @SuppressWarnings("resource")
+ @Before
+ public void setUp() throws Exception {
+ this.helper = new DocGenTestHelper();
+ this.helper.setUp();
+
+ final SchemaContext context = this.helper.createMockSchemaContext();
+ final DOMSchemaService schemaService = this.helper.createMockSchemaService(context);
+
+ final DOMMountPoint mountPoint = mock(DOMMountPoint.class);
+ when(mountPoint.getSchemaContext()).thenReturn(context);
+
+ final DOMMountPointService service = mock(DOMMountPointService.class);
+ when(service.getMountPoint(INSTANCE_ID)).thenReturn(java.util.Optional.of(mountPoint));
+ MountPointSwaggerGeneratorDraft02 swagger =
+ new MountPointSwaggerGeneratorDraft02(schemaService, service);
+ swagger.getMountPointSwagger().onMountPointCreated(INSTANCE_ID);
+ this.apiDocService = new ApiDocServiceImpl(
+ swagger,
+ new MountPointSwaggerGeneratorRFC8040(schemaService, service),
+ new ApiDocGeneratorDraftO2(schemaService),
+ new ApiDocGeneratorRFC8040(schemaService));
+ }
+
+ @Test
+ public void getListOfMounts() throws java.net.URISyntaxException {
+ final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL);
+ // simulate the behavior of GsonProvider
+ Gson gson = (new GsonBuilder()).serializeNulls()
+ .enableComplexMapKeySerialization().create();
+ String result = gson.toJson(apiDocService.getListOfMounts(mockInfo).getEntity());
+ Assert.assertEquals("[{\"instance\":\"/nodes/node/123/\",\"id\":1}]", result);
+ }
+}