X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=restconf%2Fsal-rest-docgen%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frest%2Fdoc%2Fimpl%2FMountPointSwaggerTest.java;h=56d3e910dc4d05d8691c0e35efc120eaf39a295c;hb=4e117e0cce9f9ac1b53ace1d9659412dddc1afa2;hp=2514e679ee10516acca23e4d8bdb51c21c8fd87f;hpb=376150894b137d05800ef3ef924d58336362d0e3;p=netconf.git diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/MountPointSwaggerTest.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/MountPointSwaggerTest.java index 2514e679ee..56d3e910dc 100644 --- a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/MountPointSwaggerTest.java +++ b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/MountPointSwaggerTest.java @@ -12,8 +12,11 @@ import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import java.util.Arrays; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Optional; import java.util.Set; import java.util.TreeSet; @@ -23,115 +26,86 @@ 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.impl.ApiDocServiceImpl.OAversion; +import org.opendaylight.netconf.sal.rest.doc.impl.ApiDocServiceImpl.URIType; import org.opendaylight.netconf.sal.rest.doc.impl.MountPointSwaggerGeneratorDraft02; import org.opendaylight.netconf.sal.rest.doc.mountpoints.MountPointSwagger; -import org.opendaylight.netconf.sal.rest.doc.swagger.Api; -import org.opendaylight.netconf.sal.rest.doc.swagger.ApiDeclaration; -import org.opendaylight.netconf.sal.rest.doc.swagger.Operation; -import org.opendaylight.netconf.sal.rest.doc.swagger.Resource; -import org.opendaylight.netconf.sal.rest.doc.swagger.ResourceList; +import org.opendaylight.netconf.sal.rest.doc.swagger.SwaggerObject; 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 MountPointSwaggerTest { +public final class MountPointSwaggerTest extends AbstractApiDocTest { 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 MountPointSwagger swagger; - private DocGenTestHelper helper; - private SchemaContext schemaContext; - @SuppressWarnings("resource") @Before - public void setUp() throws Exception { - this.helper = new DocGenTestHelper(); - this.helper.setUp(); - this.schemaContext = this.helper.getSchemaContext(); - + public void before() { // We are sharing the global schema service and the mount schema service // in our test. - // OK for testing - real thing would have seperate instances. - final SchemaContext context = this.helper.createMockSchemaContext(); - final DOMSchemaService schemaService = this.helper.createMockSchemaService(context); - + // OK for testing - real thing would have separate instances. final DOMMountPoint mountPoint = mock(DOMMountPoint.class); - when(mountPoint.getSchemaContext()).thenReturn(context); + when(mountPoint.getService(DOMSchemaService.class)).thenReturn(Optional.of(SCHEMA_SERVICE)); final DOMMountPointService service = mock(DOMMountPointService.class); when(service.getMountPoint(INSTANCE_ID)).thenReturn(Optional.of(mountPoint)); - MountPointSwaggerGeneratorDraft02 generator = new MountPointSwaggerGeneratorDraft02(schemaService, service); - this.swagger = generator.getMountPointSwagger(); - } - - @Test() - public void testGetResourceListBadIid() throws Exception { - final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL); - - assertEquals(null, this.swagger.getResourceList(mockInfo, 1L)); + swagger = new MountPointSwaggerGeneratorDraft02(SCHEMA_SERVICE, service).getMountPointSwagger(); } @Test() - public void getInstanceIdentifiers() throws Exception { - assertEquals(0, this.swagger.getInstanceIdentifiers().size()); - this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of - // mount points - assertEquals(1, this.swagger.getInstanceIdentifiers().size()); - assertEquals((Long) 1L, this.swagger.getInstanceIdentifiers().entrySet().iterator().next() + public void getInstanceIdentifiers() { + assertEquals(0, swagger.getInstanceIdentifiers().size()); + swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points + assertEquals(1, swagger.getInstanceIdentifiers().size()); + assertEquals((Long) 1L, swagger.getInstanceIdentifiers().entrySet().iterator().next() .getValue()); - assertEquals(INSTANCE_URL, this.swagger.getInstanceIdentifiers().entrySet().iterator().next() + assertEquals(INSTANCE_URL, swagger.getInstanceIdentifiers().entrySet().iterator().next() .getKey()); - this.swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount - // points - assertEquals(0, this.swagger.getInstanceIdentifiers().size()); - } - - @Test - public void testGetResourceListGoodId() throws Exception { - final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL); - this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of - // mount points - final ResourceList resourceList = this.swagger.getResourceList(mockInfo, 1L); - - Resource dataStoreResource = null; - for (final Resource r : resourceList.getApis()) { - if (r.getPath().endsWith("/Datastores(-)")) { - dataStoreResource = r; - } - } - assertNotNull("Failed to find data store resource", dataStoreResource); + swagger.onMountPointRemoved(INSTANCE_ID); // remove ID from list of mount points + assertEquals(0, swagger.getInstanceIdentifiers().size()); } @Test public void testGetDataStoreApi() throws Exception { - final UriInfo mockInfo = this.helper.createMockUriInfo(HTTP_URL); - this.swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of - // mount points + final UriInfo mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL); + swagger.onMountPointCreated(INSTANCE_ID); // add this ID into the list of mount points - final ApiDeclaration mountPointApi = this.swagger.getMountPointApi(mockInfo, 1L, "Datastores", "-"); + final SwaggerObject mountPointApi = (SwaggerObject) swagger.getMountPointApi(mockInfo, 1L, "Datastores", + "-", URIType.DRAFT02, OAversion.V2_0); assertNotNull("failed to find Datastore API", mountPointApi); - final List apis = mountPointApi.getApis(); - assertEquals("Unexpected api list size", 3, apis.size()); - - final Set actualApis = new TreeSet<>(); - for (final Api api : apis) { - actualApis.add(api.getPath()); - final List operations = api.getOperations(); - assertEquals("unexpected operation size on " + api.getPath(), 1, operations.size()); - assertEquals("unexpected operation method " + api.getPath(), "GET", operations.get(0) - .getMethod()); - assertNotNull("expected non-null desc on " + api.getPath(), operations.get(0) - .getNotes()); + + final ObjectNode pathsObject = mountPointApi.getPaths(); + assertNotNull(pathsObject); + + assertEquals("Unexpected api list size", 3, pathsObject.size()); + + final Set actualUrls = new TreeSet<>(); + + final Iterator> fields = pathsObject.fields(); + while (fields.hasNext()) { + final Map.Entry field = fields.next(); + final String path = field.getKey(); + final JsonNode operations = field.getValue(); + actualUrls.add(field.getKey()); + assertEquals("unexpected operations size on " + path, 1, operations.size()); + + final JsonNode getOperation = operations.get("get"); + + assertNotNull("unexpected operation method on " + path, getOperation); + + assertNotNull("expected non-null desc on " + path, getOperation.get("description")); } - final Set expectedApis = new TreeSet<>(Arrays.asList(new String[] { - "/restconf/config" + INSTANCE_URL + "yang-ext:mount", - "/restconf/operational" + INSTANCE_URL + "yang-ext:mount", - "/restconf/operations" + INSTANCE_URL + "yang-ext:mount",})); - assertEquals(expectedApis, actualApis); + + final Set expectedUrls = new TreeSet<>(List.of( + "/restconf/config" + INSTANCE_URL + "yang-ext:mount", + "/restconf/operational" + INSTANCE_URL + "yang-ext:mount", + "/restconf/operations" + INSTANCE_URL + "yang-ext:mount")); + assertEquals(expectedUrls, actualUrls); } }