001    package org.hackystat.dailyprojectdata.resource.cache;
002    
003    import static org.junit.Assert.assertTrue;
004    import static org.junit.Assert.assertEquals;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    
008    import org.hackystat.dailyprojectdata.client.DailyProjectDataClient;
009    import org.hackystat.dailyprojectdata.test.DailyProjectDataTestHelper;
010    import org.hackystat.sensorbase.client.SensorBaseClient;
011    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDatas;
012    import org.hackystat.utilities.tstamp.Tstamp;
013    import org.junit.Test;
014    import org.hackystat.dailyprojectdata.resource.build.TestBuildRestApi;
015    
016    /**
017     * Tests the Cache clear.
018     * 
019     * @author Philip Johnson
020     */
021    public class TestCacheRestApi extends DailyProjectDataTestHelper {
022    
023      /** The user for this test case. */
024      private String user = "TestCache@hackystat.org";
025    
026      /**
027       * Test that DELETE {host}/cache/{user} and {user}/{project} can be invoked successfully.
028       * 
029       * @throws Exception If problems occur.
030       */
031      @Test
032      public void deleteCacheServerSide() throws Exception {
033        // Now connect to the DPD server.
034        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
035        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(),
036            user, user);
037        dpdClient.authenticate();
038        assertTrue("Testing DPD user cache delete", dpdClient.clearServerCache());
039        assertTrue("Testing DPD user/project cache delete.", 
040            dpdClient.clearServerCache(user, "Default"));
041      }
042    
043      /**
044       * Tests that client-side cache clearing operations work as expected.
045       * @throws Exception if problems occur.
046       */
047      @Test
048      public void testCacheClientSide() throws Exception {
049        // Create a batch of test build data
050        String runtime = "2007-10-30T02:00:00";
051        String SUCCESS = "success";
052        String FAILURE = "failure";
053        SensorDatas batchData = new SensorDatas();
054        batchData.getSensorData().add(
055            TestBuildRestApi.makeBuild("2007-10-30T02:00:00", user, runtime, "cruisecontrol", SUCCESS));
056        batchData.getSensorData().add(
057            TestBuildRestApi.makeBuild("2007-10-30T02:10:00", user, runtime, "local", FAILURE));
058        batchData.getSensorData().add(
059            TestBuildRestApi.makeBuild("2007-10-30T02:15:00", user, runtime, null, SUCCESS));
060        batchData.getSensorData().add(
061            TestBuildRestApi.makeBuild("2007-10-30T02:20:00", user, runtime, null, SUCCESS));
062        batchData.getSensorData().add(
063            TestBuildRestApi.makeBuild("2007-10-30T02:25:00", user, runtime, "local", FAILURE));
064        // Connect to the sensorbase and register the DailyProjectDataCodeIssue user.
065        SensorBaseClient.registerUser(getSensorBaseHostName(), user);
066        SensorBaseClient client = new SensorBaseClient(getSensorBaseHostName(), user, user);
067        client.authenticate();
068        // Send the sensor data to the SensorBase.
069        client.putSensorDataBatch(batchData);
070    
071        // Now connect to the DPD server.
072        DailyProjectDataClient dpdClient = new DailyProjectDataClient(getDailyProjectDataHostName(),
073            user, user);
074        dpdClient.authenticate();
075        dpdClient.enableCaching("TestDpdCache", "testdpdcache", 1D, 1000L);
076        dpdClient.clearLocalCache();
077    
078        XMLGregorianCalendar requestTstamp = Tstamp.makeTimestamp("2007-10-30");
079    
080        String project = "Default";
081        // This thing should be cached.
082        dpdClient.getBuild(user, project, requestTstamp, null);
083        assertEquals("Check initial cache", 1, dpdClient.localCacheSize(user, project));
084        dpdClient.clearLocalCache();
085        assertEquals("Check cleared cache 1", 0, dpdClient.localCacheSize(user, project));
086    
087        // Add it back and check the project-specific clear.
088        dpdClient.getBuild(user, project, requestTstamp, null);
089        assertEquals("Check cache 2", 1, dpdClient.localCacheSize(user, project));
090        dpdClient.clearLocalCache(user, project);
091        assertEquals("Check cleared cache 2", 0, dpdClient.localCacheSize(user, project));
092        
093        // Finally, check to make sure the server-side cache can be cleared as well.  
094        // We just check to make sure we can call the methods successfully.
095       dpdClient.clearServerCache();
096       dpdClient.clearServerCache(user, project);
097      }
098    }