001 package org.hackystat.sensorbase.resource.users; 002 003 import static org.junit.Assert.assertEquals; 004 import static org.junit.Assert.assertFalse; 005 import static org.junit.Assert.assertTrue; 006 import static org.junit.Assert.fail; 007 008 import org.hackystat.sensorbase.client.SensorBaseClient; 009 import org.hackystat.sensorbase.client.SensorBaseClientException; 010 import org.hackystat.sensorbase.resource.users.jaxb.Properties; 011 import org.hackystat.sensorbase.resource.users.jaxb.Property; 012 import org.hackystat.sensorbase.resource.users.jaxb.User; 013 import org.hackystat.sensorbase.resource.users.jaxb.UserIndex; 014 import org.hackystat.sensorbase.resource.users.jaxb.UserRef; 015 import org.hackystat.sensorbase.test.SensorBaseRestApiHelper; 016 017 import static org.hackystat.sensorbase.server.ServerProperties.TEST_DOMAIN_KEY; 018 019 import org.junit.Ignore; 020 import org.junit.Test; 021 022 023 /** 024 * Tests the SensorBase REST API for Users and User resources. 025 * @author Philip M. Johnson 026 */ 027 public class TestUsersRestApi extends SensorBaseRestApiHelper { 028 029 /** The TestUser user email. */ 030 private String testUser = "TestUser@hackystat.org"; 031 032 033 /** 034 * Test that GET host/sensorbase/users returns an index containing TestUser and 035 * that the HREFs are OK. 036 * @throws Exception If problems occur. 037 */ 038 @Test public void getUsersIndex() throws Exception { 039 // Create an admin client and check authentication. 040 SensorBaseClient client = new SensorBaseClient(getHostName(), adminEmail, adminPassword); 041 client.authenticate(); 042 // Get the index of all users. 043 UserIndex userIndex = client.getUserIndex(); 044 // Make sure that we can iterate through the Users, find the test user, and dereference hrefs. 045 boolean foundTestUser = false; 046 for (UserRef ref : userIndex.getUserRef()) { 047 if (testUser.equals(ref.getEmail())) { 048 foundTestUser = true; 049 } 050 // Make sure the href is OK. 051 client.getUser(ref); 052 } 053 assertTrue("Checking that we found the TestUser", foundTestUser); 054 } 055 056 /** 057 * Test that the SensorBaseClient.isHost() method operates correctly. 058 * @throws Exception If problems occur. 059 */ 060 @Test public void isHost() throws Exception { 061 assertTrue("Checking isHost() with sensorbase", SensorBaseClient.isHost(getHostName())); 062 assertFalse("Checking isHost() with bogus URL", SensorBaseClient.isHost("http://Foo")); 063 } 064 065 /** 066 * Test that the SensorBaseClient.isRegistered() method operates correctly. 067 * @throws Exception If problems occur. 068 */ 069 @Test public void isRegistered() throws Exception { 070 assertTrue("OK register", SensorBaseClient.isRegistered(getHostName(), testUser, testUser)); 071 assertFalse("Bad register", SensorBaseClient.isRegistered(getHostName(), "foo", "bar")); 072 } 073 074 /** 075 * Test that GET host/sensorbase/users/TestUser@hackystat.org returns the TestUser test user. 076 * @throws Exception If problems occur. 077 */ 078 @Test public void getUser() throws Exception { 079 // Create the TestUser client and check authentication. 080 SensorBaseClient client = new SensorBaseClient(getHostName(), testUser, testUser); 081 client.authenticate(); 082 // Retrieve the TestUser User resource and test a couple of fields. 083 User user = client.getUser(testUser); 084 assertEquals("Bad email", testUser, user.getEmail()); 085 assertEquals("Bad password", testUser, user.getPassword()); 086 } 087 088 /** 089 * Tests the POST method that registers a new user. 090 * @throws Exception If problems occur. 091 */ 092 @Test public void registerUser() throws Exception { 093 // Register the TestPost@hackystat.org user. 094 String testPost = "TestPost@" + server.getServerProperties().get(TEST_DOMAIN_KEY); 095 SensorBaseClient.registerUser(getHostName(), testPost); 096 // Now that TestPost is registered, see if we can retrieve him (her?) 097 SensorBaseClient client = new SensorBaseClient(getHostName(), testPost, testPost); 098 client.authenticate(); 099 User user = client.getUser(testPost); 100 assertEquals("Bad email", testPost, user.getEmail()); 101 // Clean up, get rid of this user. 102 client.deleteUser(testPost); 103 } 104 105 106 /** 107 * Tests multiple register users. 108 * @throws Exception If problems occur. 109 */ 110 @Ignore 111 @Test public void registerUsers() throws Exception { //NOPMD (No JUnit assert) 112 String testPost = "TestRegisterUser@" + server.getServerProperties().get(TEST_DOMAIN_KEY); 113 for (int i = 0; i < 5; i++) { 114 SensorBaseClient.registerUser(getHostName(), testPost); 115 } 116 } 117 118 /** 119 * Tests that a user can be deleted after creation. 120 * @throws Exception If problems occur. 121 */ 122 @Test public void deleteUser () throws Exception { 123 // Register the TestPost@hackystat.org user 124 String testPost = "TestPost@" + server.getServerProperties().get(TEST_DOMAIN_KEY); 125 SensorBaseClient.registerUser(getHostName(), testPost); 126 // Now that TestPost is registered, see if we can delete him (her?) 127 SensorBaseClient client = new SensorBaseClient(getHostName(), testPost, testPost); 128 client.deleteUser(testPost); 129 130 //Ensure that TestPost is no longer found as a user. 131 try { 132 client.authenticate(); 133 fail("Authentication should not have succeeded."); 134 } 135 catch (SensorBaseClientException e) { //NOPMD 136 // good, we got here. 137 // We can't use the JUnit annotation idiom because the code above us could throw 138 // the same exception type, and that would be a valid error. 139 } 140 } 141 142 /** 143 * Tests that a user can have their properties updated. 144 * @throws Exception If problems occur. 145 */ 146 @Test public void postUserProperties () throws Exception { 147 // Register the TestPost@hackystat.org user. 148 String testPost = "TestPost@" + server.getServerProperties().get(TEST_DOMAIN_KEY); 149 SensorBaseClient.registerUser(getHostName(), testPost); 150 // Now that TestPost is registered, see if we can update his properties. 151 Properties properties = new Properties(); 152 Property property = new Property(); 153 property.setKey("testKey"); 154 property.setValue("testValue"); 155 properties.getProperty().add(property); 156 157 SensorBaseClient client = 158 new SensorBaseClient(getHostName(), testPost, testPost); 159 client.updateUserProperties(testPost, properties); 160 161 User user = client.getUser(testPost); 162 Property theProperty = user.getProperties().getProperty().get(0); 163 assertEquals("Got the property", "testValue", theProperty.getValue()); 164 // Clean up, get rid of this user. 165 client.deleteUser(testPost); 166 } 167 }