Adjust to yangtools-2.0.0/odlparent-3.0.0 changes
[netconf.git] / restconf / sal-rest-docgen / src / test / java / org / opendaylight / controller / sal / rest / doc / impl / DocGenTestHelper.java
index 5265ae2669386b94239657a5070dd334c1875bcf..1c48480a830070f0aac00c49fc30dfb4db1053f0 100644 (file)
@@ -9,30 +9,26 @@ package org.opendaylight.controller.sal.rest.doc.impl;
 
 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 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.Optional;
 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;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public class DocGenTestHelper {
 
@@ -40,9 +36,7 @@ public class DocGenTestHelper {
     private ObjectMapper mapper;
     private SchemaContext schemaContext;
 
-    public Set<Module> loadModules(final String resourceDirectory)
-            throws FileNotFoundException,
-            URISyntaxException, ReactorException {
+    public Set<Module> loadModules(final String resourceDirectory) throws URISyntaxException, FileNotFoundException {
 
         final URI resourceDirUri = getClass().getResource(resourceDirectory).toURI();
         final File testDir = new File(resourceDirUri);
@@ -50,13 +44,12 @@ public class DocGenTestHelper {
         if (fileList == null) {
             throw new FileNotFoundException(resourceDirectory.toString());
         }
-        final CrossSourceStatementReactor.BuildAction reactor = YangInferencePipeline.RFC6020_REACTOR.newBuild();
+        final List<File> files = new ArrayList<>();
         for (final String fileName : fileList) {
-            final File file = new File(testDir, fileName);
-            reactor.addSource(new YangStatementSourceImpl(new NamedFileInputStream(file, file.getPath())));
+            files.add(new File(testDir, fileName));
         }
 
-        this.schemaContext = reactor.buildEffective();
+        this.schemaContext = YangParserTestUtils.parseYangFiles(files);
         return this.schemaContext.getModules();
     }
 
@@ -67,7 +60,6 @@ public class DocGenTestHelper {
     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);
     }
 
@@ -94,36 +86,30 @@ public class DocGenTestHelper {
         when(mockContext.getModules()).thenReturn(this.modules);
 
         final ArgumentCaptor<String> moduleCapture = ArgumentCaptor.forClass(String.class);
-        final ArgumentCaptor<Date> dateCapture = ArgumentCaptor.forClass(Date.class);
+        final ArgumentCaptor<Optional> dateCapture = ArgumentCaptor.forClass(Optional.class);
         final ArgumentCaptor<URI> namespaceCapture = ArgumentCaptor.forClass(URI.class);
-        when(mockContext.findModuleByName(moduleCapture.capture(), dateCapture.capture())).then(
-                new Answer<Module>() {
-                    @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.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);
                     }
-                });
-        when(mockContext.findModuleByNamespaceAndRevision(namespaceCapture.capture(), dateCapture.capture())).then(
-                new Answer<Module>() {
-                    @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 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;
     }
 
@@ -134,12 +120,7 @@ public class DocGenTestHelper {
 
         final ArgumentCaptor<String> subStringCapture = ArgumentCaptor.forClass(String.class);
         when(mockBuilder.path(subStringCapture.capture())).thenReturn(mockBuilder);
-        when(mockBuilder.build()).then(new Answer<URI>() {
-            @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);