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=a8196dac47d562035afaa33cfa15d626e9e8c122;hpb=9bbc46b835a5829afd2070e0a2c463adc23b1614;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 a8196dac47..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,141 +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 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.Date; -import java.util.List; -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.test.util.YangParserTestUtils; -public class DocGenTestHelper { +final class DocGenTestHelper { - private Set modules; - private ObjectMapper mapper; - private SchemaContext schemaContext; - - public Set loadModules(final String resourceDirectory) - throws URISyntaxException, FileNotFoundException, 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 List files = new ArrayList<>(); - for (final String fileName : fileList) { - files.add(new File(testDir, fileName)); - } - - this.schemaContext = YangParserTestUtils.parseYangSources(files); - return this.schemaContext.getModules(); - } - - public Collection getModules() { - return this.modules; + private DocGenTestHelper() { + // hidden on purpose } - public void setUp() throws Exception { - this.modules = loadModules("/yang"); - this.mapper = new ObjectMapper(); - 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 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; - } - - 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()); + } }