Resource-validator

From WebLab Wiki
Jump to: navigation, search

This page is a tutorial for the WebLab Resource Validator.

What is the WebLab Resource Validator

The WebLab Resource Validator is a utility project that was proposed to check whether services outputs suit the WebLab requirements.

This project is still in a beta phase, it allows most important checking:

  • the resource can be marshalled,
  • unmarshalling the marshalled resource resolves the and exactly identical resource,
  • a MediaUnit does not contain any incoherent type of Segment, and some other constraints are verified(TrackSegment contains 2 or more Segments, SpatialSegment has two or more Coordinates, Temporal and Linear Segment starts before they ends)
  • the URIs contained in the resource are well Formed,
  • URIs of each resource and segment inside this resource are unique within the scope of the main resource.

More checking should be add later.

A brief technical description

The resource-validator project consists on: (For more detail see the javadoc.)

  • A Validator Interface to be implemented for the validation of WebLab resources.
  • An already developed list of Validator implementing the Validator Interface. For the moment, five Validators are developed, each one of them dealing with one of the validations mentioned in the list below.
  • An InMemoryExecutor that is a specific Validator implementation, which can be initialised with a list of Validators to execute. (The Validators are executed in the list order and stops at the first that fails.)


How to use the WebLab Resource Validator?

The WebLab Resource Validator can be used in three ways to validate your own resource:

For specific project, the Validator interface can be implemented, and calling an Executor with a custom list of Validator should be easy. (see Going further)

Use Resource Validator as an executable jar

We aggregate the project output along with all its dependencies into a single distributable archive ([1])


To use the Resource Validator as an executable jar, you have first to download its compiled -with all its dependencies- artefact. This distributable archive is already available on the OW2 maven repository (download).

Then you just have to execute the following command:

java -jar resource-validator-1.0.1-executable.jar [FileToValidate]

Use Resource Validator in a test unit or in your own java class

  • First, you have to add the resource-validator as a dependency in you project.

To do so:

Add the OW2 maven repository in your maven settings:

Instead of building resource-validator from source, you can add the maven repository containing resource-validator artifact. To use resource-validator artifact in your project, add the following repository in your maven settings (your-apache-maven-path\conf\settings.xml):

   <repository>
      <id>OW2 maven repository</id>
      <url>http://maven.ow2.org/maven2</url>
   </repository>
   <repository>
      <id>WebLab Maven repository</id>
      <url>http://maven.weblab-project.org/</url>
   </repository>

Add resource-validator as a Maven dependency in your project:

To use the resource-validator as a dependency in you project, add the corresponding artifact in the pom.xml file of your project. An example of a simple pom.xml project using the WebLab resource-validator as a dependency:

<project xmlns=http://maven.apache.org/POM/4.0.0 
	  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
	  http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
 
  <groupId>my.group.id</groupId>
  <artifactId>example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <name>My project name</name>
 
  <description>This project defines the WebLab resource-validator as a dependency library.</description>
 
   <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.0.2</version>
        <inherited>true</inherited>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
          <encoding>UTF-8</encoding>
          <showWarnings>true</showWarnings>
          <showDeprecations>true</showDeprecations>
          <debug>true</debug>
          <optimize>false</optimize>
          <verbose>true</verbose>
        </configuration>
      </plugin>
    </plugins>
  </build>
 
  <dependencies>
    <dependency>
      <groupId>org.ow2.weblab.core.helpers</groupId>
      <artifactId>resource-validator</artifactId>
      <version>1.0.1</version>
    </dependency>
  </dependencies>
 
</project>


  • Now you can use the resource-validator. Two possibilities are proposed to use it in other java classes depending on the type of data to validate: (a) an xml file or (b) a WebLab Resource.

(a) If you need to validate a file, you can use the ResourceValidator class as following: (org.ow2.weblab.core.helpers.validator.ResourceValidator)

File resourceFileToCheck = new File("pathToFile/resourceToCheck.xml");
Boolean isValid=ResourceValidator.validateFile(resourceFileToCheck);

(b) If you need to validate a WebLab Resource, we provide a InMemoryExecutor class, that runs a predefined or a given list of Validators against a Resource. (org.ow2.weblab.core.helpers.validator.InMemoryExecutor.InMemoryExecutor)

By default, InMemoryExecutor runs the five Validators already implemented and that are defined in the META-INF/services/org.ow2.weblab.core. helpers.validator.Validator file :

InMemoryExecutor myValidator = new InMemoryExecutor();

You can also set the Validator or list of Validators you want the InMemoryExecutor runs:

InMemoryExecutor myValidator = new InMemoryExecutor(new CanBeMarshalled_Validator (), new Resource_URIs_Validator());

Then use your Validator instance to check your Resource:

org.ow2.weblab.core.model.Resource resourceToCheck;
Boolean isValid= myValidator.validate(resourceToCheck);

Use Resource Validator in a SoapUI project

Resource Validator can be used in in SoapUI to validate your tested service response. We aggregate the project output along with all its dependencies into a single distributable archive ([2]) that can be imported into the SoapUI projects.

All the steps to use Resource Validator in SoapUI are detailed in SoapUI Testing Chain.

Briefly, you have to:

  • Copy resource-validator-version-jar-with-dependencies.jar into {path_to_your_soapUI }/bin/ext/ and restart Soap-UI. (See here)
  • Add your request in a TestCase if not done. (See here)
  • Add a validation of the response message by adding a Script Assertion that will use the WebLab Resource Validator API. (See here)

Going further: Implementing your own Resource Validator

First, add the resource-validator as a dependency in you project as shown previously. You may need to use other dependencies such as WebLab helpers.

If you are planning to use You-Validator in a SoapUI project, and you are using the WebLab org.ow2.weblab.core:extended dependency in your java project, you should use its 1.2.5 version or later.


  • Create Your-Validator class

Your class must implements the Validator class and its validate method.

public class Your_Validator implements Validator {
 
public boolean validate(Resource resource) {
 
//Your code here 
}
}

  • Redefine Validators list to execute

To call the Executor (InMemoryExecutor class) with a chosen list of Validator you should modify the list already defined in the META-INF/services/org.ow2.weblab.core. helpers.validator.Validator file. For example, you can add Your-Validator in the end of the existing list.

  • You plan to use Your-Validator in a SoapUI project?

If you plan to use You-Validator in a SoapUI project, your project's artifact must be packaged along with all its dependencies in a single JAR. To do so, you can use, in the project’s POM and in addition to the Maven Compiler Plugin, the Maven Assembly Plugin configured as follows:

<build>
   <finalName>Your-Resource-Validator</finalName>
   <plugins>
	<plugin>
	   <groupId>org.apache.maven.plugins</groupId>
	   <artifactId>maven-compiler-plugin</artifactId>
	   ……
	</plugin>
	<plugin>
	   <groupId>org.apache.maven.plugins</groupId>
	   <artifactId>maven-assembly-plugin</artifactId>
	   <version>2.2.1</version>
	   <executions>
		<execution>
		   <goals><goal>single</goal></goals>	
		   <phase>package</phase>
		   <configuration>
			<appendAssemblyId>false</appendAssemblyId>
			<descriptorRefs>
			   <descriptorRef>jar-with-dependencies</descriptorRef>
		     	</descriptorRefs>
		   </configuration>
		</execution>			
	   </executions>	
	</plugin>
   </plugins>
</build>