From ea6445f6641f6d367d89372157aaf1db313192b6 Mon Sep 17 00:00:00 2001 From: Jozef Gloncak Date: Wed, 4 Dec 2013 13:50:06 +0100 Subject: [PATCH] Common yang and xml loader for tests Class YangAndXmlAndDataSchemaLoader was created as common ancestor for those test which need load yang files, get reference to concrete module and concrete schema node. DummyRpcResult class was transformed (inner Builder class including) to generic class. Type parameter represents return type of method getResult. Now it is possible to use DummyRpcResult also in InvokeRpcMethodTest. Change-Id: Ib8d99596bbb16c42724ab79125f4273f9d609a50 Signed-off-by: Jozef Gloncak --- .../restconf/impl/test/DummyRpcResult.java | 48 ++++++------- .../impl/test/InvokeRpcMethodTest.java | 29 +------- .../sal/restconf/impl/test/TestUtils.java | 7 +- .../impl/test/ToJsonChoiceCaseTest.java | 21 ++---- .../impl/test/ToJsonIdentityrefTest.java | 18 ++--- .../restconf/impl/test/ToJsonLeafrefType.java | 15 +--- .../restconf/impl/test/XmlProvidersTest.java | 68 +++++++++++-------- .../test/YangAndXmlAndDataSchemaLoader.java | 27 ++++++++ 8 files changed, 103 insertions(+), 130 deletions(-) create mode 100644 opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyRpcResult.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyRpcResult.java index 5ab4f99fdc..7f23776a84 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyRpcResult.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyRpcResult.java @@ -2,31 +2,25 @@ package org.opendaylight.controller.sal.restconf.impl.test; import java.util.Collection; -import org.opendaylight.controller.md.sal.common.api.TransactionStatus; -import org.opendaylight.yangtools.yang.common.RpcError; -import org.opendaylight.yangtools.yang.common.RpcResult; +import org.opendaylight.yangtools.yang.common.*; + +public class DummyRpcResult implements RpcResult { -public class DummyRpcResult implements RpcResult { - private final boolean isSuccessful; - private final TransactionStatus result; + private final T result; private final Collection errors; - + public DummyRpcResult() { isSuccessful = false; result = null; errors = null; } - - private DummyRpcResult(Builder builder) { + + private DummyRpcResult(Builder builder) { isSuccessful = builder.isSuccessful; result = builder.result; errors = builder.errors; } - - public static Builder builder() { - return new DummyRpcResult.Builder(); - } @Override public boolean isSuccessful() { @@ -34,7 +28,7 @@ public class DummyRpcResult implements RpcResult { } @Override - public TransactionStatus getResult() { + public T getResult() { return result; } @@ -42,31 +36,31 @@ public class DummyRpcResult implements RpcResult { public Collection getErrors() { return errors; } - - public static class Builder { + + public static class Builder { private boolean isSuccessful; - private TransactionStatus result; + private T result; private Collection errors; - - public Builder isSuccessful(boolean isSuccessful) { + + public Builder isSuccessful(boolean isSuccessful) { this.isSuccessful = isSuccessful; return this; } - - public Builder result(TransactionStatus result) { + + public Builder result(T result) { this.result = result; return this; } - - public Builder errors(Collection errors) { + + public Builder errors(Collection errors) { this.errors = errors; return this; } - - public RpcResult build() { - return new DummyRpcResult(this); + + public RpcResult build() { + return new DummyRpcResult(this); } - + } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java index 104e52f6a9..103c9ed3cd 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java @@ -23,38 +23,11 @@ public class InvokeRpcMethodTest { private static Set modules; - private class RpcResultImpl implements RpcResult { - - final T result; - - public RpcResultImpl(T compNode) { - this.result = compNode; - } - - @Override - public boolean isSuccessful() { - // TODO Auto-generated method stub - return false; - } - - @Override - public T getResult() { - return result; - } - - @Override - public Collection getErrors() { - // TODO Auto-generated method stub - return null; - } - - } - private class AnswerImpl implements Answer> { @Override public RpcResult answer(InvocationOnMock invocation) throws Throwable { CompositeNode compNode = (CompositeNode) invocation.getArguments()[1]; - return new RpcResultImpl(compNode); + return new DummyRpcResult.Builder().result(compNode).build(); } } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java index 61942e81a6..20dfb31dab 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java @@ -205,14 +205,14 @@ final class TestUtils { ControllerContext contContext = ControllerContext.getInstance(); contContext.setSchemas(loadSchemaContext(modules)); - + StructuredDataToJsonProvider structuredDataToJsonProvider = StructuredDataToJsonProvider.INSTANCE; structuredDataToJsonProvider.writeTo(new StructuredData(compositeNode, dataSchemaNode), null, null, null, null, null, byteArrayOS); jsonResult = byteArrayOS.toString(); if (outputPath != null) { - try { + try { outputToFile(byteArrayOS, outputPath); } catch (IOException e) { System.out.println("Output file wasn't cloased sucessfuly."); @@ -343,7 +343,8 @@ final class TestUtils { ControllerContext controllerContext = mock(ControllerContext.class); BrokerFacade broker = mock(BrokerFacade.class); - RpcResult rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build(); + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.COMMITED).build(); Future> future = DummyFuture.builder().rpcResult(rpcResult).build(); when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema); when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn( diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java index 8e3ff1d2ed..c5682cb5cc 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java @@ -1,27 +1,18 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.util.Set; -import javax.activation.UnsupportedDataTypeException; import javax.ws.rs.WebApplicationException; import org.junit.*; -import org.opendaylight.yangtools.yang.model.api.*; -public class ToJsonChoiceCaseTest { - - private static Set modules; - private static DataSchemaNode dataSchemaNode; +public class ToJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/yang-to-json-conversion/choice"); - Module module = TestUtils.resolveModule(null, modules); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null); - + dataLoad("/yang-to-json-conversion/choice"); } /** @@ -44,9 +35,9 @@ public class ToJsonChoiceCaseTest { /** * Test when some data are in one case node and other in another. - * Additionally data are loadef from various choices. This isn't - * correct. Next Json validator should return error because nodes has to be - * from one case below concrete choice. + * Additionally data are loadef from various choices. This isn't correct. + * Next Json validator should return error because nodes has to be from one + * case below concrete choice. * */ @Test diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java index c9ac042ff1..ce1b4afaba 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java @@ -1,9 +1,9 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.util.Set; import java.util.regex.*; import javax.ws.rs.WebApplicationException; @@ -11,22 +11,12 @@ import javax.ws.rs.WebApplicationException; import org.junit.*; import org.opendaylight.yangtools.yang.data.api.*; import org.opendaylight.yangtools.yang.data.impl.NodeFactory; -import org.opendaylight.yangtools.yang.model.api.*; -public class ToJsonIdentityrefTest { - - private static Set modules; - private static DataSchemaNode dataSchemaNode; +public class ToJsonIdentityrefTest extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/yang-to-json-conversion/identityref"); - assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("identityref-module", modules); - assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont"); - assertNotNull(dataSchemaNode); - + dataLoad("/yang-to-json-conversion/identityref", 2, "identityref-module", "cont"); } @Test diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java index a5cc02e9f2..b4b8b4b3e3 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java @@ -1,31 +1,20 @@ package org.opendaylight.controller.sal.restconf.impl.test; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.io.IOException; -import java.util.Set; import java.util.regex.Matcher; import javax.ws.rs.WebApplicationException; import org.junit.*; -import org.opendaylight.yangtools.yang.model.api.*; -public class ToJsonLeafrefType { - private static Set modules; - private static DataSchemaNode dataSchemaNode; +public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader { @BeforeClass public static void initialization() { - modules = TestUtils.resolveModules("/yang-to-json-conversion/leafref"); - assertEquals(2, modules.size()); - Module module = TestUtils.resolveModule("main-module", modules); - assertNotNull(module); - dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont"); - assertNotNull(dataSchemaNode); - + dataLoad("/yang-to-json-conversion/leafref", 2, "main-module", "cont"); } @Test diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java index 10885b642b..5b0eea3212 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java @@ -54,7 +54,8 @@ public class XmlProvidersTest extends JerseyTest { @BeforeClass public static void init() throws FileNotFoundException { - Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath()); + Set allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs") + .getPath()); SchemaContext schemaContext = TestUtils.loadSchemaContext(allModules); controllerContext = ControllerContext.getInstance(); controllerContext.setSchemas(schemaContext); @@ -75,11 +76,11 @@ public class XmlProvidersTest extends JerseyTest { @Test public void testStructuredDataToXmlProvider() throws FileNotFoundException, UnsupportedEncodingException { String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0"); - + InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); CompositeNode loadedCompositeNode = TestUtils.loadCompositeNode(xmlStream); when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(loadedCompositeNode); - + Response response = target(uri).request(MEDIA_TYPE).get(); assertEquals(200, response.getStatus()); } @@ -87,94 +88,100 @@ public class XmlProvidersTest extends JerseyTest { @Test public void testBadFormatXmlToCompositeNodeProvider() throws UnsupportedEncodingException, URISyntaxException { String uri = createUri("/operations/", "ietf-interfaces:interfaces/interface/eth0"); - + Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post( Entity.entity("", MEDIA_TYPE)); assertEquals(400, response.getStatus()); - + response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post( Entity.entity("", MEDIA_TYPE)); assertEquals(400, response.getStatus()); } - + @Test public void testXmlToCompositeNode404NotFound() throws UnsupportedEncodingException, URISyntaxException { String uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0"); - + when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null); - - Response response = target(uri).request(Draft01.MediaTypes.DATA+RestconfService.XML).get(); + + Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get(); assertEquals(404, response.getStatus()); } - + @Test public void testXmlToCompositeNode400() throws UnsupportedEncodingException, URISyntaxException { String uri = createUri("/datastore/", "simple-nodes:user/name"); - + when(brokerFacade.readOperationalData(any(InstanceIdentifier.class))).thenReturn(null); - - Response response = target(uri).request(Draft01.MediaTypes.DATA+RestconfService.XML).get(); + + Response response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).get(); assertEquals(400, response.getStatus()); } - + @Test public void testRpcResultCommitedToStatusCodes() throws UnsupportedEncodingException { InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); Entity entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02); - RpcResult rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build(); + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.COMMITED).build(); Future> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build(); - when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture); - when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture); - + when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(dummyFuture); + when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(dummyFuture); + String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity); assertEquals(204, response.getStatus()); response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity); assertEquals(200, response.getStatus()); - + uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity); assertEquals(204, response.getStatus()); response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity); assertEquals(200, response.getStatus()); - + uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0"); response = target(uri).request(MEDIA_TYPE).put(entity); assertEquals(204, response.getStatus()); response = target(uri).request(MEDIA_TYPE).post(entity); assertEquals(200, response.getStatus()); } - + @Test public void testRpcResultOtherToStatusCodes() throws UnsupportedEncodingException { InputStream xmlStream = RestconfImplTest.class.getResourceAsStream("/parts/ietf-interfaces_interfaces.xml"); String xml = TestUtils.getDocumentInPrintableForm(TestUtils.loadDocumentFrom(xmlStream)); Entity entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02); - RpcResult rpcResult = DummyRpcResult.builder().result(TransactionStatus.FAILED).build(); + RpcResult rpcResult = new DummyRpcResult.Builder().result( + TransactionStatus.FAILED).build(); Future> dummyFuture = DummyFuture.builder().rpcResult(rpcResult).build(); - when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture); - when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(dummyFuture); - + when(brokerFacade.commitOperationalDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(dummyFuture); + when(brokerFacade.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))) + .thenReturn(dummyFuture); + String uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); Response response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity); assertEquals(500, response.getStatus()); response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity); assertEquals(500, response.getStatus()); - + uri = createUri("/config/", "ietf-interfaces:interfaces/interface/eth0"); response = target(uri).request(MEDIA_TYPE_DRAFT02).put(entity); assertEquals(500, response.getStatus()); response = target(uri).request(MEDIA_TYPE_DRAFT02).post(entity); assertEquals(500, response.getStatus()); - + uri = createUri("/datastore/", "ietf-interfaces:interfaces/interface/eth0"); response = target(uri).request(MEDIA_TYPE).put(entity); assertEquals(500, response.getStatus()); response = target(uri).request(MEDIA_TYPE).post(entity); assertEquals(500, response.getStatus()); } - + private String createUri(String prefix, String encodedPart) throws UnsupportedEncodingException { return URI.create(prefix + URLEncoder.encode(encodedPart, Charsets.US_ASCII.name()).toString()).toASCIIString(); } @@ -185,9 +192,10 @@ public class XmlProvidersTest extends JerseyTest { enable(TestProperties.DUMP_ENTITY); enable(TestProperties.RECORD_LOG_LEVEL); set(TestProperties.RECORD_LOG_LEVEL, Level.ALL.intValue()); - + ResourceConfig resourceConfig = new ResourceConfig(); - resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, XmlToCompositeNodeProvider.INSTANCE); + resourceConfig = resourceConfig.registerInstances(restconfImpl, StructuredDataToXmlProvider.INSTANCE, + XmlToCompositeNodeProvider.INSTANCE); return resourceConfig; } diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java new file mode 100644 index 0000000000..7e3da0e4b4 --- /dev/null +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java @@ -0,0 +1,27 @@ +package org.opendaylight.controller.sal.restconf.impl.test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.Set; +import org.opendaylight.yangtools.yang.model.api.*; + +public abstract class YangAndXmlAndDataSchemaLoader { + + protected static Set modules; + protected static DataSchemaNode dataSchemaNode; + + protected static void dataLoad(String yangPath) { + dataLoad(yangPath, 1, null, null); + } + + protected static void dataLoad(String yangPath, int modulesNumber, String moduleName, String dataSchemaName) { + modules = TestUtils.resolveModules(yangPath); + assertEquals(modulesNumber, modules.size()); + Module module = TestUtils.resolveModule(moduleName, modules); + assertNotNull(module); + dataSchemaNode = TestUtils.resolveDataSchemaNode(module, dataSchemaName); + assertNotNull(dataSchemaNode); + } + +} -- 2.36.6