use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class RetrieveServiceTest method testListValidWithTransaction.
@Test
public void testListValidWithTransaction() {
EntityDescriptor entity = server.repo.getEntity("dummy");
AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
String key = "my-fancy-entity-key";
List<StreamElement> elements = Arrays.asList(StreamElement.upsert(entity, attribute, 1000L, key, "wildcard.1", System.currentTimeMillis(), new byte[] { 1, 2, 3 }), StreamElement.upsert(entity, attribute, 1001L, key, "wildcard.2", System.currentTimeMillis(), new byte[] { 1, 2, 3, 4 }));
elements.forEach(el -> Optionals.get(server.direct.getWriter(el.getAttributeDescriptor())).write(el, (succ, exc) -> {
}));
String transactionId = UUID.randomUUID().toString();
Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).setTransactionId(transactionId).build();
List<Rpc.ListResponse> responses = new ArrayList<>();
final StreamObserver<Rpc.ListResponse> responseObserver;
responseObserver = new StreamObserver<Rpc.ListResponse>() {
@Override
public void onNext(Rpc.ListResponse res) {
responses.add(res);
}
@Override
public void onError(Throwable thrwbl) {
throw new RuntimeException(thrwbl);
}
@Override
public void onCompleted() {
}
};
retrieve.listAttributes(request, responseObserver);
assertEquals(1, responses.size());
Rpc.ListResponse response = responses.get(0);
assertEquals(200, response.getStatus());
assertEquals(2, response.getValueCount());
assertEquals("wildcard.1", response.getValue(0).getAttribute());
assertArrayEquals(new byte[] { 1, 2, 3 }, response.getValue(0).getValue().toByteArray());
assertEquals("wildcard.2", response.getValue(1).getAttribute());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.getValue(1).getValue().toByteArray());
assertEquals(1, transactionUpdates.size());
assertTrue(transactionUpdates.containsKey(transactionId));
assertEquals(KeyAttributes.ofWildcardQueryElements(entity, key, attribute, elements), transactionUpdates.get(transactionId));
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class RetrieveServiceTest method testListValidWithLimitWithTransaction.
@Test
public void testListValidWithLimitWithTransaction() {
EntityDescriptor entity = server.repo.getEntity("dummy");
AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
String key = "my-fancy-entity-key";
Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).setTransactionId(UUID.randomUUID().toString()).setLimit(1).build();
final List<Rpc.ListResponse> responses = new ArrayList<>();
final StreamObserver<Rpc.ListResponse> responseObserver;
responseObserver = new StreamObserver<Rpc.ListResponse>() {
@Override
public void onNext(Rpc.ListResponse res) {
responses.add(res);
}
@Override
public void onError(Throwable thrwbl) {
throw new RuntimeException(thrwbl);
}
@Override
public void onCompleted() {
}
};
retrieve.listAttributes(request, responseObserver);
assertEquals(1, responses.size());
Rpc.ListResponse response = responses.get(0);
assertEquals(400, response.getStatus());
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class RetrieveServiceTest method testListValid.
@Test
public void testListValid() {
EntityDescriptor entity = server.repo.getEntity("dummy");
AttributeDescriptor<?> attribute = entity.getAttribute("wildcard.*");
String key = "my-fancy-entity-key";
Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, "wildcard.1", System.currentTimeMillis(), new byte[] { 1, 2, 3 }), (s, err) -> {
});
Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, "wildcard.2", System.currentTimeMillis(), new byte[] { 1, 2, 3, 4 }), (s, err) -> {
});
Rpc.ListRequest request = Rpc.ListRequest.newBuilder().setEntity(entity.getName()).setWildcardPrefix("wildcard").setKey(key).build();
List<Rpc.ListResponse> responses = new ArrayList<>();
AtomicBoolean finished = new AtomicBoolean(false);
final StreamObserver<Rpc.ListResponse> responseObserver;
responseObserver = new StreamObserver<Rpc.ListResponse>() {
@Override
public void onNext(Rpc.ListResponse res) {
responses.add(res);
}
@Override
public void onError(Throwable thrwbl) {
throw new RuntimeException(thrwbl);
}
@Override
public void onCompleted() {
finished.set(true);
}
};
retrieve.listAttributes(request, responseObserver);
assertTrue(finished.get());
assertEquals(1, responses.size());
Rpc.ListResponse response = responses.get(0);
assertEquals(200, response.getStatus());
assertEquals(2, response.getValueCount());
assertEquals("wildcard.1", response.getValue(0).getAttribute());
assertArrayEquals(new byte[] { 1, 2, 3 }, response.getValue(0).getValue().toByteArray());
assertEquals("wildcard.2", response.getValue(1).getAttribute());
assertArrayEquals(new byte[] { 1, 2, 3, 4 }, response.getValue(1).getValue().toByteArray());
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class RetrieveServiceTest method testGetValidExtendedScheme.
@Test
public void testGetValidExtendedScheme() throws InvalidProtocolBufferException {
EntityDescriptor entity = server.repo.getEntity("test");
AttributeDescriptor<?> attribute = entity.getAttribute("data");
String key = "my-fancy-entity-key";
ExtendedMessage payload = ExtendedMessage.newBuilder().setFirst(1).setSecond(2).build();
Optionals.get(server.direct.getWriter(attribute)).write(StreamElement.upsert(entity, attribute, UUID.randomUUID().toString(), key, attribute.getName(), System.currentTimeMillis(), payload.toByteArray()), (s, err) -> {
});
Rpc.GetRequest request = Rpc.GetRequest.newBuilder().setEntity(entity.getName()).setAttribute(attribute.getName()).setKey(key).build();
final List<Rpc.GetResponse> responses = new ArrayList<>();
final AtomicBoolean finished = new AtomicBoolean(false);
final StreamObserver<Rpc.GetResponse> responseObserver;
responseObserver = new StreamObserver<Rpc.GetResponse>() {
@Override
public void onNext(Rpc.GetResponse res) {
responses.add(res);
}
@Override
public void onError(Throwable thrwbl) {
throw new RuntimeException(thrwbl);
}
@Override
public void onCompleted() {
finished.set(true);
}
};
retrieve.get(request, responseObserver);
assertTrue(finished.get());
assertEquals(1, responses.size());
Rpc.GetResponse response = responses.get(0);
assertEquals("Error: " + response.getStatus() + ": " + response.getStatusMessage(), 200, response.getStatus());
assertEquals(payload, ExtendedMessage.parseFrom(response.getValue().toByteArray()));
}
use of cz.o2.proxima.transaction.Response in project proxima-platform by O2-Czech-Republic.
the class RetrieveService method listAttributes.
@Override
public void listAttributes(Rpc.ListRequest request, StreamObserver<Rpc.ListResponse> responseObserver) {
try {
Metrics.LIST_REQUESTS.increment();
if (request.getEntity().isEmpty() || request.getKey().isEmpty() || request.getWildcardPrefix().isEmpty()) {
throw new Status(400, "Missing some required fields");
}
if (!request.getTransactionId().isEmpty() && (!request.getOffset().isEmpty() || request.getLimit() > 0)) {
throw new Status(400, "Unsupported: transactions do not support limited list requests, currently");
}
EntityDescriptor entity = repo.findEntity(request.getEntity()).orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " not found"));
AttributeDescriptor<Object> wildcard = entity.findAttribute(request.getWildcardPrefix() + ".*").orElseThrow(() -> new Status(404, "Entity " + request.getEntity() + " does not have wildcard attribute " + request.getWildcardPrefix()));
RandomAccessReader reader = instantiateReader(wildcard);
List<KeyValue<Object>> kvs = new ArrayList<>();
Rpc.ListResponse.Builder response = Rpc.ListResponse.newBuilder().setStatus(200);
synchronized (reader) {
reader.scanWildcard(request.getKey(), wildcard, reader.fetchOffset(RandomAccessReader.Listing.ATTRIBUTE, request.getOffset()), request.getLimit() > 0 ? request.getLimit() : -1, kvs::add);
kvs.forEach(kv -> response.addValue(Rpc.ListResponse.AttrValue.newBuilder().setAttribute(kv.getAttribute()).setValue(ByteString.copyFrom(kv.getValue()))));
}
noticeListResult(request, entity, wildcard, kvs);
replyLogged(responseObserver, request, response.build());
} catch (Status s) {
replyStatusLogged(responseObserver, request, s.statusCode, s.message);
} catch (Exception ex) {
log.error("Failed to process request {}", request, ex);
replyStatusLogged(responseObserver, request, 500, ex.getMessage());
}
responseObserver.onCompleted();
}
Aggregations