Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Commit

Permalink
Added missing tests for View API.
Browse files Browse the repository at this point in the history
  • Loading branch information
keiono committed Jun 26, 2015
1 parent 623a602 commit 40bab51
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.HashSet;
import java.util.Set;
import java.util.Spliterator;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import javax.inject.Singleton;
Expand Down Expand Up @@ -94,7 +95,8 @@ private final void initLexicon() {
this.lexicon = getLexicon();
nodeLexicon = lexicon.getAllDescendants(BasicVisualLexicon.NODE);
edgeLexicon = lexicon.getAllDescendants(BasicVisualLexicon.EDGE);
networkLexicon = lexicon.getAllDescendants(BasicVisualLexicon.NETWORK);
networkLexicon = lexicon.getAllDescendants(BasicVisualLexicon.NETWORK).stream()
.filter(vp->vp.getIdString().startsWith("NETWORK")).collect(Collectors.toSet());;

}
/**
Expand Down Expand Up @@ -623,6 +625,14 @@ public Response updateNetworkView(@PathParam("networkId") Long networkId, @PathP
}


@GET
@Path("/{viewId}/network")
@Produces(MediaType.APPLICATION_JSON)
public Response getNetworkVisualProps(@PathParam("networkId") Long networkId, @PathParam("viewId") Long viewId) {
return this.getViews(networkId, viewId, "network", null);
}


/**
* @summary Get view object for the specified type (node or edge)
*
Expand Down Expand Up @@ -697,7 +707,7 @@ public String getSingleVisualPropertyValue(@PathParam("networkId") Long networkI
@GET
@Path("/{viewId}/network/{visualProperty}")
@Produces(MediaType.APPLICATION_JSON)
public String getNetworkView(@PathParam("networkId") Long networkId, @PathParam("viewId") Long viewId,
public String getNetworkVisualProp(@PathParam("networkId") Long networkId, @PathParam("viewId") Long viewId,
@PathParam("visualProperty") String visualProperty) {
final CyNetworkView networkView = getView(networkId, viewId);

Expand Down Expand Up @@ -739,11 +749,12 @@ private String getSingleVp(final String visualProperty, final View<? extends CyI
@GET
@Path("/{viewId}/{objectType}")
@Produces(MediaType.APPLICATION_JSON)
public String getViews(@PathParam("networkId") Long networkId, @PathParam("viewId") Long viewId,
public Response getViews(@PathParam("networkId") Long networkId, @PathParam("viewId") Long viewId,
@PathParam("objectType") String objectType, @QueryParam("visualProperty") String visualProperty) {

if(visualProperty != null) {
return getSingleVisualPropertyOfViews(networkId, viewId, objectType, visualProperty);
final String result = getSingleVisualPropertyOfViews(networkId, viewId, objectType, visualProperty);
return Response.ok(result).build();
}

if(nodeLexicon == null) {
Expand All @@ -756,10 +767,11 @@ public String getViews(@PathParam("networkId") Long networkId, @PathParam("viewI
} else if(objectType.equals("edges")) {
vps = edgeLexicon;
} else if(objectType.equals("network")) {
System.out.println("=============This is NETWORK = ");
vps = networkLexicon;
}

return getViewForVPList(networkId, viewId, objectType, vps);
return Response.ok(getViewForVPList(networkId, viewId, objectType, vps)).build();
}

private final String getViewForVPList(final Long networkId, final Long viewId, final String objectType, Collection<VisualProperty<?>> vps) {
Expand Down
145 changes: 134 additions & 11 deletions src/test/java/org/cytoscape/rest/service/NetworkViewResourceTest.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package org.cytoscape.rest.service;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.awt.Color;
import java.awt.Paint;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javassist.bytecode.analysis.ControlFlow.Node;
import java.util.stream.Collectors;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.cytoscape.model.CyEdge;
import org.cytoscape.model.CyNetwork;
import org.cytoscape.model.CyNode;
import org.cytoscape.rest.internal.resource.GlobalTableResource;
import org.cytoscape.rest.internal.resource.NetworkViewResource;
import org.cytoscape.view.model.View;
import org.cytoscape.view.presentation.property.BasicVisualLexicon;
import org.glassfish.jersey.internal.util.collection.Values;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand Down Expand Up @@ -68,7 +69,7 @@ public void testCreateNetworkView() throws Exception {
}

@Test
public void testGetViews() throws Exception {
public void testGetNetworkViews() throws Exception {

final Long suid = network.getSUID();
final Long viewSuid = view.getSUID();
Expand All @@ -86,6 +87,65 @@ public void testGetViews() throws Exception {
assertEquals(viewSuid, Long.valueOf(root.get(0).asLong()));
}

@Test
public void testGetNetworkVisualProp() throws Exception {

final Long suid = network.getSUID();
final Long viewSuid = view.getSUID();

Response result = target("/v1/networks/" + suid.toString() + "/views/" + viewSuid + "/network/NETWORK_SIZE").request().get();
assertNotNull(result);
String body = result.readEntity(String.class);
System.out.println("Result = " + result);

JsonNode root = mapper.readTree(body);
assertNotNull(root);
System.out.println(root);
assertTrue(root.isObject());
assertEquals((Double)550.0, (Double)root.get("value").asDouble());
}

@Test
public void testGetViewsOf() throws Exception {

final Long suid = network.getSUID();
final Long viewSuid = view.getSUID();

Response result = target("/v1/networks/" + suid.toString() + "/views/" + viewSuid + "/nodes").request().get();
assertNotNull(result);
String body = result.readEntity(String.class);
System.out.println("Result = " + result);

JsonNode root = mapper.readTree(body);
assertNotNull(root);
System.out.println(root);
assertTrue(root.isArray());
assertEquals(view.getNodeViews().size(), root.size());

result = target("/v1/networks/" + suid.toString() + "/views/" + viewSuid + "/edges").request().get();
assertNotNull(result);
body = result.readEntity(String.class);
System.out.println("Result = " + result);

root = mapper.readTree(body);
assertNotNull(root);
System.out.println(root);
assertTrue(root.isArray());
assertEquals(view.getEdgeViews().size(), root.size());


result = target("/v1/networks/" + suid.toString() + "/views/" + viewSuid + "/network").request().get();
assertNotNull(result);
body = result.readEntity(String.class);
System.out.println("Network Result = " + result);

root = mapper.readTree(body);
assertNotNull(root);
System.out.println(root);
assertTrue(root.isArray());
assertEquals(12, root.size());
}


@Test
public void testGetNetworkView() throws Exception {
Expand Down Expand Up @@ -288,6 +348,69 @@ private void testViewUpdates(final String newVal, final Long suid, final Long vi
}



private final String createViewJson(Map<String, Long> idmap) throws Exception {
final JsonFactory factory = new JsonFactory();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = null;

generator = factory.createGenerator(stream);
generator.writeStartArray();

// Node 1 Updates:
generator.writeStartObject();
generator.writeNumberField("SUID", idmap.get("n1"));

generator.writeArrayFieldStart("view");

generator.writeStartObject();
generator.writeStringField("visualProperty", "NODE_FILL_COLOR");
generator.writeStringField("value", "yellow");
generator.writeEndObject();

generator.writeStartObject();
generator.writeStringField("visualProperty", "NODE_SIZE");
generator.writeNumberField("value", 130);
generator.writeEndObject();

generator.writeEndArray();

generator.writeEndObject();

generator.writeEndArray();
generator.close();
final String result = stream.toString("UTF-8");
stream.close();
return result;
}
@Test
public void testUpdateViews() throws Exception {
final List<CyNode> nodes = network.getNodeList();

Map<String, Long> idmap = nodes.stream()
.collect(Collectors.toMap(
node->network.getRow(node).get(CyNetwork.NAME, String.class),
n->n.getSUID()
)
);

System.out.println("Nodes: " + idmap);
final Long suid = network.getSUID();
final Long viewSuid = view.getSUID();
final String newVal = createViewJson(idmap);
Entity<String> entity = Entity.entity(newVal, MediaType.APPLICATION_JSON_TYPE);
Response result = target("/v1/networks/" + suid.toString() + "/views/" + viewSuid + "/nodes").request().put(entity);
assertNotNull(result);
System.out.println("res: " + result.toString());
assertFalse(result.getStatus() == 500);
assertEquals(200, result.getStatus());
final View<CyNode> nv1 = view.getNodeView(network.getNode(idmap.get("n1")));

assertEquals((Double)130.0, nv1.getVisualProperty(BasicVisualLexicon.NODE_SIZE));
assertEquals(Color.YELLOW, nv1.getVisualProperty(BasicVisualLexicon.NODE_FILL_COLOR));
}


@Test
public void testDeleteAllNetworkViews() throws Exception {

Expand Down

0 comments on commit 40bab51

Please sign in to comment.