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%2FDocGenTestHelper.java;h=0a2e939bd3689a443d80a929c68c4e534951a5f0;hb=4e117e0cce9f9ac1b53ace1d9659412dddc1afa2;hp=5265ae2669386b94239657a5070dd334c1875bcf;hpb=43b95227901a7d8aa4ed081bc11f9462f5321f9b;p=netconf.git diff --git a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/DocGenTestHelper.java b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/DocGenTestHelper.java index 5265ae2669..0a2e939bd3 100644 --- a/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/DocGenTestHelper.java +++ b/restconf/sal-rest-docgen/src/test/java/org/opendaylight/controller/sal/rest/doc/impl/DocGenTestHelper.java @@ -7,145 +7,56 @@ */ 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 com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule; -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.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.Set; import javax.ws.rs.core.UriBuilder; import javax.ws.rs.core.UriInfo; import org.mockito.ArgumentCaptor; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -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.parser.spi.meta.ReactorException; -import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangInferencePipeline; -import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.YangStatementSourceImpl; -import org.opendaylight.yangtools.yang.parser.util.NamedFileInputStream; - -public class DocGenTestHelper { - - private Set modules; - private ObjectMapper mapper; - private SchemaContext schemaContext; - - public Set loadModules(final String resourceDirectory) - throws FileNotFoundException, - URISyntaxException, ReactorException { - - 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 CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild(); - for (final String fileName : fileList) { - final File file = new File(testDir, fileName); - reactor.addSource(new YangStatementSourceImpl(new NamedFileInputStream(file, file.getPath()))); - } - - this.schemaContext = reactor.buildEffective(); - return this.schemaContext.getModules(); - } - - public Collection getModules() { - return this.modules; - } - - public void setUp() throws Exception { - this.modules = loadModules("/yang"); - this.mapper = new ObjectMapper(); - this.mapper.registerModule(new JsonOrgModule()); - this.mapper.configure(SerializationFeature.INDENT_OUTPUT, true); - } - - public SchemaContext getSchemaContext() { - return this.schemaContext; - } - - public SchemaService createMockSchemaService() { - return createMockSchemaService(null); - } - - 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 class DocGenTestHelper { - final ArgumentCaptor moduleCapture = ArgumentCaptor.forClass(String.class); - final ArgumentCaptor dateCapture = ArgumentCaptor.forClass(Date.class); - final ArgumentCaptor namespaceCapture = ArgumentCaptor.forClass(URI.class); - when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then( - new Answer() { - @Override - public Module answer(final InvocationOnMock invocation) throws Throwable { - final String module = moduleCapture.getValue(); - final Date date = dateCapture.getValue(); - for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) { - if (m.getName().equals(module) && m.getRevision().equals(date)) { - return m; - } - } - return null; - } - }); - when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then( - new Answer() { - @Override - public Module answer(final InvocationOnMock invocation) throws Throwable { - final URI namespace = namespaceCapture.getValue(); - final Date date = dateCapture.getValue(); - for (final Module m : Collections.unmodifiableSet(DocGenTestHelper.this.modules)) { - if (m.getNamespace().equals(namespace) && m.getRevision().equals(date)) { - return m; - } - } - return null; - } - }); - return mockContext; + private DocGenTestHelper() { + // hidden on purpose } - 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 subStringCapture = ArgumentCaptor.forClass(String.class); when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder); - when(mockBuilder.build()).then(new Answer() { - @Override - public URI answer(final InvocationOnMock invocation) throws Throwable { - return URI.create(uri + "/" + subStringCapture.getValue()); - } - }); + 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()); + } }