Commit fdaa1fb3 authored by Francesco Chicchiriccò's avatar Francesco Chicchiriccò
Browse files

Adding first deserialization test (based on provided 'before_wp5') + using StAX

parent a2db9cc7
......@@ -62,6 +62,12 @@ limitations under the License.
</distributionManagement>
<dependencies>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>woodstox-core-asl</artifactId>
<version>4.4.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
......@@ -86,7 +92,7 @@ limitations under the License.
<plugin>
<groupId>org.apache.rat</groupId>
<artifactId>apache-rat-plugin</artifactId>
<version>0.11</version>
<version>0.12</version>
<configuration>
<excludes>
<exclude>nbactions.xml</exclude>
......
......@@ -15,141 +15,102 @@
*/
package eu.chorevolution.datamodel;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import eu.chorevolution.datamodel.deployment.DeploymentLocation;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
@XmlRootElement
public class Choreography {
private static final JAXBContext CTX;
private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
static {
try {
CTX = JAXBContext.newInstance(Choreography.class);
} catch (final JAXBException e) {
throw new RuntimeException("Could not initialize JAXB", e);
}
}
private String id;
private String tenant;
private List<ServiceGroup> service_groups;
private StatusType status;
private DeploymentLocation location;
private List<ServiceGroup> serviceGroups;
private StatusType status;
private DeploymentLocation location;
public Choreography() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name="service_group")
public List<ServiceGroup> getService_groups() {
return service_groups;
}
public void setService_groups(List<ServiceGroup> service_groups) {
this.service_groups = service_groups;
}
public String getTenant() {
return tenant;
}
public void setTenant(String tenant) {
this.tenant = tenant;
}
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@XmlElement(name = "service_group")
public List<ServiceGroup> getServiceGroups() {
return serviceGroups;
}
public void setServiceGroups(final List<ServiceGroup> serviceGroups) {
this.serviceGroups = serviceGroups;
}
public String getTenant() {
return tenant;
}
public void setTenant(String tenant) {
this.tenant = tenant;
}
public StatusType getStatus() {
return status;
}
public void setStatus(StatusType status) {
this.status = status;
}
public DeploymentLocation getLocation() {
return location;
}
public void setLocation(DeploymentLocation location) {
this.location = location;
}
public String getXML() throws JAXBException {
Marshaller marshaller;
marshaller = getMarshaller(Choreography.class);
Writer writer = new StringWriter();
marshaller.marshal(this, writer);
return writer.toString();
return status;
}
private Marshaller getMarshaller(Class<?> clazz) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
return marshaller;
public void setStatus(StatusType status) {
this.status = status;
}
// based on
// http://arthur.gonigberg.com/2010/04/26/jaxb-generating-schema-from-object-model/
// and
// http://stackoverflow.com/questions/2325388/java-shortest-way-to-pretty-print-to-stdout-a-org-w3c-dom-document
public void printSchema() throws JAXBException, IOException, TransformerException {
// grab the context
JAXBContext context = JAXBContext.newInstance(this.getClass());
final List<DOMResult> results = new ArrayList<DOMResult>();
// generate the schema
context.generateSchema(
// need to define a SchemaOutputResolver to store to
new SchemaOutputResolver() {
@Override
public Result createOutput(String ns, String file) throws IOException {
// save the schema to the list
DOMResult result = new DOMResult();
result.setSystemId(file);
results.add(result);
return result;
}
});
// output schema via System.out
DOMResult domResult = results.get(0);
Document doc = (Document) domResult.getNode();
printDocument(doc, System.out);
// OutputFormat format = new OutputFormat(doc);
// format.setIndenting(true);
// StringWriter writer = new StringWriter();
// XMLSerializer serializer = new XMLSerializer(writer, format);
// serializer.serialize(doc);
// return writer.toString();
public DeploymentLocation getLocation() {
return location;
}
private static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.transform(new DOMSource(doc),
new StreamResult(new OutputStreamWriter(out, "UTF-8")));
public void setLocation(DeploymentLocation location) {
this.location = location;
}
}
\ No newline at end of file
public String getXML() throws JAXBException, XMLStreamException {
Marshaller marshaller = CTX.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
Writer writer = new StringWriter();
marshaller.marshal(this, XML_OUTPUT_FACTORY.createXMLStreamWriter(writer));
return writer.toString();
}
public static Choreography fromXML(final InputStream is) throws JAXBException, XMLStreamException {
return CTX.createUnmarshaller().
unmarshal(XML_INPUT_FACTORY.createXMLStreamReader(is), Choreography.class).getValue();
}
}
......@@ -36,7 +36,7 @@ public class ChoreographyService {
return name;
}
public void setName(String name) {
public void setName(final String name) {
this.name = name;
}
......@@ -45,7 +45,7 @@ public class ChoreographyService {
return roles;
}
public void setRoles(List<String> roles) {
public void setRoles(final List<String> roles) {
this.roles = roles;
}
......
......@@ -29,7 +29,7 @@ public class ExistingService extends ChoreographyService {
return url;
}
public void setUrl(String url) {
public void setUrl(final String url) {
this.url = url;
}
......
......@@ -34,7 +34,7 @@ public class ServiceGroup {
return services;
}
public void setServices(List<ChoreographyService> services) {
public void setServices(final List<ChoreographyService> services) {
this.services = services;
}
......
/*
* Copyright 2016 The CHOReVOLUTION project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package chorspec;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import eu.chorevolution.datamodel.Choreography;
import eu.chorevolution.datamodel.ChoreographyService;
import eu.chorevolution.datamodel.DeployableService;
import eu.chorevolution.datamodel.ExistingService;
import eu.chorevolution.datamodel.PackageType;
import eu.chorevolution.datamodel.ServiceDependency;
import eu.chorevolution.datamodel.ServiceGroup;
import eu.chorevolution.datamodel.ServiceType;
import java.util.List;
public class DeserializationTest {
private ChoreographyService findService(final List<ChoreographyService> services, final String name) {
return services.stream().filter(
(ChoreographyService s) -> name.equals(s.getName())).
findFirst().get();
}
@Test
public void wp5Before() throws Exception {
Choreography choreography = Choreography.fromXML(getClass().getResourceAsStream("/before_wp5.choreospec"));
assertNotNull(choreography);
assertNull(choreography.getId());
assertNotNull(choreography.getServiceGroups());
assertFalse(choreography.getServiceGroups().isEmpty());
assertEquals(1, choreography.getServiceGroups().size());
ServiceGroup serviceGroup = choreography.getServiceGroups().get(0);
assertNotNull(serviceGroup);
assertNotNull(serviceGroup.getServices());
assertFalse(serviceGroup.getServices().isEmpty());
ChoreographyService service = findService(serviceGroup.getServices(), "TrafficInformation");
assertNotNull(service);
assertTrue(service instanceof ExistingService);
ExistingService eService = (ExistingService) service;
assertTrue(eService.getUrl().startsWith("http://"));
service = findService(serviceGroup.getServices(), "cdclientSTApp");
assertNotNull(service);
assertTrue(service instanceof DeployableService);
DeployableService dService = (DeployableService) service;
assertEquals(ServiceType.COORDINATION_DELEGATE, dService.getServiceType());
assertEquals(PackageType.ODE, dService.getPackageType());
assertEquals(1, dService.getInstances());
assertTrue(dService.getPackageUrl().startsWith("http://"));
assertFalse(dService.getDependencies().isEmpty());
assertEquals(1, dService.getDependencies().size());
ServiceDependency dependency = dService.getDependencies().get(0);
assertNotNull(dependency);
assertEquals("cdtouristagent", dependency.getServiceSpecName());
}
}
......@@ -15,13 +15,11 @@
*/
package chorspec;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
import org.junit.Test;
import eu.chorevolution.datamodel.Choreography;
import eu.chorevolution.datamodel.ChoreographyService;
import eu.chorevolution.datamodel.DeployableService;
......@@ -32,14 +30,14 @@ import eu.chorevolution.datamodel.ServiceGroup;
public class SerializationTest {
@Test
public void test1() {
public void test1() throws Exception {
Choreography chor = new Choreography();
chor.setId("test_choreography");
ServiceGroup group = new ServiceGroup();
List<ServiceGroup> groups = new ArrayList<ServiceGroup>();
List<ServiceGroup> groups = new ArrayList<>();
groups.add(group);
chor.setService_groups(groups);
List<ChoreographyService> group_services = new ArrayList<ChoreographyService>();
chor.setServiceGroups(groups);
List<ChoreographyService> group_services = new ArrayList<>();
group.setServices(group_services);
DeployableService dep_srv = new DeployableService();
dep_srv.setInstances(2);
......@@ -52,11 +50,8 @@ public class SerializationTest {
ex1.setName("existing_test_service");
ex1.setUrl("http://www.example.org/test");
group_services.add(ex1);
try {
System.out.println(chor.getXML());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String xml = chor.getXML();
assertNotNull(xml);
}
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
Copyright 2015 The CHOReVOLUTION project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<choreography>
<service_group>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>TrafficInformation</name>
<role>Traffic Information</role>
<url>http://localhost:8080/TrafficInformation/TrafficInformation</url>
</service>
<service xsi:type="deployableService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>consumercdtripplanner</name>
<role>Consumer Trip Planner</role>
<instances>1</instances>
<packageType>WAR</packageType>
<packageUrl>http://localhost:8080/cdtripplanner/cdtripplanner.war</packageUrl>
<serviceType>COORDINATION_DELEGATE</serviceType>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>OSMParking</name>
<role>OSM Parking</role>
<url>http://localhost:8080/OSMParking/OSMParking</url>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>News</name>
<role>News</role>
<url>http://localhost:8080/News/News</url>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Poi</name>
<role>Poi</role>
<url>http://localhost:8080/Poi/Poi</url>
</service>
<service xsi:type="deployableService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>consumercdtouristagent</name>
<role>Consumer Tourist Agent</role>
<instances>1</instances>
<packageType>WAR</packageType>
<packageUrl>http://localhost:8080/cdtouristagent/cdtouristagent.war</packageUrl>
<serviceType>COORDINATION_DELEGATE</serviceType>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>STApp</name>
<role>STApp</role>
<url>http://localhost:8080/STApp/STApp</url>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>JourneyPlanner</name>
<role>Journey Planner</role>
<url>http://localhost:8080/JourneyPlanner/JourneyPlanner</url>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>PublicTransportation</name>
<role>Public Transportation</role>
<url>http://localhost:8080/PublicTransportation/PublicTransportation</url>
</service>
<service xsi:type="existingService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>Weather</name>
<role>Weather</role>
<url>http://localhost:8080/Weather/Weather</url>
</service>
<service xsi:type="deployableService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>cdtripplanner</name>
<role>Trip Planner</role>
<dependency>
<serviceSpecName>consumercdtripplanner</serviceSpecName>
<serviceSpecRole>Consumer Trip Planner</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>TrafficInformation</serviceSpecName>
<serviceSpecRole>Traffic Information</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>JourneyPlanner</serviceSpecName>
<serviceSpecRole>Journey Planner</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>OSMParking</serviceSpecName>
<serviceSpecRole>OSM Parking</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>PublicTransportation</serviceSpecName>
<serviceSpecRole>Public Transportation</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>Weather</serviceSpecName>
<serviceSpecRole>Weather</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>cdtouristagent</serviceSpecName>
<serviceSpecRole>Tourist Agent</serviceSpecRole>
</dependency>
<instances>1</instances>
<packageType>ODE</packageType>
<packageUrl>http://localhost:8080/cdtripplanner/cdtripplanner.tar.gz</packageUrl>
<serviceType>COORDINATION_DELEGATE</serviceType>
</service>
<service xsi:type="deployableService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>cdtouristagent</name>
<role>Tourist Agent</role>
<dependency>
<serviceSpecName>consumercdtouristagent</serviceSpecName>
<serviceSpecRole>Consumer Tourist Agent</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>Poi</serviceSpecName>
<serviceSpecRole>Poi</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>News</serviceSpecName>
<serviceSpecRole>News</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>cdtripplanner</serviceSpecName>
<serviceSpecRole>Trip Planner</serviceSpecRole>
</dependency>
<dependency>
<serviceSpecName>cdclientSTApp</serviceSpecName>
<serviceSpecRole>CD Client STApp</serviceSpecRole>
</dependency>
<instances>1</instances>
<packageType>ODE</packageType>
<packageUrl>http://localhost:8080/cdtouristagent/cdtouristagent.tar.gz</packageUrl>
<serviceType>COORDINATION_DELEGATE</serviceType>
</service>
<service xsi:type="deployableService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<name>cdclientSTApp</name>
<role>CD Client STApp</role>
<dependency>
<serviceSpecName>cdtouristagent</serviceSpecName>
<serviceSpecRole>Tourist Agent</serviceSpecRole>
</dependency>
<instances>1</instances>
<packageType>ODE</packageType>
<packageUrl>http://localhost:8080/STApp/STApp.tar.gz</packageUrl>
<serviceType>COORDINATION_DELEGATE</serviceType>
</service>
</service_group>
</choreography>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment