gFeed/gCat-Feeder/src/test/java/org/gcube/data/publishing/gCatFeeder/service/mockups/PersistenceManagerMock.java

80 lines
2.7 KiB
Java

package org.gcube.data.publishing.gCatFeeder.service.mockups;
import java.util.Collection;
import java.util.Collections;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import javax.inject.Singleton;
import java.util.function.BiFunction;
import org.gcube.data.publishing.gCatFeeder.service.engine.PersistenceManager;
import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionDescriptor;
import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionDescriptorFilter;
import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionRequest;
import org.gcube.data.publishing.gCatFeeder.service.model.ExecutionStatus;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.ElementNotFound;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.InvalidRequest;
import org.gcube.data.publishing.gCatFeeder.service.model.fault.PersistenceError;
@Singleton
public class PersistenceManagerMock implements PersistenceManager {
@Override
public ExecutionDescriptor create(ExecutionRequest request) throws PersistenceError, InvalidRequest {
return getEquivalent(request);
}
@Override
public ExecutionDescriptor getById(String id) throws PersistenceError, ElementNotFound, InvalidRequest {
if(theMap.containsKey(id))
return theMap.get(id);
else throw new ElementNotFound("Unable to find request with id "+id);
}
@Override
public Collection<ExecutionDescriptor> get(ExecutionDescriptorFilter filter)
throws PersistenceError, InvalidRequest {
return Collections.emptyList();
}
@Override
public boolean update(ExecutionDescriptor toUpdate) throws PersistenceError, ElementNotFound {
if(theMap.containsKey(toUpdate.getId())) {
theMap.replace(toUpdate.getId(), toUpdate);
return true;
}
else throw new ElementNotFound("Unable to find request with id "+toUpdate.getId());
}
@Override
public boolean acquire(String id) throws PersistenceError, ElementNotFound {
theMap.computeIfPresent(id, BiFunction<String, ExecutionDescriptor, ExecutionDescriptor> bi = (x,y) ->{
});
}
// Actuall persistence in map
private ConcurrentHashMap<String,ExecutionDescriptor> theMap=new ConcurrentHashMap<>();
private ExecutionDescriptor getEquivalent(ExecutionRequest req) {
ExecutionDescriptor toReturn=new ExecutionDescriptor();
toReturn.setCallerContext("TEST");
toReturn.setCallerIdentity("TESTER");
toReturn.setCallerEncryptedToken("TEST");
toReturn.setCatalogues(req.getToInvokeControllers());
toReturn.setCollectors(req.getToInvokeCollectors());
toReturn.setId(UUID.randomUUID().toString());
toReturn.setStatus(ExecutionStatus.PENDING);
theMap.put(toReturn.getId(), toReturn);
return toReturn;
}
}