Modernize sal-rest-docgen tests a bit
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / DocGenTestHelper.java
index 9cd7ae780de9462b8958df8cb17bd8b5321ba249..0a2e939bd3689a443d80a929c68c4e534951a5f0 100644 (file)
  */
 package org.opendaylight.controller.sal.rest.doc.impl;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.fasterxml.jackson.databind.SerializationFeature;
-import java.io.File;
-import java.io.FileNotFoundException;
+import com.fasterxml.jackson.databind.JsonNode;
 import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.List;
-import java.util.Optional;
-import java.util.Set;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 import org.mockito.ArgumentCaptor;
-import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-public class DocGenTestHelper {
+final class DocGenTestHelper {
 
-    private Set<Module> modules;
-    private ObjectMapper mapper;
-    private SchemaContext schemaContext;
-
-    public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
-
-        final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
-        final File testDir = new File(resourceDirUri);
-        final String[] fileList = testDir.list();
-        if (fileList == null) {
-            throw new FileNotFoundException(resourceDirectory.toString());
-        }
-        final List<File> files = new ArrayList<>();
-        for (final String fileName : fileList) {
-            files.add(new File(testDir, fileName));
-        }
-
-        this.schemaContext = YangParserTestUtils.parseYangFiles(files);
-        return this.schemaContext.getModules();
-    }
-
-    public Collection<Module> getModules() {
-        return this.modules;
-    }
-
-    public void setUp() throws Exception {
-        this.modules = loadModules("/yang");
-        this.mapper = new ObjectMapper();
-        this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
+    private DocGenTestHelper() {
+        // hidden on purpose
     }
 
-    public SchemaContext getSchemaContext() {
-        return this.schemaContext;
-    }
-
-    public SchemaService createMockSchemaService(SchemaContext mockContext) {
-        if (mockContext == null) {
-            mockContext = createMockSchemaContext();
-        }
-
-        final SchemaService mockSchemaService = mock(SchemaService.class);
-        when(mockSchemaService.getGlobalContext()).thenReturn(mockContext);
-        return mockSchemaService;
-    }
-
-    public SchemaContext createMockSchemaContext() {
-        final SchemaContext mockContext = mock(SchemaContext.class);
-        when(mockContext.getModules()).thenReturn(this.modules);
-
-        final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
-        final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
-        final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
-        when(mockContext.findModule(moduleCapture.capture(), dateCapture.capture())).then(
-            invocation -> {
-                final String module = moduleCapture.getValue();
-                final Optional<?> date = dateCapture.getValue();
-                for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
-                    if (m.getName().equals(module) && m.getRevision().equals(date)) {
-                        return Optional.of(m);
-                    }
-                }
-                return Optional.empty();
-            });
-        when(mockContext.findModule(namespaceCapture.capture(), dateCapture.capture())).then(
-            invocation -> {
-                final URI namespace = namespaceCapture.getValue();
-                final Optional<?> date = dateCapture.getValue();
-                for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) {
-                    if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) {
-                        return Optional.of(m);
-                    }
-                }
-                return Optional.empty();
-            });
-        return mockContext;
-    }
-
-    public UriInfo createMockUriInfo(final String urlPrefix) throws URISyntaxException {
+    static UriInfo createMockUriInfo(final String urlPrefix) throws Exception {
         final URI uri = new URI(urlPrefix);
-
         final UriBuilder mockBuilder = mock(UriBuilder.class);
 
         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
@@ -119,10 +34,29 @@ public class DocGenTestHelper {
         when(mockBuilder.build()).then(invocation -> URI.create(uri + "/" + subStringCapture.getValue()));
 
         final UriInfo info = mock(UriInfo.class);
-
         when(info.getRequestUriBuilder()).thenReturn(mockBuilder);
+        when(mockBuilder.replaceQuery(any())).thenReturn(mockBuilder);
         when(info.getBaseUri()).thenReturn(uri);
+
         return info;
     }
 
+    /**
+     * Checks whether object {@code mainObject} contains in properties/items key $ref with concrete value.
+     */
+    static void containsReferences(final JsonNode mainObject, final String childObject, final String expectedRef) {
+        final JsonNode properties = mainObject.get("properties");
+        assertNotNull(properties);
+
+        final JsonNode childNode = properties.get(childObject);
+        assertNotNull(childNode);
+
+        //list case
+        JsonNode refWrapper = childNode.get("items");
+        if (refWrapper == null) {
+            //container case
+            refWrapper = childNode;
+        }
+        assertEquals(expectedRef, refWrapper.get("$ref").asText());
+    }
 }