use of java.io.Serializable in project jersey by jersey.
the class ReadWriteLockDataStore method get.
public <T extends Serializable> T get(String key, Class<T> type) {
requireNonNull(key, "The parameter 'key' must not be null");
requireNonNull(type, "The parameter 'type' must not be null");
readerLock.lock();
try {
Serializable object = datastore.get(key);
return type.cast(object);
} finally {
readerLock.unlock();
}
}
use of java.io.Serializable in project jersey by jersey.
the class CombinedFeedService method getAll.
@Override
public List<CombinedFeed> getAll() {
//TODO ugly, for purposes of CRUD controller
if (datastore instanceof ReadWriteLockDataStore) {
ReadWriteLockDataStore rwDatastore = (ReadWriteLockDataStore) datastore;
Collection<Serializable> entities = rwDatastore.getAll();
return entities.parallelStream().filter(entity -> entity instanceof CombinedFeed).map(CombinedFeed.class::cast).collect(Collectors.toList());
}
return new ArrayList<>();
}
use of java.io.Serializable in project jersey by jersey.
the class ReadWriteLockDataStore method put.
public <T extends Serializable> Serializable put(String key, T data) {
requireNonNull(key, "The parameter 'key' must not be null");
writerLock.lock();
try {
if (data == null) {
observers.forEach(observer -> observer.remove(key));
return datastore.remove(key);
}
Serializable previousEntity = datastore.put(key, data);
if (previousEntity == null) {
observers.forEach(observer -> observer.save(data));
}
return previousEntity;
} finally {
writerLock.unlock();
}
}
use of java.io.Serializable 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.");
}
}
use of java.io.Serializable 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);
}
Aggregations