Permalink
Browse files

Removing outdated test cases, enabling autoreconnect in demo

  • Loading branch information...
1 parent aa4fd80 commit 5904936e0c0f50f9db44384fad849f15595f89da @theterg theterg committed Jun 18, 2012
View
7 SimpleSwarmExample/simpleswarmexample/SimpleSwarmExampleApplication.java
@@ -40,7 +40,7 @@ public void run(Map<Object, Object> services) {
try {
//create a session
- producersession = SwarmClientFactory.createProductionSession(hostname, participation_key, resource_id, false, false, swarms);
+ producersession = SwarmClientFactory.createProductionSession(hostname, participation_key, resource_id, true, true, swarms);
//add a listener for incoming messages
producersession.addListener(new ISwarmJsonMessageListener() {
@@ -72,10 +72,13 @@ public void messageRecieved(Map<String, ?> payload, String fromSwarm, String fro
map.put("message", "Java client hi");
try {
while(true){
- producersession.send(map);
+ if (producersession != null && producersession.isConnected()){
+ producersession.send(map);
+ }
Thread.sleep(1000);
}
} catch (InterruptedException e){
+ ilog("closing session");
producersession.close();
return;
}
View
1 com.buglabs.bug.swarm.client/META-INF/MANIFEST.MF
@@ -6,7 +6,6 @@ Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: Bug Labs, Inc.
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Import-Package:
- junit.framework;resolution:=optional,
org.codehaus.jackson,
org.codehaus.jackson.map,
org.touge.restclient;version="1.0"
View
122 ...rm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmAPIKeyWSAPITests.java
@@ -1,122 +0,0 @@
-package com.buglabs.bug.swarm.client.test.configuration;
-
-import java.io.IOException;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmKeysClient;
-import com.buglabs.bug.swarm.client.ISwarmKeysClient.KeyType;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.model.SwarmKey;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-
-/**
- * Tests for the Swarm API Keys WS API.
- *
- * @author kgilmer
- *
- */
-public class SwarmAPIKeyWSAPITests extends TestCase {
-
- @Override
- protected void setUp() throws Exception {
-
- }
-
- @Override
- protected void tearDown() throws Exception {
-
- }
-
- /**
- * Test that client can add keys.
- * @throws IOException
- */
- public void testAddKeys() throws IOException {
- assertNotNull(AccountConfig.getConfiguration().getHostname(Protocol.HTTP));
-
- String hostname = AccountConfig.getConfiguration().getHostname(Protocol.HTTP);
- String username = "connector_test";
- String password = "connector_test";
-
- ISwarmKeysClient client = SwarmClientFactory.getAPIKeyClient(hostname);
-
- assertNotNull(client);
-
- List<SwarmKey> keys = client.create(username, password, null);
-
- assertNotNull(keys);
- assertTrue(keys.size() == 2);
-
- for (SwarmKey key : keys) {
- System.out.println(key.toString());
- assertNotNull(key.getCreatedAt());
- assertNotNull(key.getKey());
- assertNotNull(key.getUserId());
- assertNotNull(key.getType());
- assertTrue(key.isActive());
- }
-
- keys = client.create(username, password, KeyType.CONFIGURATION);
-
- assertNotNull(keys);
- assertTrue(keys.size() == 1);
-
- for (SwarmKey key : keys) {
- System.out.println(key.toString());
- assertNotNull(key.getCreatedAt());
- assertNotNull(key.getKey());
- assertNotNull(key.getUserId());
- assertNotNull(key.getType() == KeyType.CONFIGURATION);
- assertTrue(key.isActive());
- }
-
- keys = client.create(username, password, KeyType.PARTICIPATION);
-
- assertNotNull(keys);
- assertTrue(keys.size() == 1);
-
- for (SwarmKey key : keys) {
- System.out.println(key.toString());
- assertNotNull(key.getCreatedAt());
- assertNotNull(key.getKey());
- assertNotNull(key.getUserId());
- assertNotNull(key.getType() == KeyType.PARTICIPATION);
- assertTrue(key.isActive());
- }
- }
-
- /**
- * Test that client can list keys.
- * @throws IOException
- */
- public void testListKeys() throws IOException {
- assertNotNull(AccountConfig.getConfiguration().getHostname(Protocol.HTTP));
-
- String hostname = AccountConfig.getConfiguration().getHostname(Protocol.HTTP);
- String username = "connector_test";
- String password = "connector_test";
-
- ISwarmKeysClient client = SwarmClientFactory.getAPIKeyClient(hostname);
-
- assertNotNull(client);
-
- List<SwarmKey> keys = client.list(username, password, null);
-
- assertNotNull(keys);
- assertTrue(keys.size() > 1);
-
- for (SwarmKey key : keys) {
- System.out.println(key.toString());
- assertNotNull(key.getCreatedAt());
- assertNotNull(key.getKey());
- assertNotNull(key.getUserId());
- assertNotNull(key.getType());
- assertTrue(key.isActive());
- }
-
- }
-}
View
283 ...lient/test/com/buglabs/bug/swarm/client/test/configuration/SwarmInvitationWSAPITests.java
@@ -1,283 +0,0 @@
-package com.buglabs.bug.swarm.client.test.configuration;
-
-import java.io.IOException;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationResponse;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationState;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.model.Invitation;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-/**
- * Unit tests for ISwarmInvitationWSClient implementation.
- *
- * @author kgilmer
- *
- */
-public class SwarmInvitationWSAPITests extends TestCase {
-
- private static final String description = "invite test description";
-
- /* (non-Javadoc)
- * @see junit.framework.TestCase#setUp()
- *
- * For user 1:
- * - Delete all existing swarms
- * - Delete all existing resources
- * - Create swarm, store id
- * - Create resource, store id
- *
- * For user 2:
- * - Delete all existing swarms
- * - Delete all existing resources
- * - Create new resource, store id
- *
- */
- @Override
- protected void setUp() throws Exception {
- assertNotNull(AccountConfig.getConfiguration());
- assertNotNull(AccountConfig.getConfiguration2());
-
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- List<SwarmModel> swarms = client.list();
-
- for (SwarmModel sm : swarms) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
- String id = client.create(AccountConfig.generateRandomSwarmName(), true, AccountConfig.getTestSwarmDescription());
- AccountConfig.testSwarmId = id;
-
- UserResourceModel urc = client.getUserResourceClient().add(AccountConfig.generateRandomResourceName(), "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource = urc;
-
- //Determine that 2nd user can connect and delete any existing swarms.
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- for (SwarmModel sm : client2.list()) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration2().getUsername())) {
- client2.destroy(sm.getId());
- }
- }
-
- for (UserResourceModel ur : client2.getUserResourceClient().list())
- client2.getUserResourceClient().destroy(ur.getResourceId());
-
- urc = client2.getUserResourceClient().add(AccountConfig.generateRandomResourceName(), "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource2 = urc;
-
- //Confirm that original user still has swarm.
- assertTrue(client.list().size() == 1);
- }
-
- /**
- * Test sending an invite. Only confirms that server returns invite object as specified in documentation.
- *
- * @throws IOException
- */
- public void testSendInvite() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- assertNotNull(client);
- assertNotNull(client.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
-
- Invitation invite = client.getSwarmInviteClient().send(
- AccountConfig.testSwarmId,
- AccountConfig.getConfiguration2().getUsername(),
- AccountConfig.testUserResource2.getResourceId(),
- MemberType.CONSUMER,
- description);
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
-
- AccountConfig.testInviteId = invite.getId();
- }
-
- /**
- * Test sending invitations. Check that listing existing invites changes appropriately when
- * we create new invitations.
- *
- * @throws IOException
- */
- public void testListSentInvitations() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(client);
- assertNotNull(client.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(AccountConfig.testInviteId);
-
- List<Invitation> sentInvites = client.getSwarmInviteClient().getSentInvitations(AccountConfig.testSwarmId);
-
- assertNotNull(sentInvites);
- assertTrue(sentInvites.isEmpty() == true);
-
- testSendInvite();
- sentInvites = client.getSwarmInviteClient().getSentInvitations(AccountConfig.testSwarmId);
-
- assertNotNull(sentInvites);
- assertTrue(sentInvites.isEmpty() == false);
- assertTrue(sentInvites.size() == 1);
- Invitation invite = sentInvites.get(0);
- assertTrue(invite.getId().equals(AccountConfig.testInviteId));
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
- }
-
- /**
- * Test recieving invitations. Confirm that target of invitation properly receives invite.
- *
- * @throws IOException
- */
- public void testListRecievedInvitations() throws IOException {
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- assertNotNull(client2);
- assertNotNull(client2.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(AccountConfig.testInviteId);
-
- testSendInvite();
- List<Invitation> receivedInvites = client2.getSwarmInviteClient().getRecievedInvitations();
-
- assertNotNull(receivedInvites);
- assertTrue(receivedInvites.isEmpty() == false);
- Invitation invite = null;
-
- for (Invitation i : receivedInvites)
- if (i.getId().equals(AccountConfig.testInviteId))
- invite = i;
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
- }
-
- /**
- * Test accepting an invitation.
- * @throws IOException
- */
- public void testAcceptInvitation() throws IOException {
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- assertNotNull(client2);
- assertNotNull(client2.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(AccountConfig.testInviteId);
-
- testSendInvite();
- List<Invitation> receivedInvites = client2.getSwarmInviteClient().getRecievedInvitations(AccountConfig.testUserResource2.getResourceId());
-
- assertNotNull(receivedInvites);
- assertTrue(receivedInvites.isEmpty() == false);
- Invitation invite = null;
-
- for (Invitation i : receivedInvites)
- if (i.getId().equals(AccountConfig.testInviteId))
- invite = i;
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
-
- Invitation acceptInvite = client2.getSwarmInviteClient().respond(invite.getResourceId(), invite.getId(), InvitationResponse.ACCEPT);
-
- assertNotNull(acceptInvite);
- assertTrue(acceptInvite.getStatus().equals(InvitationState.ACCEPTED));
- assertNotNull(acceptInvite.getAcceptedAt());
- }
-
- /**
- * Test rejecting an invitation.
- *
- * @throws IOException
- */
- public void testRejectInvitation() throws IOException {
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- assertNotNull(client2);
- assertNotNull(client2.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(AccountConfig.testInviteId);
-
- testSendInvite();
- List<Invitation> receivedInvites = client2.getSwarmInviteClient().getRecievedInvitations(AccountConfig.testUserResource2.getResourceId());
-
- assertNotNull(receivedInvites);
- assertTrue(receivedInvites.isEmpty() == false);
- Invitation invite = null;
-
- for (Invitation i : receivedInvites)
- if (i.getId().equals(AccountConfig.testInviteId))
- invite = i;
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
-
- Invitation acceptInvite = client2.getSwarmInviteClient().respond(invite.getResourceId(), invite.getId(), InvitationResponse.REJECT);
-
- assertNotNull(acceptInvite);
- assertTrue(acceptInvite.getStatus().equals(InvitationState.REJECTED));
- assertNull(acceptInvite.getAcceptedAt());
- }
-}
View
333 ....client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmResourceWSAPITests.java
@@ -1,333 +0,0 @@
-package com.buglabs.bug.swarm.client.test.configuration;
-
-import java.io.IOException;
-import java.util.List;
-import java.util.Random;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationResponse;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationState;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.model.Invitation;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.SwarmResourceModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-
-/**
- * Tests for the Swarm Member WS API.
- *
- * @author kgilmer
- *
- */
-public class SwarmResourceWSAPITests extends TestCase {
-
- private static final MemberType DEFAULT_MEMBER_TYPE = MemberType.CONSUMER;
-
- @Override
- protected void setUp() throws Exception {
- assertNotNull(AccountConfig.getConfiguration());
- assertNotNull(AccountConfig.getConfiguration2());
-
- assertFalse(AccountConfig.getConfiguration().getConfingurationAPIKey().equals(AccountConfig.getConfiguration2().getConfingurationAPIKey()));
- assertFalse(AccountConfig.getConfiguration().getParticipationAPIKey().equals(AccountConfig.getConfiguration2().getParticipationAPIKey()));
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- List<SwarmModel> swarms = client.list();
-
- for (SwarmModel sm : swarms) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
- String id = client.create(AccountConfig.generateRandomSwarmName(), true, AccountConfig.getTestSwarmDescription());
- AccountConfig.testSwarmId = id;
-
- //Determine that 2nd user can connect and delete any existing swarms.
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- for (SwarmModel sm : client2.list()) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration2().getUsername())) {
- client2.destroy(sm.getId());
- }
- }
-
- for (UserResourceModel ur : client2.getUserResourceClient().list())
- client2.getUserResourceClient().destroy(ur.getResourceId());
-
- UserResourceModel urc = client2.getUserResourceClient().add("3rd_resource", "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource2 = urc;
-
- //Confirm that original user still has swarm.
- assertTrue(client.list().size() == 1);
- }
-
- @Override
- protected void tearDown() throws Exception {
- if (AccountConfig.testSwarmId != null) {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- client.destroy(AccountConfig.testSwarmId);
- }
- }
- /**
- * This test must occur after testSwarmId is set by a previous test.
- * @throws IOException
- */
- public void testAddSwarmMember() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(AccountConfig.testUserResource2);
-
- //Create a new swarm
- Random r = new Random();
- String swarmName = "swarm-" + r.nextFloat();
- String newSwarm = client.create(swarmName, true, "desc " + swarmName);
- assertNotNull(newSwarm);
- assertTrue(newSwarm.length() > 0);
- AccountConfig.testSwarmId = newSwarm;
- //Verify we can get the new swarm from the server
- SwarmModel swarmModel = client.get(newSwarm);
- assertNotNull(swarmModel);
- assertTrue(swarmModel.getId().equals(newSwarm));
-
- //Create a new resource
- String rn = "resource-" + r.nextFloat();
- UserResourceModel urm = client.getUserResourceClient().add(
- rn, "desc " + rn, "pc", r.nextFloat(), r.nextFloat());
- assertNotNull(urm);
- AccountConfig.testUserResource1 = null;
-
- //Confirm that the resource is listed
- for (UserResourceModel res : client.getUserResourceClient().list()) {
- if (res.getName().equals(rn))
- AccountConfig.testUserResource1 = res;
- }
- assertNotNull(AccountConfig.testUserResource1);
-
- //Associate the resource to the swarm as a consumer.
- SwarmWSResponse response = client.getSwarmResourceClient().add(
- newSwarm, MemberType.CONSUMER, AccountConfig.testUserResource1.getResourceId());
-
- // I am receiving 404 Not Found Cannot POST /swarms/315d61ae... here, not sure why.
- assertFalse(response.isError());
-
- //Send invitation
- String inviteTarget = AccountConfig.getConfiguration2().getUsername();
- Invitation invite = client.getSwarmInviteClient().send(
- AccountConfig.testSwarmId, inviteTarget,
- AccountConfig.testUserResource2.getResourceId(),
- DEFAULT_MEMBER_TYPE,
- "test");
- assertNotNull(invite);
- assertTrue(invite.getStatus() == InvitationState.NEW);
-
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
-
- assertTrue(client2.getSwarmInviteClient().getRecievedInvitations(AccountConfig.testUserResource2.getResourceId()).size() > 0);
- Invitation inviteResponse = client2.getSwarmInviteClient().respond(invite.getResourceId(), invite.getId(), InvitationResponse.ACCEPT);
- assertNotNull(inviteResponse);
- assertTrue(inviteResponse.getStatus() == InvitationState.ACCEPTED);
-
- // At this point the client2 resource should be a member of our test swarm. Confirm...
- List<SwarmResourceModel> resources = client.getSwarmResourceClient().list(AccountConfig.testSwarmId, DEFAULT_MEMBER_TYPE);
- assertNotNull(resources);
- assertTrue(resources.size() > 0);
- boolean user2listed = false;
- for (SwarmResourceModel m : resources)
- if (m.getUserId().equals(AccountConfig.getConfiguration2().getUsername()))
- user2listed = true;
-
- assertTrue(user2listed);
- /*
-
- SwarmWSResponse rc = client.getSwarmResourceClient().add(
- AccountConfig.testSwarmId,
- DEFAULT_MEMBER_TYPE,
- AccountConfig.testUserResource2.getResourceId());
-
- if (rc.isError()) {
- System.err.println(rc.getMessage());
- }
- assertTrue(!rc.isError());*/
-
- //TODO: This test is failing, and am not sure why.
- }
-
- public void testListConsumerMembers() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- ISwarmResourcesClient membersClient = client.getSwarmResourceClient();
-
- testAddSwarmMember();
-
- List<SwarmResourceModel> list = membersClient.list(AccountConfig.testSwarmId, MemberType.CONSUMER);
-
- assertNotNull(list);
- assertTrue(list.size() > 0);
-
- //TODO validate that a we are a member of the swarm since we created it.
- }
-
- public void testListProducerMembers() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- ISwarmResourcesClient membersClient = client.getSwarmResourceClient();
-
- testAddSwarmMember();
-
- List<SwarmResourceModel> list = membersClient.list(AccountConfig.testSwarmId, MemberType.PRODUCER);
-
- assertNotNull(list);
- //There should be no PRODUCERS since we created only a CONSUMER
- assertTrue(list.size() == 0);
-
- //Create WS client for 2nd user.
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- ISwarmResourcesClient membersClient2 = client2.getSwarmResourceClient();
- UserResourceModel client2resource = null;
- if (client2.getUserResourceClient().list().size() == 0) {
- client2resource = client2.getUserResourceClient().add("test_resource", "test resource desc", "pc", 0, 0);
- } else {
- client2resource = client2.getUserResourceClient().list().get(0);
- }
- assertNotNull(client2resource);
-
- //Now add a producer by inviting
- Invitation invite = client.getSwarmInviteClient().send(
- AccountConfig.testSwarmId,
- AccountConfig.getConfiguration2().getUsername(),
- client2resource.getResourceId(),
- MemberType.PRODUCER,
- "test desc");
-
- assertNotNull(invite);
- assertTrue(invite.getStatus() == InvitationState.NEW);
-
- //And accept the invitation as user2
- List<Invitation> invitiations = client2.getSwarmInviteClient().getRecievedInvitations(client2resource.getResourceId());
- for (Invitation inv : invitiations)
- if (inv.getStatus() == InvitationState.NEW)
- client2.getSwarmInviteClient().respond(
- client2resource.getResourceId(), inv.getId(), InvitationResponse.ACCEPT);
-
- //We have accepted the invitation. The original swarm should now list us as a producer of the swarm.
- list = membersClient.list(AccountConfig.testSwarmId, MemberType.PRODUCER);
- assertNotNull(list);
- //There should be one PRODUCER now
- assertTrue(list.size() == 1);
-
- //TODO validate that a we are a member of the swarm since we created it.
- }
-
- /**
- * @throws IOException
- */
- public void testListSwarmsForMembers() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- ISwarmResourcesClient membersClient = client.getSwarmResourceClient();
-
- testAddSwarmMember();
-
- List<UserResourceModel> resources = client.getUserResourceClient().get();
-
- for (UserResourceModel resource : resources) {
- List<SwarmModel> members = membersClient.getSwarmsByMember(resource.getResourceId());
-
- assertNotNull(members);
- //assertTrue(members.size() > 0);
- }
-
- //Create client for second user.
- client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- membersClient = client.getSwarmResourceClient();
-
- testAddSwarmMember();
-
- resources = client.getUserResourceClient().get();
-
- for (UserResourceModel resource : resources) {
- List<SwarmModel> members = membersClient.getSwarmsByMember(resource.getResourceId());
-
- assertNotNull(members);
- //assertTrue(members.size() > 0);
- }
- }
-
- /**
- * @throws IOException
- *
- */
- public void testRemoveSwarmMember() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- ISwarmResourcesClient membersClient = client.getSwarmResourceClient();
-
- testAddSwarmMember();
-
- //Get the current number of members.
- int count = membersClient.getSwarmsByMember(
- AccountConfig.testUserResource1.getResourceId()).size();
- assertTrue(count > 0);
- //TODO: determine set of test users that can be created or assumed to exist.
- SwarmWSResponse rc = membersClient.remove(
- AccountConfig.testSwarmId,
- DEFAULT_MEMBER_TYPE,
- AccountConfig.getConfiguration().getUsername(),
- AccountConfig.testUserResource1.getResourceId());
- assertFalse(rc.isError());
-
- //Make sure member count has decreased by one.
- assert(membersClient.getSwarmsByMember(
- AccountConfig.testUserResource1.getResourceId()).size() == count - 1);
-
- client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- membersClient = client.getSwarmResourceClient();
-
- boolean error = false;
- try {
- rc = membersClient.remove(
- AccountConfig.testSwarmId,
- DEFAULT_MEMBER_TYPE,
- AccountConfig.getConfiguration2().getUsername(),
- AccountConfig.testUserResource1.getResourceId());
- } catch (IOException e) {
- error = true;
- }
-
- assertTrue(error);
- }
-}
View
223 ...ug.swarm.client/test/com/buglabs/bug/swarm/client/test/configuration/SwarmWSAPITests.java
@@ -1,223 +0,0 @@
-package com.buglabs.bug.swarm.client.test.configuration;
-
-import java.io.IOException;
-import java.util.Arrays;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-/**
- * Unit tests for ISwarmWSClient implementation.
- *
- * @author kgilmer
- *
- */
-public class SwarmWSAPITests extends TestCase {
-
- public void testDeleteAllSwarms() throws IOException {
- assertNotNull(AccountConfig.getConfiguration());
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- for (SwarmModel sm : client.list())
- client.destroy(sm.getId());
-
- assertTrue(client.list().size() == 0);
- }
-
- /**
- * @throws IOException on error
- */
- public void testCreateSwarm() throws IOException {
- assertNotNull(AccountConfig.getConfiguration());
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(client.getSwarmResourceClient());
-
- //Delete all pre-existing resources owned by the test user.
- for (UserResourceModel ur : client.getUserResourceClient().list())
- client.getUserResourceClient().destroy(ur.getResourceId());
-
- assertTrue(client.getUserResourceClient().list().size() == 0);
-
- //Delete all pre-existing swarms owned by test user.
- for (SwarmModel sm : client.list()) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
- String id = client.create(
- AccountConfig.generateRandomSwarmName(),
- true,
- AccountConfig.getTestSwarmDescription());
-
- System.out.println("Created swarm with id: " + id);
- assertNotNull(id);
- assertTrue(id.length() > 0);
-
- AccountConfig.testSwarmId = id;
-
- //Test that id now is in the list of swarms for client.
- boolean swarmIdFound = false;
- for (SwarmModel sm : client.list()) {
- if (sm.getId().equals(id)) {
- swarmIdFound = true;
- }
- }
- assertTrue(swarmIdFound);
-
- //Create user resource.
- /*
- * "name": "My resource",
- "machine_type": "pc",
- "description": "My Resource description",
- "position": {
- "longitude": 0,
- "latitude": 0
- }
- */
- UserResourceModel userResource = client.getUserResourceClient().add(
- AccountConfig.generateRandomResourceName(),
- "Test resource",
- "pc", 0, 0);
-
- assertNotNull(userResource);
- AccountConfig.getConfiguration().setResourceId(userResource.getResourceId());
- assertNotNull(AccountConfig.getConfiguration().getResource());
-
- //Make sure user resource is now present in list.
- boolean userResourceFound = false;
- for (UserResourceModel ur : client.getUserResourceClient().list())
- if (ur.getResourceId().equals(userResource.getResourceId()))
- userResourceFound = true;
- assertTrue(userResourceFound);
- }
-
- /**
- * @throws IOException on error
- */
- public void testUpdateSwarm() throws IOException {
- testCreateSwarm();
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- SwarmWSResponse rval = client.update(AccountConfig.testSwarmId, true, "new description");
-
- assertTrue(!rval.isError());
- }
-
- /**
- * @throws IOException on error
- */
- public void testGet() throws IOException {
- testCreateSwarm();
-
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(client);
- assertNotNull(AccountConfig.testSwarmId);
-
- SwarmModel swarmInfo = client.get(AccountConfig.testSwarmId);
-
- assertTrue(swarmInfo != null);
- }
-
- /**
- * @throws IOException on error
- */
- public void testList() throws IOException {
- testCreateSwarm();
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(client);
-
- List<SwarmModel> swarms = client.list();
-
- assertTrue(swarms != null);
- assertTrue(swarms.size() > 0);
-
- boolean swarmIdExists = false;
- for (SwarmModel sm : swarms) {
- System.out.println("testSwarmId: " + AccountConfig.testSwarmId + " SwarmModelId: " + sm.getId());
- if (sm.getId().equals(AccountConfig.testSwarmId)) {
- swarmIdExists = true;
- break;
- }
- }
-
- assertTrue(swarmIdExists);
- }
-
- /**
- * @throws IOException on error
- */
- public void testDestroy() throws IOException {
- testCreateSwarm();
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- assertNotNull(client);
- assertNotNull(AccountConfig.testSwarmId);
- SwarmWSResponse rval = client.destroy(AccountConfig.testSwarmId);
-
- assertTrue(!rval.isError());
- }
-
- /**
- * Test the SwarmWSResponse class.
- */
- public void testSwarmWSResponse() {
- //Test that valid HTTP responses can be created.
- SwarmWSResponse rs = SwarmWSResponse.R200;
-
- assertNotNull(rs);
- assertEquals(rs.getCode(), 200);
-
- Integer[] validCodes = new Integer[] {200, 201, 400, 401, 403, 404, 409, 500};
-
- for (int i : Arrays.asList(validCodes)) {
- rs = SwarmWSResponse.fromCode(i);
- assertTrue(rs.getCode() == i);
-
- //Test that only 200, 201 are non-error codes.
- if (rs.getCode() == 200 || rs.getCode() == 201) {
- assertFalse(rs.isError());
- } else {
- assertTrue(rs.isError());
- }
- }
-
- //Test that invalid http code throws runtime exception.
- boolean caught = false;
- try {
- rs = SwarmWSResponse.fromCode(2);
- } catch (RuntimeException e) {
- caught = true;
- }
-
- assertTrue(caught);
- }
-}
View
126 ...m.client/test/com/buglabs/bug/swarm/client/test/configuration/UserResourceWSAPITests.java
@@ -1,126 +0,0 @@
-package com.buglabs.bug.swarm.client.test.configuration;
-
-import java.io.IOException;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
-import com.buglabs.bug.swarm.client.IUserResourceClient;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-
-/**
- * Tests for the Resource WS API.
- *
- * @author kgilmer
- *
- */
-public class UserResourceWSAPITests extends TestCase {
-
- private static final String TEST_RESOURCE_NAME = "test_resource_name";
- private static final String TEST_RESOURCE_DESC = "test resource desc";
- private static final String TEST_RESOURCE_MACHINE_TYPE = "pc";
- private static final MemberType TEST_RESOURCE_TYPE = MemberType.PRODUCER;
-
- @Override
- protected void setUp() throws Exception {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- //Delete all pre-existing swarms owned by test user.
- List<SwarmModel> swarms = client.list();
-
- for (SwarmModel sm : swarms) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
-
- //Delete all pre-existing resources owned by the test user.
- IUserResourceClient resourceClient = client.getUserResourceClient();
- assertNotNull(resourceClient);
-
- List<UserResourceModel> resources = resourceClient.get();
-
- for (UserResourceModel model : resources)
- if (model.getUserId().equals(AccountConfig.getConfiguration().getUsername()))
- resourceClient.destroy(model.getResourceId());
-
- String id = client.create(AccountConfig.generateRandomSwarmName(), true, AccountConfig.getTestSwarmDescription());
- AccountConfig.testSwarmId = id;
- }
-
- @Override
- protected void tearDown() throws Exception {
- if (AccountConfig.testSwarmId != null) {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- client.destroy(AccountConfig.testSwarmId);
- }
- }
- /**
- * This test must occur after testSwarmId is set by a previous test.
- * @throws IOException
- */
- public void testAddResource() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- IUserResourceClient rclient = client.getUserResourceClient();
-
- UserResourceModel urm = rclient.add(
- TEST_RESOURCE_NAME,
- TEST_RESOURCE_DESC,
- TEST_RESOURCE_MACHINE_TYPE,
- 0,
- 0);
-
- assertNotNull(urm);
-
- List<UserResourceModel> resources = rclient.get();
-
- assertNotNull(resources);
- assertTrue(resources.size() == 1);
-
- UserResourceModel userrm = resources.get(0);
-
- assertTrue(userrm.getDescription().equals(TEST_RESOURCE_DESC));
- assertTrue(userrm.getMachineType().equals(TEST_RESOURCE_MACHINE_TYPE));
- assertTrue(userrm.getUserId().equals(AccountConfig.getConfiguration().getUsername()));
- }
-
- /**
- * @throws IOException
- */
- public void testDeleteResources() throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- IUserResourceClient rclient = client.getUserResourceClient();
-
- testAddResource();
-
- List<UserResourceModel> resources = rclient.get();
-
- assertNotNull(resources);
- assertTrue(resources.size() == 1);
-
- SwarmWSResponse response = rclient.destroy(resources.get(0).getResourceId());
- assertFalse(response.isError());
-
- resources = rclient.get();
- assertNotNull(resources);
- assertTrue(resources.size() == 0);
- }
-
-}
View
104 ...labs.bug.swarm.client/test/com/buglabs/bug/swarm/client/test/participation/FeedTests.java
@@ -1,104 +0,0 @@
-package com.buglabs.bug.swarm.client.test.participation;
-
-import java.io.IOException;
-import java.net.UnknownHostException;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Random;
-
-import com.buglabs.bug.swarm.client.ISwarmSession;
-import com.buglabs.bug.swarm.client.ISwarmStringMessageListener;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.model.Configuration;
-import com.buglabs.bug.swarm.client.model.Configuration.Protocol;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-import com.buglabs.bug.swarm.client.test.TwoParticipantsOneSwarmTestCase;
-
-/**
- * Various tests for swarm feeds using Participation client.
- * @author kgilmer
- *
- */
-public class FeedTests extends TwoParticipantsOneSwarmTestCase {
-
- private static final Random rnd = new Random();
- /**
- * Basic test for producer client sending a feed that a consumer client receives.
- *
- * @throws UnknownHostException
- * @throws IOException
- * @throws InterruptedException
- */
- public void testFeedSendReceive() throws UnknownHostException, IOException, InterruptedException {
- Configuration ac1 = AccountConfig.getConfiguration();
- Configuration ac2 = AccountConfig.getConfiguration2();
-
- ISwarmSession produceClient = SwarmClientFactory.createConsumptionSession(
- ac1.getHostname(Protocol.HTTP), ac1.getParticipationAPIKey(), AccountConfig.testUserResource.getResourceId(), AccountConfig.testSwarmId);
-
- ISwarmSession consumeClient = SwarmClientFactory.createConsumptionSession(
- ac2.getHostname(Protocol.HTTP), ac2.getParticipationAPIKey(), AccountConfig.testUserResource2.getResourceId(), AccountConfig.testSwarmId);
-
- assertNotNull(produceClient);
- assertNotNull(consumeClient);
-
- TestListener consumerListener = new TestListener();
- consumeClient.addListener(consumerListener);
-
- TestListener producerListener = new TestListener();
- produceClient.addListener(producerListener);
-
- produceClient.send(generateRandomPayload());
-
- Thread.sleep(1000);
-
- assertTrue(consumerListener.getMessage());
-
- produceClient.close();
- consumeClient.close();
- }
-
- private Map<String, ?> generateRandomPayload() {
- Map<String, Object> m = new HashMap<String, Object>();
-
- int l = rnd.nextInt(5) + 5;
-
- for (int i = 0; i < l; ++i)
- m.put("key-" + i, rnd.nextDouble());
-
- return m;
- }
-
- private class TestListener implements ISwarmStringMessageListener {
- private boolean presence = false;
- private boolean exception = false;
- private boolean message = false;
-
- @Override
- public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) {
- presence = true;
- }
-
- @Override
- public void exceptionOccurred(ExceptionType type, String message) {
- exception = true;
- }
-
- public boolean getPresence() {
- return presence;
- }
-
- public boolean getException() {
- return exception;
- }
-
- public boolean getMessage() {
- return message;
- }
-
- @Override
- public void messageRecieved(String payload, String fromSwarm, String fromResource, boolean isPublic) {
- message = true;
- }
- }
-}
View
329 ...rm.client/test/com/buglabs/bug/swarm/client/test/participation/ParticipationAPITests.java
@@ -1,329 +0,0 @@
-package com.buglabs.bug.swarm.client.test.participation;
-
-
-import java.io.IOException;
-import java.util.List;
-import java.util.Map;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationResponse;
-import com.buglabs.bug.swarm.client.ISwarmInviteClient.InvitationState;
-import com.buglabs.bug.swarm.client.ISwarmJsonMessageListener;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
-import com.buglabs.bug.swarm.client.ISwarmSession;
-import com.buglabs.bug.swarm.client.ISwarmStringMessageListener;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.model.Configuration;
-import com.buglabs.bug.swarm.client.model.Invitation;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-/**
- * Unit tests for ISwarmParticipationClient implementation.
- *
- * @author kgilmer
- *
- */
-public class ParticipationAPITests extends TestCase {
-
- private static final String description = "invite test description";
-
- volatile boolean psession1MessageRecieved = false;
- volatile boolean psession2MessageRecieved = false;
- volatile boolean psession1PresenceMessageRecieved = false;
- volatile boolean psession2PresenceMessageRecieved = false;
- volatile boolean psession1ExceptionRecieved = false;
- volatile boolean psession2ExceptionRecieved = false;
-
- /* (non-Javadoc)
- * @see junit.framework.TestCase#setUp()
- *
- * For user 1:
- * - Delete all existing swarms
- * - Delete all existing resources
- * - Create swarm, store id
- * - Create resource, store id
- *
- * For user 2:
- * - Delete all existing swarms
- * - Delete all existing resources
- * - Create new resource, store id
- *
- */
- @Override
- protected void setUp() throws Exception {
- assertNotNull(AccountConfig.getConfiguration());
- assertNotNull(AccountConfig.getConfiguration2());
-
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- List<SwarmModel> swarms = client.list();
-
- for (SwarmModel sm : swarms) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
- String id = client.create(AccountConfig.generateRandomSwarmName(), true, AccountConfig.getTestSwarmDescription());
- AccountConfig.testSwarmId = id;
-
- UserResourceModel urc = client.getUserResourceClient().add(AccountConfig.generateRandomResourceName(), "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource = urc;
-
- SwarmWSResponse response = client.getSwarmResourceClient().add(AccountConfig.testSwarmId, MemberType.PRODUCER, urc.getResourceId());
-
- assertTrue(!response.isError());
-
- //Determine that 2nd user can connect and delete any existing swarms.
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- for (SwarmModel sm : client2.list()) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration2().getUsername())) {
- client2.destroy(sm.getId());
- }
- }
-
- for (UserResourceModel ur : client2.getUserResourceClient().list())
- client2.getUserResourceClient().destroy(ur.getResourceId());
-
- urc = client2.getUserResourceClient().add(AccountConfig.generateRandomResourceName(), "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource2 = urc;
-
- //Confirm that original user still has swarm.
- assertTrue(client.list().size() == 1);
- }
-
- /**
- * Test sending an invite. Only confirms that server returns invite object as specified in documentation.
- *
- * @throws IOException
- */
- private void sendInvite(MemberType type) throws IOException {
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
- assertNotNull(client);
- assertNotNull(client.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
-
- Invitation invite = client.getSwarmInviteClient().send(
- AccountConfig.testSwarmId,
- AccountConfig.getConfiguration2().getUsername(),
- AccountConfig.testUserResource2.getResourceId(),
- type,
- description);
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
-
- AccountConfig.testInviteId = invite.getId();
- }
-
- /**
- * Test accepting an invitation.
- * @throws IOException
- * @throws InterruptedException
- */
- public void testSwarmParticipationAPI() throws IOException, InterruptedException {
- sendInvite(MemberType.PRODUCER);
- acceptInvitation();
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- UserResourceModel urc = client.getUserResourceClient().add("stream_resource", "stream resource desc", "pc", 0, 0);
-
- System.out.println("User 1 Key: " + AccountConfig.getConfiguration().getParticipationAPIKey().substring(0, 4));
- ISwarmSession psession1 = SwarmClientFactory.createProductionSession(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getParticipationAPIKey(),
- urc.getResourceId(),
- AccountConfig.testSwarmId);
-
- psession1MessageRecieved = false;
- psession2MessageRecieved = false;
- psession1PresenceMessageRecieved = false;
- psession2PresenceMessageRecieved = false;
- psession1ExceptionRecieved = false;
- psession2ExceptionRecieved = false;
-
- psession1.addListener(new ISwarmJsonMessageListener() {
-
- @Override
- public void messageRecieved(Map<String, ?> payload, String fromSwarm, String fromResource, boolean isPublic) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.print(fromResource);
- System.out.print(" ");
- System.out.println(payload);
- psession1MessageRecieved = true;
- }
-
- @Override
- public void exceptionOccurred(ExceptionType type, String message) {
- System.err.print(type.toString());
- System.err.print(" ");
- System.err.print(message);
- psession1ExceptionRecieved = true;
- }
-
- @Override
- public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.println(fromResource);
- psession1PresenceMessageRecieved = true;
- }
- });
-
- psession1.addListener(new ISwarmStringMessageListener() {
-
- @Override
- public void messageRecieved(String payload, String fromSwarm, String fromResource, boolean isPublic) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.print(fromResource);
- System.out.print(" ");
- System.out.println(payload);
- psession1MessageRecieved = true;
- }
-
- @Override
- public void exceptionOccurred(ExceptionType type, String message) {
- System.err.print(type.toString());
- System.err.print(" ");
- System.err.print(message);
- psession1ExceptionRecieved = true;
- }
-
- @Override
- public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.println(fromResource);
- psession1PresenceMessageRecieved = true;
- }
- });
-
- psession1.join(AccountConfig.testSwarmId, urc.getResourceId());
-
- //Setup the second user.
- assertNotNull(AccountConfig.testUserResource2.getResourceId());
- System.out.println("User 2 Key: " + AccountConfig.getConfiguration2().getParticipationAPIKey().substring(0, 4));
-
- ISwarmSession psession2 = SwarmClientFactory.createProductionSession(
- AccountConfig.getConfiguration2().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration2().getParticipationAPIKey(),
- AccountConfig.testUserResource2.getResourceId(), AccountConfig.testSwarmId);
-
- psession2.addListener(new ISwarmJsonMessageListener() {
-
- @Override
- public void messageRecieved(Map<String, ?> payload, String fromSwarm, String fromResource, boolean isPublic) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.print(fromResource);
- System.out.print(" ");
- System.out.println(payload);
- psession2MessageRecieved = true;
- }
-
- @Override
- public void exceptionOccurred(ExceptionType type, String message) {
- System.err.print(type.toString());
- System.err.print(" ");
- System.err.print(message);
-
- psession2ExceptionRecieved = true;
- }
-
- @Override
- public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) {
- System.out.print(fromSwarm);
- System.out.print(" ");
- System.out.println(fromResource);
- psession2PresenceMessageRecieved = true;
- }
- });
-
- psession2.join(AccountConfig.testSwarmId, AccountConfig.testUserResource2.getResourceId());
- psession2.send(AccountConfig.generateRandomPayload());
-
- Thread.sleep(10000);
-
- assertTrue(psession1PresenceMessageRecieved);
- //assertFalse(psession1MessageRecieved);
- assertFalse(psession1ExceptionRecieved);
-
- assertTrue(psession2PresenceMessageRecieved);
- //assertFalse(psession2MessageRecieved);
- assertFalse(psession2ExceptionRecieved);
-
- psession1.close();
- psession2.close();
-
- Thread.sleep(2000);
- assertFalse(psession1ExceptionRecieved);
- assertFalse(psession2ExceptionRecieved);
- }
-
- /**
- * Test accepting an invitation.
- * @throws IOException
- */
- private void acceptInvitation() throws IOException {
- ISwarmClient client2 = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration2().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration2().getConfingurationAPIKey());
- assertNotNull(client2);
- assertNotNull(client2.getSwarmInviteClient());
- assertNotNull(AccountConfig.testSwarmId);
- assertNotNull(AccountConfig.testInviteId);
-
- sendInvite(MemberType.PRODUCER);
- List<Invitation> receivedInvites = client2.getSwarmInviteClient().getRecievedInvitations(AccountConfig.testUserResource2.getResourceId());
-
- assertNotNull(receivedInvites);
- assertTrue(receivedInvites.isEmpty() == false);
- Invitation invite = null;
-
- for (Invitation i : receivedInvites)
- if (i.getId().equals(AccountConfig.testInviteId))
- invite = i;
-
- assertNotNull(invite);
- assertNotNull(invite.getId());
- assertNotNull(invite.getFromUser());
- assertNotNull(invite.getToUser());
- assertNotNull(invite.getResourceId());
- assertNotNull(invite.getStatus());
- assertTrue(invite.getStatus().equals(InvitationState.NEW));
- assertNotNull(invite.getDescription());
- assertTrue(invite.getDescription().equals(description));
-
- Invitation acceptInvite = client2.getSwarmInviteClient().respond(invite.getResourceId(), invite.getId(), InvitationResponse.ACCEPT);
-
- assertNotNull(acceptInvite);
- assertTrue(acceptInvite.getStatus().equals(InvitationState.ACCEPTED));
- assertNotNull(acceptInvite.getAcceptedAt());
- }
-}
View
172 ...m.client/test/com/buglabs/bug/swarm/client/test/participation/SessionManagementTests.java
@@ -1,172 +0,0 @@
-package com.buglabs.bug.swarm.client.test.participation;
-
-import java.io.IOException;
-import java.net.UnknownHostException;
-import java.util.List;
-
-import junit.framework.TestCase;
-
-import com.buglabs.bug.swarm.client.ISwarmClient;
-import com.buglabs.bug.swarm.client.ISwarmResourcesClient.MemberType;
-import com.buglabs.bug.swarm.client.ISwarmSession;
-import com.buglabs.bug.swarm.client.ISwarmStringMessageListener;
-import com.buglabs.bug.swarm.client.SwarmClientFactory;
-import com.buglabs.bug.swarm.client.SwarmWSResponse;
-import com.buglabs.bug.swarm.client.model.Configuration;
-import com.buglabs.bug.swarm.client.model.SwarmModel;
-import com.buglabs.bug.swarm.client.model.UserResourceModel;
-import com.buglabs.bug.swarm.client.test.AccountConfig;
-
-/**
- * Tests for session management between swarm.client and the server.
- *
- */
-public class SessionManagementTests extends TestCase {
-
- @Override
- protected void setUp() throws Exception {
- /*
- * Create a swarm, a resource, and associate them.
- */
- assertNotNull(AccountConfig.getConfiguration());
- assertNotNull(AccountConfig.getConfiguration2());
-
-
- ISwarmClient client = SwarmClientFactory.getSwarmClient(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getConfingurationAPIKey());
-
- //Delete all pre-existing swarms owned by test user.
- List<SwarmModel> swarms = client.list();
-
- for (SwarmModel sm : swarms) {
- if (sm.getUserId().equals(AccountConfig.getConfiguration().getUsername())) {
- client.destroy(sm.getId());
- }
- }
-
- String id = client.create(AccountConfig.generateRandomSwarmName(), true, AccountConfig.getTestSwarmDescription());
- AccountConfig.testSwarmId = id;
-
- UserResourceModel urc = client.getUserResourceClient().add(AccountConfig.generateRandomResourceName(), "user resource desc", "pc", 0, 0);
- AccountConfig.testUserResource = urc;
-
- SwarmWSResponse response = client.getSwarmResourceClient().add(AccountConfig.testSwarmId, MemberType.PRODUCER, urc.getResourceId());
-
- assertTrue(!response.isError());
- }
-
- /**
- * Open a session, wait 30 seconds, confirm that it is still active. Then close the session.
- * @throws UnknownHostException
- * @throws IOException
- * @throws InterruptedException
- */
- public void test30SecondSession() throws UnknownHostException, IOException, InterruptedException {
- assertNotNull(AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP));
- assertNotNull(AccountConfig.getConfiguration().getParticipationAPIKey());
- assertNotNull(AccountConfig.testUserResource.getResourceId());
- assertNotNull(AccountConfig.testSwarmId);
-
- ISwarmSession session = SwarmClientFactory.createProductionSession(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getParticipationAPIKey(),
- AccountConfig.testUserResource.getResourceId(),
- AccountConfig.testSwarmId);
-
- assertNotNull(session);
-
- assertTrue(session.isConnected());
- SessionTestListener tl = new SessionTestListener();
- session.addListener(tl);
- session.join(AccountConfig.testSwarmId, AccountConfig.testUserResource.getResourceId());
-
- Thread.sleep(30000);
-
- assertTrue(session.isConnected());
- assertTrue(tl.isPresenceReceived());
-
- session.close();
-
- assertFalse(session.isConnected());
- }
-
- /**
- * Open a session, wait 3 minutes, confirm that it is still active. Then close the session.
- * @throws UnknownHostException
- * @throws IOException
- * @throws InterruptedException
- *
- * //Disabling for now to speed up build.
- */
- public void test3MinuteSession() throws UnknownHostException, IOException, InterruptedException {
- assertNotNull(AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP));
- assertNotNull(AccountConfig.getConfiguration().getParticipationAPIKey());
- assertNotNull(AccountConfig.testUserResource.getResourceId());
- assertNotNull(AccountConfig.testSwarmId);
-
- ISwarmSession session = SwarmClientFactory.createProductionSession(
- AccountConfig.getConfiguration().getHostname(Configuration.Protocol.HTTP),
- AccountConfig.getConfiguration().getParticipationAPIKey(),
- AccountConfig.testUserResource.getResourceId(),
- AccountConfig.testSwarmId);
-
- assertNotNull(session);
-
- assertTrue(session.isConnected());
- SessionTestListener tl = new SessionTestListener();
- session.addListener(tl);
- session.join(AccountConfig.testSwarmId, AccountConfig.testUserResource.getResourceId());
-
- Thread.sleep(60000 * 3);
-
- assertTrue(session.isConnected());
- assertTrue(tl.isPresenceReceived());
-
- session.close();
-
- assertFalse(session.isConnected());
- }
-
- /**
- *
- * A test listener that captures which messages have been sent from session.
- *
- */
- class SessionTestListener implements ISwarmStringMessageListener {
-
- private boolean presenceReceived;
- private boolean messageRecieved;
- private boolean exceptionReceived;
-
- public SessionTestListener() {
- messageRecieved = false;
- presenceReceived = false;
- exceptionReceived = false;
- }
- @Override
- public void messageRecieved(String payload, String fromSwarm, String fromResource, boolean isPublic) {
- messageRecieved = true;
- }
-
- @Override
- public void presenceEvent(String fromSwarm, String fromResource, boolean isAvailable) {
- presenceReceived = true;
- }
-
- @Override
- public void exceptionOccurred(ExceptionType type, String message) {
- exceptionReceived = true;
- }
-
- public boolean isPresenceReceived() {
- return presenceReceived;
- }
- public boolean isMessageRecieved() {
- return messageRecieved;
- }
- public boolean isExceptionReceived() {
- return exceptionReceived;
- }
- }
-}

0 comments on commit 5904936

Please sign in to comment.