import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-import java.io.IOException;
+import java.io.File;
+import java.io.FileNotFoundException;
import java.io.InputStream;
+import java.util.Collection;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
+import org.junit.BeforeClass;
import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
+import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);
- private JsonNormalizedNodeBodyReader jsonBodyReader;
- private SchemaContext schemaContext;
+ private final JsonNormalizedNodeBodyReader jsonBodyReader;
+ private static EffectiveModelContext schemaContext;
- public JsonToNnTest() throws NoSuchFieldException, SecurityException {
+ public JsonToNnTest() {
+ super(schemaContext, null);
+ this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
}
- public static void initialize(final String path, SchemaContext schemaContext) {
- schemaContext = schemaContextLoader(path, schemaContext);
- CONTROLLER_CONTEXT.setSchemas(schemaContext);
+ @BeforeClass
+ public static void initialize() throws FileNotFoundException {
+ final Collection<File> testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1");
+ testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3"));
+ testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4"));
+ testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang"));
+ testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang"));
+ schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
}
@Test
public void simpleListTest() throws Exception {
simpleTest("/json-to-nn/simple-list.json",
- "/json-to-nn/simple-list-yang/1", "lst", "simple-list-yang1");
+ "lst", "simple-list-yang1");
}
@Test
public void simpleContainerTest() throws Exception {
simpleTest("/json-to-nn/simple-container.json",
- "/json-to-nn/simple-container-yang", "cont",
- "simple-container-yang");
+ "cont", "simple-container-yang");
}
@Test
public void multipleItemsInLeafListTest() throws Exception {
-
- initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
-
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/multiple-leaflist-items.json",
"simple-list-yang1:lst");
@Test
public void multipleItemsInListTest() throws Exception {
- initialize("/json-to-nn/simple-list-yang/3", this.schemaContext);
-
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/multiple-items-in-list.json",
"multiple-items-yang:lst");
assertNotNull(normalizedNodeContext);
- assertEquals("lst", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
+ assertEquals("lst", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
verityMultipleItemsInList(normalizedNodeContext);
}
@Test
public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
- initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
-
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
assertNotNull(normalizedNodeContext);
- assertEquals("cont", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
+ assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
- final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
- .getData());
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("lf"));
assertTrue(dataTree.contains("empty"));
}
@Test
public void incorrectTopLevelElementsTest() throws Exception {
-
- this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
InputStream inputStream = this.getClass().getResourceAsStream(
}
assertNotNull(exception);
assertEquals(
- "Error parsing input: Schema node with name cont was not found under "
+ "Error parsing input: Schema node with name wrong was not found under "
+ "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
exception.getErrors().get(0).getErrorMessage());
@Test
public void emptyDataReadTest() throws Exception {
-
- initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
-
final NormalizedNodeContext normalizedNodeContext = prepareNNC(
"/json-to-nn/empty-data.json", "array-with-null-yang:cont");
assertNotNull(normalizedNodeContext);
- assertEquals("cont", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
+ assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
- final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
- .getData());
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("lflst1"));
assertTrue(dataTree.contains("lflst2 45"));
- this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
RestconfDocumentedException exception = null;
mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
- final InputStream inputStream = this.getClass().getResourceAsStream(
- "/json-to-nn/empty-data.json1");
+ final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/empty-data.json1");
try {
- this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
- inputStream);
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
}
assertNotNull(exception);
- assertEquals("Error parsing input: null", exception.getErrors().get(0)
- .getErrorMessage());
+ assertEquals("Error parsing input: null", exception.getErrors().get(0).getErrorMessage());
}
@Test
public void testJsonBlankInput() throws Exception {
- initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
- final NormalizedNodeContext normalizedNodeContext = prepareNNC("",
- "array-with-null-yang:cont");
+ final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont");
assertNull(normalizedNodeContext);
}
@Test
public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
-
- initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
-
final String uri = "simple-list-yang1" + ":" + "lst";
- final NormalizedNodeContext normalizedNodeContext = prepareNNC(
- "/json-to-nn/simple-list.json", uri);
+ final NormalizedNodeContext normalizedNodeContext = prepareNNC("/json-to-nn/simple-list.json", uri);
assertNotNull(normalizedNodeContext);
verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
- final InputStream inputStream = this.getClass().getResourceAsStream(
- "/json-to-nn/simple-list.json");
+ final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/simple-list.json");
try {
- this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
- inputStream);
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
fail("NormalizedNodeContext should not be create because of different namespace");
} catch (final RestconfDocumentedException e) {
LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
@Test
public void dataAugmentedTest() throws Exception {
-
- initialize("/common/augment/yang", this.schemaContext);
-
- NormalizedNodeContext normalizedNodeContext = prepareNNC(
- "/common/augment/json/dataa.json", "main:cont");
+ NormalizedNodeContext normalizedNodeContext = prepareNNC("/common/augment/json/dataa.json", "main:cont");
assertNotNull(normalizedNodeContext);
- assertEquals("cont", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
+ assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
.getData());
assertTrue(dataTree.contains("cont1"));
assertTrue(dataTree.contains("lf11 lf11 value from a"));
- normalizedNodeContext = prepareNNC("/common/augment/json/datab.json",
- "main:cont");
+ normalizedNodeContext = prepareNNC("/common/augment/json/datab.json", "main:cont");
assertNotNull(normalizedNodeContext);
- assertEquals("cont", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
- dataTree = NormalizedNodes
- .toStringTree(normalizedNodeContext.getData());
+ assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
+ dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("cont1"));
assertTrue(dataTree.contains("lf11 lf11 value from b"));
}
- private void simpleTest(final String jsonPath, final String yangPath,
- final String topLevelElementName, final String moduleName) throws Exception {
-
- initialize(yangPath, this.schemaContext);
-
+ private void simpleTest(final String jsonPath, final String topLevelElementName,
+ final String moduleName) throws Exception {
final String uri = moduleName + ":" + topLevelElementName;
final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
}
private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
- this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
try {
mockBodyReader(uri, this.jsonBodyReader, false);
- } catch (NoSuchFieldException | SecurityException
- | IllegalArgumentException | IllegalAccessException e) {
+ } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
LOG.warn("Operation failed due to: {}", e.getMessage());
}
final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);
NormalizedNodeContext normalizedNodeContext = null;
try {
- normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null,
- this.mediaType, null, inputStream);
- } catch (WebApplicationException | IOException e) {
+ normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
+ } catch (WebApplicationException e) {
// TODO Auto-generated catch block
}
private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
final String topLevelElementName) {
- assertEquals(topLevelElementName, normalizedNodeContext.getData()
- .getNodeType().getLocalName());
+ assertEquals(topLevelElementName, normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
- final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
- .getData());
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("cont1"));
assertTrue(dataTree.contains("lst1"));
assertTrue(dataTree.contains("lflst1"));
}
private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
-
- final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
- .getData());
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("lf11"));
assertTrue(dataTree.contains("lf11_1"));
assertTrue(dataTree.contains("lflst11"));
@Test
public void unsupportedDataFormatTest() throws Exception {
- this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/1", this.schemaContext);
mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);
- final InputStream inputStream = this.getClass().getResourceAsStream(
- "/json-to-nn/unsupported-json-format.json");
+ final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/unsupported-json-format.json");
RestconfDocumentedException exception = null;
try {
- this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
- inputStream);
+ this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
} catch (final RestconfDocumentedException e) {
exception = e;
}
LOG.info(exception.getErrors().get(0).getErrorMessage());
- assertTrue(exception.getErrors().get(0).getErrorMessage()
- .contains("is not a simple type"));
+ assertTrue(exception.getErrors().get(0).getErrorMessage().contains("is not a simple type"));
}
@Test
public void invalidUriCharacterInValue() throws Exception {
-
- this.jsonBodyReader = new JsonNormalizedNodeBodyReader();
- initialize("/json-to-nn/simple-list-yang/4", this.schemaContext);
mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
final InputStream inputStream = this.getClass().getResourceAsStream(
null, null, null, this.mediaType, null, inputStream);
assertNotNull(normalizedNodeContext);
- assertEquals("cont", normalizedNodeContext.getData().getNodeType()
- .getLocalName());
+ assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
- final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
- .getData());
+ final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
}