Commit 734fa63a authored by Gwenael Cattez's avatar Gwenael Cattez
Browse files

Add JUnit tests (one block test)

parent b383c12a
......@@ -39,10 +39,6 @@
<name>OW2 FraSCAti Web Explorer</name>
<properties>
<org.apache.cxf.jaxrs.provider.JSONProvider>org.ow2.frascati.demo.webexplorer.service.resolver.MyResolver</org.apache.cxf.jaxrs.provider.JSONProvider>
</properties>
<!-- ============= -->
<!-- Build -->
<!-- ============= -->
......@@ -106,6 +102,14 @@
<artifactId>commons-io</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>org.ow2.frascati.demo</groupId>
<artifactId>common-onoffdevice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
......@@ -28,14 +28,19 @@ package org.ow2.frascati.webexplorer.service;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.jdom.JDOMException;
import org.json.JSONException;
import org.json.JSONObject;
import org.osoa.sca.annotations.EagerInit;
import org.osoa.sca.annotations.Init;
import org.osoa.sca.annotations.Scope;
import org.ow2.frascati.binding.factory.AbstractBindingFactoryProcessor;
import org.ow2.frascati.webexplorer.tree.AbstractNode;
import org.ow2.frascati.webexplorer.tree.asbstractnodes.AbstractComponentTreeNode;
import org.ow2.frascati.webexplorer.tree.asbstractnodes.AbstractServiceReferenceTreeNode;
......@@ -54,25 +59,11 @@ import org.ow2.frascati.webexplorer.tree.utils.TreeUtils;
*
*/
@Scope("COMPOSITE")
public class WebExplorerImpl implements WebExplorer
public class WebExplorerImpl implements WebExplorerItf
{
private static final String DEFAULT_DOMAIN_ID="0_0";
public String getTree(UriInfo uriInfo) throws JSONException, JDOMException, IOException
public String getTree() throws JSONException, JDOMException, IOException
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode defaultDomain=root.getDomain(DEFAULT_DOMAIN_ID);
if(defaultDomain==null)
{
URI baseURI = uriInfo.getBaseUri();
String host = baseURI.getHost();
int port = baseURI.getPort();
String localBaseUri = "http://" + host + ":" + port;
defaultDomain = new DomainTreeNode(root,"Default Domain",localBaseUri,"FFFFFF");
root.addDomain(defaultDomain);
}
JSONObject object = root.toJSON();
return object.toString();
}
......@@ -155,10 +146,13 @@ public class WebExplorerImpl implements WebExplorer
if(node.getType()==ComponentTreeNode.COMPONENT_TYPE || node.getType()==CompositeTreeNode.COMPOSITE_TYPE)
{
AbstractComponentTreeNode abstractComponentTreeNode=(AbstractComponentTreeNode) node;
abstractComponentTreeNode.stop();
AbstractNode abstractNode=abstractComponentTreeNode.refreshNode();
JSONObject object=abstractNode.toJSON();
return object.toString();
boolean isStopped=abstractComponentTreeNode.stop();
if(isStopped)
{
AbstractNode abstractNode=abstractComponentTreeNode.refreshNode();
JSONObject object=abstractNode.toJSON();
return object.toString();
}
}
return "";
}
......@@ -181,7 +175,7 @@ public class WebExplorerImpl implements WebExplorer
return "not a method";
}
MethodTreeNode methodTreeNode=(MethodTreeNode) node;
String nodeId=TreeUtils.getParentId(node.getId());
String nodeId=TreeUtils.getParentId(methodTreeNode.getId());
node=root.getNode(nodeId);
nodeId=TreeUtils.getParentId(node.getId());
node=root.getNode(nodeId);
......
......@@ -37,9 +37,7 @@ import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
import org.jdom.JDOMException;
import org.json.JSONException;
......@@ -49,12 +47,12 @@ import org.osoa.sca.annotations.Service;
*
*/
@Service
public interface WebExplorer
public interface WebExplorerItf
{
@GET
@Path("/tree")
@Produces("application/json")
public String getTree(@Context UriInfo uriInfo) throws JSONException, JDOMException, IOException;
public String getTree() throws JSONException, JDOMException, IOException;
@GET
@Path("/domain")
......
......@@ -47,7 +47,7 @@ public class UploadServiceImpl implements UploadServiceItf
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomain(domainId);
Deployment deployment=domain.getDeploymentClient();
Deployment deployment=domain.getdeploymentClient();
String entries=deployment.getCompositeEntriesFromJar(encodedComposite);
return entries;
}
......@@ -63,7 +63,7 @@ public class UploadServiceImpl implements UploadServiceItf
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomain(domainId);
Deployment deployment=domain.getDeploymentClient();
Deployment deployment=domain.getdeploymentClient();
deployment.deployContribution(encodedContribution);
domain.refreshNode();
JSONObject object = domain.toJSON();
......@@ -81,7 +81,7 @@ public class UploadServiceImpl implements UploadServiceItf
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomain(domainId);
Deployment deployment=domain.getDeploymentClient();
Deployment deployment=domain.getdeploymentClient();
deployment.deployComposite(compositeName, encodedComposite);
domain.refreshNode();
JSONObject object = domain.toJSON();
......
......@@ -40,7 +40,7 @@ public interface AbstractNode
{
public String getnextId();
public String getFrascatiPath();
public String getdomainURI();
public String getDomainURI();
public AbstractNode refreshNode() throws JDOMException, IOException;
public JSONObject toJSON() throws JSONException;
}
......@@ -73,9 +73,20 @@ public abstract class AbstractComponentTreeNode extends AbstractIndexableTreeNod
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
introspection.invokeMethod(this.getFrascatiPath(), null);
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
if(method.equals("start"))
{
introspection.startComponent(this.getFrascatiPath());
}
else if(method.equals("stop"))
{
introspection.stopComponent(this.getFrascatiPath());
}
else
{
return false;
}
return true;
}
catch(Exception exception)
......@@ -122,7 +133,6 @@ public abstract class AbstractComponentTreeNode extends AbstractIndexableTreeNod
for(Element tmpComponent : components)
{
componentName=tmpComponent.getAttributeValue("name");
System.out.println("component name : "+componentName);
componentNode=new ComponentTreeNode(this,componentName,position);
position++;
this.addTreeNode(componentNode);
......
......@@ -116,8 +116,8 @@ public abstract class AbstractServiceReferenceTreeNode extends AbstractIndexable
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
introspection.addBinding(this.getFrascatiPath(), params);
return true;
}
......@@ -137,8 +137,8 @@ public abstract class AbstractServiceReferenceTreeNode extends AbstractIndexable
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
introspection.removeBinding(this.getFrascatiPath(), position);
return true;
}
......@@ -159,8 +159,8 @@ public abstract class AbstractServiceReferenceTreeNode extends AbstractIndexable
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
introspection.setBindingAttribute(this.getFrascatiPath(), position, attribute, newValue);
return true;
}
......
......@@ -63,7 +63,7 @@ public abstract class AbstractTreeNode implements AbstractNode
this.text=text;
this.image=image;
this.type=type;
this.domainURI=parentNode.getdomainURI();
this.domainURI=parentNode.getDomainURI();
this.frascatiPath=parentNode.getFrascatiPath()+"/"+this.text;
this.item=new LinkedList<AbstractTreeNode>();
this.parentNode=parentNode;
......@@ -204,8 +204,7 @@ public abstract class AbstractTreeNode implements AbstractNode
return type;
}
/*minor case to avoid JSON marshalling*/
public String getdomainURI()
public String getDomainURI()
{
return domainURI;
}
......@@ -215,7 +214,6 @@ public abstract class AbstractTreeNode implements AbstractNode
this.domainURI=domainURI;
}
/*minor case to avoid JSON marshalling*/
public String getFrascatiPath()
{
return frascatiPath;
......
......@@ -95,8 +95,8 @@ public class MethodTreeNode extends AbstractTreeNode
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
String result=introspection.invokeMethod(abstractServiceReferenceTreeNode.getFrascatiPath(), params);
return result;
}
......
......@@ -36,6 +36,7 @@ import org.ow2.frascati.webexplorer.tree.asbstractnodes.AbstractIndexableTreeNod
import org.ow2.frascati.webexplorer.tree.asbstractnodes.AbstractTreeNode;
import org.ow2.frascati.webexplorer.tree.nodes.domain.DomainTreeNode;
import org.ow2.frascati.webexplorer.tree.root.RootTreeNode;
import org.ow2.frascati.webexplorer.tree.utils.TreeUtils;
/**
*
......@@ -74,17 +75,17 @@ public class PropertyTreeNode extends AbstractIndexableTreeNode
try
{
RootTreeNode root = RootTreeNode.getInstance();
DomainTreeNode domain=root.getDomainByURI(this.getdomainURI());
RemoteScaDomain introspection=domain.getIntrospectionClient();
DomainTreeNode domain=root.getDomainByURI(this.getDomainURI());
RemoteScaDomain introspection=domain.getintrospectionClient();
String parentId= this.parentId;
String parentId= TreeUtils.getParentId(this.getId());
AbstractTreeNode parentNode=root.getNode(parentId);
boolean result=introspection.setProperty(parentNode.getFrascatiPath(),this.getId(), newValue);
boolean result=introspection.setProperty(parentNode.getFrascatiPath(),this.getText(), newValue);
return result;
}
catch(Exception exception)
{
return false;
return false;
}
}
......
......@@ -95,7 +95,7 @@ public class DomainTreeNode extends AbstractTreeNode
public String getText()
{
return this.domainName+" ("+this.getdomainURI()+")";
return this.domainName+" ("+this.getDomainURI()+")";
}
public String getFrascatiPath()
......@@ -109,12 +109,14 @@ public class DomainTreeNode extends AbstractTreeNode
}
public Deployment getDeploymentClient()
/*Lower case to avoid JSON marshalling*/
public Deployment getdeploymentClient()
{
return deployment;
}
public RemoteScaDomain getIntrospectionClient()
/*Lower case to avoid JSON marshalling*/
public RemoteScaDomain getintrospectionClient()
{
return introspection;
}
......
......@@ -34,6 +34,7 @@ import java.util.List;
import org.jdom.JDOMException;
import org.json.JSONException;
import org.json.JSONObject;
import org.ow2.frascati.binding.factory.AbstractBindingFactoryProcessor;
import org.ow2.frascati.webexplorer.tree.AbstractNode;
import org.ow2.frascati.webexplorer.tree.asbstractnodes.AbstractTreeNode;
import org.ow2.frascati.webexplorer.tree.nodes.domain.DomainTreeNode;
......@@ -49,16 +50,30 @@ public class RootTreeNode implements AbstractNode
private static RootTreeNode instance;
private List<DomainTreeNode> domainList;
private RootTreeNode()
private RootTreeNode() throws JDOMException, IOException, JSONException, InterruptedException
{
this.domainList=new LinkedList<DomainTreeNode>();
String bindingURIProperty=System.getProperty(AbstractBindingFactoryProcessor.BINDING_URI_BASE_PROPERTY_NAME);
if(bindingURIProperty==null)
{
bindingURIProperty=AbstractBindingFactoryProcessor.BINDING_URI_BASE_DEFAULT_VALUE;
}
DomainTreeNode defaultDomain = new DomainTreeNode(this,"Default Domain",bindingURIProperty,"FFFFFF");
this.domainList.add(defaultDomain);
}
public static RootTreeNode getInstance()
{
if(instance==null)
{
instance=new RootTreeNode();
try
{
instance=new RootTreeNode();
}
catch (Exception e)
{
instance=null;
}
}
return instance;
}
......@@ -99,7 +114,6 @@ public class RootTreeNode implements AbstractNode
for(DomainTreeNode tmpDomain : domainList)
{
tmpDomainId=tmpDomain.getId();
System.out.println("root "+tmpDomain.getId());
if(tmpDomainId.equals(domainId))
{
return tmpDomain;
......@@ -113,7 +127,7 @@ public class RootTreeNode implements AbstractNode
String tmpDomainURI;
for(DomainTreeNode tmpDomain : domainList)
{
tmpDomainURI=tmpDomain.getdomainURI();
tmpDomainURI=tmpDomain.getDomainURI();
if(tmpDomainURI.equals(domainURI))
{
return tmpDomain;
......@@ -182,7 +196,7 @@ public class RootTreeNode implements AbstractNode
return "";
}
public String getdomainURI()
public String getDomainURI()
{
return "";
}
......
......@@ -25,6 +25,9 @@
<composite xmlns="http://www.osoa.org/xmlns/sca/1.0"
xmlns:frascati="org/ow2/frascati" name="org.ow2.frascati.FraSCAti">
<service name="webexplorer" promote="web-explorer/service"/>
<service name="webexplorerUpload" promote="web-explorer/upload"/>
<component name="web-explorer">
<implementation.composite name="frascati:WebExplorer.composite" />
......
......@@ -40,11 +40,10 @@
<service name="service" promote="service/WebExplorerService">
<frascati:binding.rest uri="/WebExplorerService"/>
</service>
<service name="uploadService" promote="upload/uploadService">
<frascati:binding.rest uri="/WebExplorerService/upload"/>
<service name="upload" promote="upload/uploadService">
<frascati:binding.rest uri="/WebExplorerService/upload"/>
</service>
<component name="WebExplorer">
<service name="Velocity">
<interface.java interface="javax.servlet.Servlet" />
......@@ -60,7 +59,7 @@
<component name="service">
<implementation.java class="org.ow2.frascati.webexplorer.service.WebExplorerImpl"/>
<service name="WebExplorerService">
<interface.java interface="org.ow2.frascati.webexplorer.service.WebExplorer"/>
<interface.java interface="org.ow2.frascati.webexplorer.service.WebExplorerItf"/>
</service>
</component>
......
......@@ -28,27 +28,202 @@
package org.ow2.frascati;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.ws.rs.core.MultivaluedMap;
import org.apache.cxf.jaxrs.impl.MetadataMap;
import org.jdom.JDOMException;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.ow2.frascati.remote.introspection.FileUtil;
import org.ow2.frascati.util.FrascatiException;
import org.ow2.frascati.webexplorer.service.WebExplorerItf;
import org.ow2.frascati.webexplorer.service.upload.UploadServiceItf;
/**
*
*/
public class WebExplorerTestCase //extends FraSCAtiTestCase
{
protected FraSCAti frascati;
@Before
public void init() throws FrascatiException
{
System.setProperty("org.ow2.frascati.bootstrap", "org.ow2.frascati.bootstrap.FraSCAtiWebExplorer");
this.frascati=FraSCAti.newFraSCAti();
}
// public static final String COMPOSITE = "org.ow2.frascati.FraSCAti.composite";
//
// @Override
// public String getComposite()
// {
// return COMPOSITE;
// }
@Test
public void test()
public void test() throws FrascatiException, URISyntaxException, IOException, JSONException, JDOMException, InterruptedException
{
// UploadServiceItf uploadService=getService(UploadServiceItf.class, "uploadService");
// WebExplorer webExplorer=getService(WebExplorer.class, "service");
org.objectweb.fractal.api.Component frascatiComponent=this.frascati.getComposite("org.ow2.frascati.FraSCAti");
WebExplorerItf webExplorer=this.frascati.getService(frascatiComponent, "webexplorer", WebExplorerItf.class);
assertNotNull(webExplorer);
/*getTree*/
String treeString=webExplorer.getTree();
JSONObject tree=new JSONObject(treeString);
assertNotNull(tree);
JSONObject rootDomain=getItem(tree,"text","Root");
assertNotNull(rootDomain);
/*only one default domain*/
JSONArray domains=rootDomain.getJSONArray("item");
assertEquals(domains.length(),1);
JSONObject defaultDomain=domains.getJSONObject(0);
UploadServiceItf uploader=this.frascati.getService(frascatiComponent, "webexplorerUpload", UploadServiceItf.class);
assertNotNull(uploader);
/*addDomain*/
String defaultDomainURI=defaultDomain.getString("domainURI");
String testDomainString=webExplorer.addDomain("test", defaultDomainURI, "FF0000");
JSONObject testDomain=new JSONObject(testDomainString);
assertNotNull(testDomain);
treeString=webExplorer.getTree();
tree=new JSONObject(treeString);
rootDomain=getItem(tree,"text","Root");
domains=rootDomain.getJSONArray("item");
assertEquals(domains.length(),2);
/*remove domain*/
String testDomainId=testDomain.getString("id");
treeString=webExplorer.removeDomain(testDomainId);
tree=new JSONObject(treeString);
rootDomain=getItem(tree,"text","Root");
domains=rootDomain.getJSONArray("item");
assertEquals(domains.length(),1);
/*deployContribution*/
URL url=this.getClass().getClassLoader().getResource("alarm.zip");
File contributionFile=new File(url.toURI());
String encodedContribution=FileUtil.getStringFromFile(contributionFile);
String defaultDomainId=defaultDomain.getString("id");
String domainString=uploader.deployContribution(defaultDomainId, encodedContribution);
JSONObject domain=new JSONObject(domainString);
JSONObject alarmComposite=getItem(domain, "text", "alarm");
assertNotNull(alarmComposite);
/*refreshTreeNode*/
alarmComposite=getItem(defaultDomain, "text", "alarm");
assertNull(alarmComposite);
String defaultDomainString=webExplorer.refreshTreeNode(defaultDomainId);
defaultDomain=new JSONObject(defaultDomainString);
alarmComposite=getItem(defaultDomain, "text", "alarm");
assertNotNull(alarmComposite);
/*stopComponent*/
String alarmStatus=alarmComposite.getString("status");
assertEquals(alarmStatus, "STARTED");
String alarmId=alarmComposite.getString("id");
webExplorer.stopComponent(alarmId);
String alarmCompositeString=webExplorer.refreshTreeNode(alarmId);
alarmComposite=new JSONObject(alarmCompositeString);
alarmStatus=alarmComposite.getString("status");
assertEquals(alarmStatus, "STOPPED");
/*startComponent*/
webExplorer.startComponent(alarmId);
alarmCompositeString=webExplorer.refreshTreeNode(alarmId);
alarmComposite=new JSONObject(alarmCompositeString);
alarmStatus=alarmComposite.getString("status");
assertEquals(alarmStatus, "STARTED");
/*invokeMethod*/
JSONObject alarmControl=getItem(alarmComposite, "text", "alarmControl");