This is a simplistic conversion, also modernizing Java constructs used.
Change-Id: Iaa10e3caec078ed3145bcc1dd9f4e0d8f8c30206
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
*/
package org.opendaylight.yangtools.yang.model.export;
-import static org.junit.Assert.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
-import java.util.Collection;
import javax.xml.stream.XMLStreamException;
import org.custommonkey.xmlunit.Diff;
import org.custommonkey.xmlunit.ElementNameAndAttributeQualifier;
abstract class AbstractYinExportTest {
final void exportYinModules(final String yangDir, final String yinDir) throws IOException, SAXException,
XMLStreamException {
- final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResourceDirectory(yangDir);
- final Collection<? extends Module> modules = schemaContext.getModules();
+ final var schemaContext = YangParserTestUtils.parseYangResourceDirectory(yangDir);
+ final var modules = schemaContext.getModules();
assertNotEquals(0, modules.size());
- for (Module module : modules) {
+ for (var module : modules) {
readAndValidateModule(schemaContext, module, yinDir);
- for (Submodule submodule : module.getSubmodules()) {
+ for (var submodule : module.getSubmodules()) {
readAndValidateSubmodule(schemaContext, module, submodule, yinDir);
}
}
*/
package org.opendaylight.yangtools.yang.model.export;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
public class Bug2444Test extends AbstractYinExportTest {
@Test
*/
package org.opendaylight.yangtools.yang.model.export;
-import java.io.IOException;
-import javax.xml.stream.XMLStreamException;
-import org.junit.Test;
-import org.xml.sax.SAXException;
+import org.junit.jupiter.api.Test;
public class Bug4504Test extends AbstractYinExportTest {
@Test
- public void test() throws IOException, SAXException, XMLStreamException {
+ public void test() throws Exception {
exportYinModules("/bugs/bug4504", null);
}
}
*/
package org.opendaylight.yangtools.yang.model.export;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.hamcrest.CoreMatchers.allOf;
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class Bug5531Test {
@Test
public void test() throws Exception {
- EffectiveModelContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/bug5531");
+ final var schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/bug5531");
assertNotNull(schema);
assertNotNull(schema.getModules());
assertEquals(1, schema.getModules().size());
- ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
+ var byteArrayOutputStream = new ByteArrayOutputStream();
+ var bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
// write small module of size less than 8kB
- for (ModuleEffectiveStatement module : schema.getModuleStatements().values()) {
+ for (var module : schema.getModuleStatements().values()) {
YinExportUtils.writeModuleAsYinText(module, bufferedOutputStream);
}
String output = byteArrayOutputStream.toString();
// if all changes were flushed then following conditions are satisfied
- assertNotEquals("Output should not be empty", 0, output.length());
- assertTrue("Output should contains start of the module", output.contains("<module"));
- assertTrue("Output should contains end of the module", output.contains("</module>"));
+ assertNotEquals(0, output.length());
+ assertThat(output, allOf(containsString("<module"), containsString("</module>")));
}
}
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.yangtools.yang.model.export;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
-import java.io.OutputStream;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.common.Revision;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
-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 Bug6856Test {
-
@Test
public void testImplicitInputAndOutputInRpc() throws Exception {
- final EffectiveModelContext schemaContext = YangParserTestUtils.parseYangResources(Bug6856Test.class,
- "/bugs/bug-6856/foo.yang");
+ final var schemaContext = YangParserTestUtils.parseYangResources(Bug6856Test.class, "/bugs/bug-6856/foo.yang");
assertNotNull(schemaContext);
- final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
+ final var byteArrayOutputStream = new ByteArrayOutputStream();
+ final var bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
- final Module fooModule = schemaContext.findModule("foo", Revision.of("2017-02-28")).get();
+ final var fooModule = schemaContext.findModule("foo", Revision.of("2017-02-28")).orElseThrow();
YinExportUtils.writeModuleAsYinText(fooModule.asEffectiveStatement(), bufferedOutputStream);
final String output = byteArrayOutputStream.toString();
@Test
public void testExplicitInputAndOutputInRpc() throws Exception {
- final SchemaContext schemaContext = YangParserTestUtils.parseYangResources(Bug6856Test.class,
- "/bugs/bug-6856/bar.yang");
+ final var schemaContext = YangParserTestUtils.parseYangResources(Bug6856Test.class, "/bugs/bug-6856/bar.yang");
assertNotNull(schemaContext);
- final OutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
+ final var byteArrayOutputStream = new ByteArrayOutputStream();
+ final var bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
- final Module barModule = schemaContext.findModule("bar", Revision.of("2017-02-28")).get();
+ final var barModule = schemaContext.findModule("bar", Revision.of("2017-02-28")).get();
YinExportUtils.writeModuleAsYinText(barModule.asEffectiveStatement(), bufferedOutputStream);
final String output = byteArrayOutputStream.toString();
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v10.html
*/
-
package org.opendaylight.yangtools.yang.model.export;
-import java.io.IOException;
-import javax.xml.stream.XMLStreamException;
-import org.junit.Test;
-import org.xml.sax.SAXException;
+import org.junit.jupiter.api.Test;
public class SchemaContextEmitterTest extends AbstractYinExportTest {
-
@Test
- public void testSchemaContextEmitter() throws IOException, XMLStreamException, SAXException {
+ public void testSchemaContextEmitter() throws Exception {
exportYinModules("/schema-context-emitter-test", "/schema-context-emitter-test");
}
}
import java.io.File;
import java.io.FileOutputStream;
-import java.io.OutputStream;
-import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
-import org.junit.Before;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation;
import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
-import org.opendaylight.yangtools.yang.model.repo.spi.SchemaListenerRegistration;
import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceListener;
import org.opendaylight.yangtools.yang.parser.repo.SharedSchemaRepository;
import org.opendaylight.yangtools.yang.parser.rfc7950.repo.TextToIRTransformer;
private EffectiveModelContextFactory schemaContextFactory;
private Set<SourceIdentifier> allTestSources;
- @Before
+ @BeforeEach
public void init() {
schemaRegistry = new SharedSchemaRepository("test");
final TextToIRTransformer astTransformer = TextToIRTransformer.create(schemaRegistry, schemaRegistry);
schemaContextFactory = schemaRegistry.createEffectiveModelContextFactory();
allTestSources = new HashSet<>();
- final SchemaListenerRegistration reg = schemaRegistry.registerSchemaSourceListener(new SchemaSourceListener() {
-
- @Override
- public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
- // NOOP
- }
+ try (var reg = schemaRegistry.registerSchemaSourceListener(
+ new SchemaSourceListener() {
+ @Override
+ public void schemaSourceUnregistered(final PotentialSchemaSource<?> source) {
+ // NOOP
+ }
- @Override
- public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
- for (final PotentialSchemaSource<?> source : sources) {
- allTestSources.add(source.getSourceIdentifier());
+ @Override
+ public void schemaSourceRegistered(final Iterable<PotentialSchemaSource<?>> sources) {
+ for (final PotentialSchemaSource<?> source : sources) {
+ allTestSources.add(source.getSourceIdentifier());
+ }
}
- }
- @Override
- public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
- // NOOP
- }
- });
- reg.close();
+ @Override
+ public void schemaSourceEncountered(final SchemaSourceRepresentation source) {
+ // NOOP
+ }
+ })) {
+ // Noop
+ }
}
@Test
testSetOfModules(allTestSources);
}
- private void testSetOfModules(final Collection<SourceIdentifier> source) throws Exception {
- final EffectiveModelContext schemaContext = schemaContextFactory.createEffectiveModelContext(source).get();
- final File outDir = new File("target/collection");
+ private void testSetOfModules(final Set<SourceIdentifier> source) throws Exception {
+ final var schemaContext = schemaContextFactory.createEffectiveModelContext(source).get();
+ final var outDir = new File("target/collection");
outDir.mkdirs();
for (final Module module : schemaContext.getModules()) {
exportModule(module, outDir);
private static File exportModule(final Module module, final File outDir)
throws Exception {
- final File outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
- try (OutputStream output = new FileOutputStream(outFile)) {
+ final var outFile = new File(outDir, YinExportUtils.wellFormedYinName(module.getName(), module.getRevision()));
+ try (var output = new FileOutputStream(outFile)) {
YinExportUtils.writeModuleAsYinText(module.asEffectiveStatement(), output);
}
return outFile;
*/
package org.opendaylight.yangtools.yang.model.export;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.XMLNamespace;
-import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class YT1313Test {
@Test
public void testSubmoduleImportPrefixes() {
- final ModuleEffectiveStatement bar = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt1313")
+ final var bar = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt1313")
.getModuleStatement(QNameModule.create(XMLNamespace.of("bar")));
final StatementPrefixResolver resolver = StatementPrefixResolver.forModule(bar);
*/
package org.opendaylight.yangtools.yang.model.export;
-import static org.junit.Assert.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.opendaylight.yangtools.yang.model.export.DeclaredStatementFormatter.defaultInstance;
-import java.util.Collection;
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.model.api.Module;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SubmoduleEffectiveStatement;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
public class YangTextSnippetTest {
@Test
public void testNotification() {
- final SchemaContext schema = YangParserTestUtils.parseYangResource("/bugs/bug2444/yang/notification.yang");
- assertFormat(schema.getModules());
+ assertFormat(YangParserTestUtils.parseYangResource("/bugs/bug2444/yang/notification.yang"));
}
@Test
public void testSubmoduleNamespaces() throws Exception {
- SchemaContext schema = YangParserTestUtils.parseYangResourceDirectory("/bugs/yt992");
- assertFormat(schema.getModules());
+ assertFormat(YangParserTestUtils.parseYangResourceDirectory("/bugs/yt992"));
}
- private static void assertFormat(final Collection<? extends Module> modules) {
- for (Module module : modules) {
- final ModuleEffectiveStatement stmt = module.asEffectiveStatement();
- assertNotNull(formatModule(stmt));
+ private static void assertFormat(final EffectiveModelContext context) {
+ for (var module : context.getModuleStatements().values()) {
+ assertNotNull(formatModule(module));
- for (SubmoduleEffectiveStatement substmt : stmt.submodules()) {
+ for (var substmt : module.submodules()) {
assertNotNull(formatSubmodule(substmt));
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
-import java.util.Optional;
import javax.xml.transform.OutputKeys;
-import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.opendaylight.yangtools.util.xml.UntrustedXML;
-import org.opendaylight.yangtools.yang.common.Revision;
import org.opendaylight.yangtools.yang.common.YangConstants;
import org.opendaylight.yangtools.yang.model.api.Module;
import org.w3c.dom.Document;
public final class YinExportTestUtils {
private YinExportTestUtils() {
- throw new UnsupportedOperationException("Utility class");
+ // Hidden on purpose
}
public static Document loadDocument(final String prefix, final Module module) throws IOException, SAXException {
- final Optional<Revision> rev = module.getRevision();
- final String fileName = !rev.isPresent() ? module.getName() : module.getName() + '@' + rev.get().toString();
+ final var rev = module.getRevision();
+ final var fileName = rev.isEmpty() ? module.getName() : module.getName() + '@' + rev.orElseThrow().toString();
return loadDocument(prefix + '/' + fileName + YangConstants.RFC6020_YIN_FILE_EXTENSION);
}
public static Document loadDocument(final String xmlPath) throws IOException, SAXException {
- final InputStream resourceAsStream = SchemaContextEmitterTest.class.getResourceAsStream(xmlPath);
- return requireNonNull(readXmlToDocument(resourceAsStream));
+ return requireNonNull(readXmlToDocument(SchemaContextEmitterTest.class.getResourceAsStream(xmlPath)));
}
static Document readXmlToDocument(final InputStream xmlContent) throws IOException, SAXException {
- final Document doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
+ final var doc = UntrustedXML.newDocumentBuilder().parse(xmlContent);
doc.getDocumentElement().normalize();
return doc;
}
public static String toString(final Node xml) {
try {
- final Transformer transformer = TransformerFactory.newInstance().newTransformer();
+ final var transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
- final StreamResult result = new StreamResult(new StringWriter());
- final DOMSource source = new DOMSource(xml);
+ final var result = new StreamResult(new StringWriter());
+ final var source = new DOMSource(xml);
transformer.transform(source, result);
return result.getWriter().toString();
- } catch (IllegalArgumentException | TransformerFactoryConfigurationError | TransformerException e) {
- throw new RuntimeException("Unable to serialize xml element " + xml, e);
+ } catch (TransformerFactoryConfigurationError | TransformerException e) {
+ throw new IllegalStateException("Unable to serialize xml element " + xml, e);
}
}
}