001 package org.hackystat.sensorbase.resource.projects; 002 003 import static org.junit.Assert.assertEquals; 004 import static org.junit.Assert.assertTrue; 005 006 import java.util.ArrayList; 007 import java.util.List; 008 009 import javax.xml.datatype.XMLGregorianCalendar; 010 011 import org.hackystat.sensorbase.client.SensorBaseClient; 012 import org.hackystat.sensorbase.resource.projects.jaxb.Invitations; 013 import org.hackystat.sensorbase.resource.projects.jaxb.Project; 014 import org.hackystat.sensorbase.resource.projects.jaxb.ProjectIndex; 015 import org.hackystat.sensorbase.resource.projects.jaxb.ProjectRef; 016 import org.hackystat.sensorbase.resource.projects.jaxb.Spectators; 017 import org.hackystat.sensorbase.resource.projects.jaxb.UriPatterns; 018 import org.hackystat.sensorbase.test.SensorBaseRestApiHelper; 019 import org.hackystat.utilities.tstamp.Tstamp; 020 import org.junit.BeforeClass; 021 import org.junit.Test; 022 023 /** 024 * Tests the SensorBase REST API for Project spectators. 025 * A project spectator can be added to the project by the owner. The project spectator can 026 * view the sensordata associated with the project. 027 * 028 * @author Philip M. Johnson 029 */ 030 public class TestProjectSpectatorRestApi extends SensorBaseRestApiHelper { 031 032 private static String testUser1 = "TestProjectSpectatorUser1@hackystat.org"; 033 private static String testUser2 = "TestProjectSpectatorUser2@hackystat.org"; 034 private static String testProject1 = "TestSpectatorProject1"; 035 private static SensorBaseClient client1; 036 private static SensorBaseClient client2; 037 038 /** 039 * Starts the server going for these tests, and makes sure our test user is registered. 040 * 041 * @throws Exception If problems occur setting up the server. 042 */ 043 @BeforeClass 044 public static void setupMembership() throws Exception { 045 SensorBaseClient.registerUser(getHostName(), testUser1); 046 SensorBaseClient.registerUser(getHostName(), testUser2); 047 client1 = new SensorBaseClient(getHostName(), testUser1, testUser1); 048 client2 = new SensorBaseClient(getHostName(), testUser2, testUser2); 049 client1.deleteProject(testUser1, testProject1); 050 } 051 052 053 /** 054 * Tests the normal project spectator use case. 055 * <ul> 056 * <li> testUser1 creates a new project called TestProject1, and puts testUser2 on the 057 * spectator list. 058 * <li> testUser2 can now retrieve the project and any sensor data. 059 * <li> testUser1 then deletes the Project. 060 * </ul> 061 * @throws Exception If problems occur. 062 */ 063 @Test 064 public void testSpectatorship() throws Exception { 065 // First, check that testUser1 has only one defined project (the default). 066 assertEquals("Size is 1", 1, client1.getProjectIndex(testUser1).getProjectRef().size()); 067 // Construct the project representation that is owned by testUser1. 068 Project project = makeProject(testProject1); 069 project.getSpectators().getSpectator().add(testUser2); 070 // PUT it to the server. Note that client1 corresponds to testUser1. 071 client1.putProject(project); 072 // Ensure that testUser2 can retrieve the project. Client2 corresponds to testUser2. 073 project = client2.getProject(testUser1, testProject1); 074 // Ensure that testUser2 can retrieve the data 075 client2.getProjectSensorData(testUser1, testProject1); 076 // Make sure that testUser2 is a spectator. 077 assertTrue("Checking spectator 1", project.getSpectators().getSpectator().contains(testUser2)); 078 079 // Make sure that the project shows up in testUser2's projectindex. 080 ProjectIndex index = client2.getProjectIndex(testUser2); 081 // Build a list of the projectNames in this index. 082 List<String> projectNames = new ArrayList<String>(); 083 for (ProjectRef ref : index.getProjectRef()) { 084 projectNames.add(ref.getName()); 085 } 086 assertTrue("Testing project is in index", projectNames.contains(project.getName())); 087 088 // Now delete the Project. 089 client1.deleteProject(testUser1, testProject1); 090 } 091 092 093 /** 094 * Create a project with the passed name. 095 * The project start time is now, and the end time is one day from now. 096 * @param projectName The name of the Project. 097 * @return The newly created Project representation. 098 */ 099 private Project makeProject(String projectName) { 100 String owner = testUser1; 101 Project project = new Project(); 102 project.setOwner(owner); 103 project.setName(projectName); 104 project.setDescription("Test Project Spectators"); 105 XMLGregorianCalendar tstamp = Tstamp.makeTimestamp(); 106 project.setStartTime(tstamp); 107 project.setEndTime(Tstamp.incrementDays(tstamp, 1)); 108 UriPatterns uris = new UriPatterns(); 109 uris.getUriPattern().add("**/test/**"); 110 project.setUriPatterns(uris); 111 Invitations invitations = new Invitations(); 112 project.setInvitations(invitations); 113 project.setSpectators(new Spectators()); 114 return project; 115 } 116 117 }