Search in sources :

Example 6 with CombinedFeed

use of org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed in project jersey by jersey.

the class CombinedFeedResourceTest method testDelete.

@Test
public void testDelete() {
    // Saving entity
    CombinedFeed feed = combinedFeed("1");
    datastore.put(feed.getId(), feed);
    // Deleting entity
    Response deleteResp = target.path(feed.getId()).request().delete();
    assertEquals(Status.NO_CONTENT.getStatusCode(), deleteResp.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 7 with CombinedFeed

use of org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed in project jersey by jersey.

the class CombinedFeedServiceTest method testSaveWithRefreshPeriod.

@Test
public void testSaveWithRefreshPeriod() {
    String id = "1";
    CombinedFeed feed = new CombinedFeed.CombinedFeedBuilder(id, "http://localhost").title("title").description("description").refreshPeriod(10).build();
    expect(idGenerator.getId()).andReturn(id);
    expect(datastore.put(eq(id), eq(feed))).andReturn(null);
    replayAll();
    CombinedFeed actual = testedClass.save(feed);
    verifyAll();
    assertEquals(feed, actual);
}
Also used : CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) Test(org.junit.Test)

Example 8 with CombinedFeed

use of org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed in project jersey by jersey.

the class ReadWriteLockDataStoreTest method testGetEntityWrongCast.

@Test(expected = ClassCastException.class)
public void testGetEntityWrongCast() {
    String id = "1";
    CombinedFeed feed = getCombinedFeed(id);
    observer.save(feed);
    replayAll();
    // save entity
    testedClass.put(id, feed);
    testedClass.get(id, FeedEntry.class);
    verifyAll();
}
Also used : CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) Test(org.junit.Test)

Example 9 with CombinedFeed

use of org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed in project jersey by jersey.

the class ReadWriteLockDataStoreTest method testLoadDatastore.

@Test
public void testLoadDatastore() {
    // Insert new combined Feed
    String id = "1";
    CombinedFeed feed = new CombinedFeed.CombinedFeedBuilder(id, "http://localhost").title("title").description("description").refreshPeriod(5L).build();
    // call save mock
    observer.save(feed);
    // Create a datastore with one Combined Feed and serialize it
    String id2 = "2";
    CombinedFeed deserializedFeed = new CombinedFeed.CombinedFeedBuilder(id2, "http://localhost").title("deserialized_title").description("deserialized_description").refreshPeriod(5L).build();
    HashMap<String, Serializable> datastore = new HashMap<>();
    datastore.put(id2, deserializedFeed);
    // call mocks after a load of the datastore
    Capture<Set<String>> captureRemoveAll = EasyMock.newCapture();
    Capture<Collection<Serializable>> captureSaveAll = EasyMock.newCapture();
    observer.removeAll(capture(captureRemoveAll));
    observer.saveAll(capture(captureSaveAll));
    replayAll();
    // Insert feed into the old datastore
    testedClass.put(id, feed);
    byte[] serializedDatastore = SerializationUtils.serialize(datastore);
    // Load the serialized datastore
    ByteArrayInputStream input = new ByteArrayInputStream(serializedDatastore);
    try {
        testedClass.load(input);
        // Test that the new datastore does not contain old Combined Feed
        CombinedFeed previousCombinedFeed = testedClass.get(id, CombinedFeed.class);
        if (previousCombinedFeed != null) {
            fail("The previous combined feed should be deleted.");
        }
        // Test that the new datastore contains new Combined Feed
        CombinedFeed newCombinedFeed = testedClass.get(id2, CombinedFeed.class);
        assertEquals(deserializedFeed, newCombinedFeed);
    } catch (IOException e) {
        fail(e.getMessage());
    }
    verifyAll();
    // Verify captured values
    // Check whether the registered observer was called because of removing entities
    Set<String> previousKeySet = captureRemoveAll.getValue();
    if (!previousKeySet.contains(id)) {
        fail("The previous keys should be deleted.");
    }
    // Check whether the registered observer was called because of saving entities
    Collection<Serializable> newlySavedEntities = captureSaveAll.getValue();
    assertEquals(1, newlySavedEntities.size());
    boolean exists = newlySavedEntities.stream().map(CombinedFeed.class::cast).anyMatch(entity -> Objects.equals(entity.getId(), id2));
    if (!exists) {
        fail("The new stored CombinedFeed was not found.");
    }
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) HashMap(java.util.HashMap) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) Collection(java.util.Collection) Test(org.junit.Test)

Example 10 with CombinedFeed

use of org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed in project jersey by jersey.

the class ReadWriteLockDataStoreTest method testRemoveEntity.

@Test
public void testRemoveEntity() {
    String id = "1";
    CombinedFeed feed = getCombinedFeed(id);
    observer.save(feed);
    observer.remove(id);
    replayAll();
    // save entity
    testedClass.put(id, feed);
    Serializable removedEntity = testedClass.put(id, null);
    verifyAll();
    if (!(removedEntity instanceof CombinedFeed)) {
        fail("The previous entity is not an instance of CombinedFeed");
    }
    assertEquals(feed, removedEntity);
}
Also used : Serializable(java.io.Serializable) CombinedFeed(org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed) Test(org.junit.Test)

Aggregations

CombinedFeed (org.glassfish.jersey.examples.feedcombiner.model.CombinedFeed)25 Test (org.junit.Test)19 Serializable (java.io.Serializable)7 Response (javax.ws.rs.core.Response)5 JerseyTest (org.glassfish.jersey.test.JerseyTest)5 IOException (java.io.IOException)2 URL (java.net.URL)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 FeedEntry (org.glassfish.jersey.examples.feedcombiner.model.FeedEntry)2 ReadWriteLockDataStore (org.glassfish.jersey.examples.feedcombiner.store.ReadWriteLockDataStore)2 SyndEntry (com.sun.syndication.feed.synd.SyndEntry)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1