Common yang and xml loader for tests 41/3441/1
authorJozef Gloncak <jgloncak@cisco.com>
Wed, 4 Dec 2013 12:50:06 +0000 (13:50 +0100)
committerJozef Gloncak <jgloncak@cisco.com>
Wed, 4 Dec 2013 12:59:40 +0000 (13:59 +0100)
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 <jgloncak@cisco.com>
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/DummyRpcResult.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/InvokeRpcMethodTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/TestUtils.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonChoiceCaseTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonIdentityrefTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/ToJsonLeafrefType.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/XmlProvidersTest.java
opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/YangAndXmlAndDataSchemaLoader.java [new file with mode: 0644]

index 5ab4f99fdc07a20c78d65343b1b2dba0f3afd210..7f23776a84bf903171ce1df059b1976d5ba12c2a 100644 (file)
@@ -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<T> implements RpcResult<T> {
 
-public class DummyRpcResult implements RpcResult<TransactionStatus> {
-    
     private final boolean isSuccessful;
-    private final TransactionStatus result;
+    private final T result;
     private final Collection<RpcError> errors;
-    
+
     public DummyRpcResult() {
         isSuccessful = false;
         result = null;
         errors = null;
     }
-    
-    private DummyRpcResult(Builder builder) {
+
+    private DummyRpcResult(Builder<T> 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<TransactionStatus> {
     }
 
     @Override
-    public TransactionStatus getResult() {
+    public T getResult() {
         return result;
     }
 
@@ -42,31 +36,31 @@ public class DummyRpcResult implements RpcResult<TransactionStatus> {
     public Collection<RpcError> getErrors() {
         return errors;
     }
-    
-    public static class Builder {
+
+    public static class Builder<T> {
         private boolean isSuccessful;
-        private TransactionStatus result;
+        private T result;
         private Collection<RpcError> errors;
-        
-        public Builder isSuccessful(boolean isSuccessful) {
+
+        public Builder<T> isSuccessful(boolean isSuccessful) {
             this.isSuccessful = isSuccessful;
             return this;
         }
-        
-        public Builder result(TransactionStatus result) {
+
+        public Builder<T> result(T result) {
             this.result = result;
             return this;
         }
-        
-        public Builder errors(Collection<RpcError> errors) {
+
+        public Builder<T> errors(Collection<RpcError> errors) {
             this.errors = errors;
             return this;
         }
-        
-        public RpcResult<TransactionStatus> build() {
-            return new DummyRpcResult(this);
+
+        public RpcResult<T> build() {
+            return new DummyRpcResult<T>(this);
         }
-        
+
     }
 
 }
index 104e52f6a9ce6a66540daee850683e35ed0fd4ac..103c9ed3cdbe04f3f192180da77d6fd8fc14945e 100644 (file)
@@ -23,38 +23,11 @@ public class InvokeRpcMethodTest {
 
     private static Set<Module> modules;
 
-    private class RpcResultImpl<T> implements RpcResult<T> {
-
-        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<RpcError> getErrors() {
-            // TODO Auto-generated method stub
-            return null;
-        }
-
-    }
-
     private class AnswerImpl implements Answer<RpcResult<CompositeNode>> {
         @Override
         public RpcResult<CompositeNode> answer(InvocationOnMock invocation) throws Throwable {
             CompositeNode compNode = (CompositeNode) invocation.getArguments()[1];
-            return new RpcResultImpl<CompositeNode>(compNode);
+            return new DummyRpcResult.Builder<CompositeNode>().result(compNode).build();
         }
     }
 
index 61942e81a6e5918689cb4e77d1c9758b94b85911..20dfb31dab3362261fb1496dd1da9246fbc8052a 100644 (file)
@@ -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<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
+        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
+                TransactionStatus.COMMITED).build();
         Future<RpcResult<TransactionStatus>> future = DummyFuture.builder().rpcResult(rpcResult).build();
         when(controllerContext.toInstanceIdentifier(any(String.class))).thenReturn(instIdAndSchema);
         when(broker.commitConfigurationDataPut(any(InstanceIdentifier.class), any(CompositeNode.class))).thenReturn(
index 8e3ff1d2ed4a998997181a897a1c8ff648099aaa..c5682cb5cc1381eb31c66716a06118aa2e557e48 100644 (file)
@@ -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<Module> 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
index c9ac042ff1b60655a89d9eb31a326d2cd3e6039d..ce1b4afababd1841f0718410b74158d43ef9d6ac 100644 (file)
@@ -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<Module> 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
index a5cc02e9f26e26fb934311b221f1e4574efa9592..b4b8b4b3e3bc8d144fde73d51090354ecbefe0a4 100644 (file)
@@ -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<Module> 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
index 10885b642b8a75de219a2e997e95d362fbf43085..5b0eea32126e5f85dc5c95d2c7b0ef52624c9a9d 100644 (file)
@@ -54,7 +54,8 @@ public class XmlProvidersTest extends JerseyTest {
 
     @BeforeClass
     public static void init() throws FileNotFoundException {
-        Set<Module> allModules = TestUtils.loadModules(RestconfImplTest.class.getResource("/full-versions/yangs").getPath());
+        Set<Module> 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("<SimpleNode/>", MEDIA_TYPE));
         assertEquals(400, response.getStatus());
-        
+
         response = target(uri).request(Draft01.MediaTypes.DATA + RestconfService.XML).post(
                 Entity.entity("<SimpleNode>", 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<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
-        RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.COMMITED).build();
+        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
+                TransactionStatus.COMMITED).build();
         Future<RpcResult<TransactionStatus>> 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<String> entity = Entity.entity(xml, MEDIA_TYPE_DRAFT02);
-        RpcResult<TransactionStatus> rpcResult = DummyRpcResult.builder().result(TransactionStatus.FAILED).build();
+        RpcResult<TransactionStatus> rpcResult = new DummyRpcResult.Builder<TransactionStatus>().result(
+                TransactionStatus.FAILED).build();
         Future<RpcResult<TransactionStatus>> 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 (file)
index 0000000..7e3da0e
--- /dev/null
@@ -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<Module> 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);
+    }
+
+}