--- /dev/null
+/*
+ * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.test.util;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.io.CharSource;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Optional;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.yangtools.yang.common.UnresolvedQName;
+import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
+
+/**
+ * A {@link YangTextSchemaSource} backed by a string literal.
+ */
+final class LiteralYangTextSchemaSource extends YangTextSchemaSource {
+ private final @NonNull String sourceString;
+
+ private LiteralYangTextSchemaSource(final SourceIdentifier identifier, final String sourceString,
+ final String symbolicName) {
+ super(identifier);
+ this.sourceString = requireNonNull(sourceString);
+ }
+
+ /**
+ * Create a new {@link YangTextSchemaSource} backed by a String input.
+ *
+ * @param sourceString YANG file as a String
+ * @return A new instance.
+ * @throws NullPointerException if {@code sourceString} is {@code null}
+ * @throws IllegalArgumentException if {@code sourceString} does not a valid YANG body, given a rather restrictive
+ * view of what is valid.
+ */
+ static @NonNull LiteralYangTextSchemaSource ofLiteral(final String sourceString) {
+ // First line of a YANG file looks as follows:
+ // `module module-name {`
+ // therefore in order to extract the name of the module from a plain string, we are interested in the second
+ // word of the first line
+ final String[] firstLine = sourceString.substring(0, sourceString.indexOf("{")).strip().split(" ");
+ final String moduleOrSubmoduleString = firstLine[0].strip();
+ checkArgument(moduleOrSubmoduleString.equals("module") || moduleOrSubmoduleString.equals("submodule"));
+
+ final String arg = firstLine[1].strip();
+ final var localName = UnresolvedQName.tryLocalName(arg);
+ checkArgument(localName != null);
+ return new LiteralYangTextSchemaSource(new SourceIdentifier(localName), sourceString, arg);
+ }
+
+ @Override
+ public InputStream openStream() throws IOException {
+ return CharSource.wrap(sourceString).asByteSource(StandardCharsets.UTF_8).openStream();
+ }
+
+ @Override
+ public Optional<String> getSymbolicName() {
+ return Optional.of(getIdentifier().name().getLocalName());
+ }
+}
--- /dev/null
+/*
+ * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.test.util;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+
+public class YT1483Test {
+ private static final String TEST_YANG = """
+ module root-conflict { // results in RootConflictData
+ namespace root-conflict;
+ prefix rc;
+
+ container root-conflict-data; // results in RootConflictData as well
+ }""";
+ private static final String INCORRECT_YANG1 = """
+ /*a comment*/ module /*another comment*/ fail {
+ namespace fail-namespace;
+ prefix ff;
+ }""";
+ private static final String INCORRECT_YANG2 = """
+ module /*a comment*/ fail {
+ namespace fail-namespace;
+ prefix ff;
+ }""";
+ private static final String INCORRECT_YANG3 = """
+ /*comment*/ module fail {
+ namespace fail-namespace;
+ prefix ff;
+ }""";
+
+ @Test
+ public void testParseYang() {
+ final var fromLiteral = YangParserTestUtils.parseYang(TEST_YANG);
+ final var fromResources = YangParserTestUtils.parseYangResourceDirectory("/");
+ final var qname = QName.create("root-conflict", "root-conflict");
+ assertTrue(fromResources.findModule(qname.getModule()).isPresent());
+ assertTrue(fromLiteral.findModule(qname.getModule()).isPresent());
+ }
+
+ @Test
+ public void testIncorrectYang() {
+ assertThrows(IllegalArgumentException.class, () -> YangParserTestUtils.parseYang(INCORRECT_YANG1));
+ assertThrows(IllegalArgumentException.class, () -> YangParserTestUtils.parseYang(INCORRECT_YANG2));
+ assertThrows(IllegalArgumentException.class, () -> YangParserTestUtils.parseYang(INCORRECT_YANG3));
+ }
+}