use of cz.o2.proxima.scheme.proto.ProtoSerializerFactory.TransactionProtoSerializer in project proxima-platform by O2-Czech-Republic.
the class ProtoSerializerFactoryTest method testTransactionSchemeProvider.
@SuppressWarnings("unchecked")
@Test
public void testTransactionSchemeProvider() {
Repository.ofTest(ConfigFactory.load("test-transactions-proto.conf").withFallback(ConfigFactory.load("test-transactions.conf")).resolve());
Repository repo = Repository.ofTest(ConfigFactory.load("test-transactions-proto.conf").withFallback(ConfigFactory.load("test-transactions.conf")).resolve());
EntityDescriptor transaction = repo.getEntity("_transaction");
Wildcard<Request> request = Wildcard.of(transaction, transaction.getAttribute("request.*"));
KeyAttribute keyAttribute = KeyAttributes.ofAttributeDescriptor(transaction, "t", request, 1L, "1");
List<KeyAttribute> wildcardQuery = KeyAttributes.ofWildcardQueryElements(transaction, "t", request, Arrays.asList(request.upsert(1L, "t", "1", System.currentTimeMillis(), newRequest(Request.Flags.OPEN)), request.upsert(2L, "t", "1", System.currentTimeMillis(), newRequest(Request.Flags.OPEN))));
assertTrue(request.getValueSerializer() instanceof TransactionProtoSerializer);
assertTrue(request.getValueSerializer().isUsable());
AttributeDescriptor<Response> response = transaction.getAttribute("response.*");
assertTrue(response.getValueSerializer() instanceof TransactionProtoSerializer);
assertTrue(request.getValueSerializer().isUsable());
AttributeDescriptor<State> state = transaction.getAttribute("state");
assertTrue(state.getValueSerializer() instanceof TransactionProtoSerializer);
assertTrue(state.getValueSerializer().isUsable());
AttributeDescriptor<State> commit = transaction.getAttribute("commit");
assertTrue(state.getValueSerializer() instanceof TransactionProtoSerializer);
assertTrue(state.getValueSerializer().isUsable());
StreamElement update = StreamElement.upsert(transaction, request, 1L, "t", request.toAttributePrefix() + "1", System.currentTimeMillis(), new byte[] {});
StreamElement delete = StreamElement.delete(transaction, request, 1L, "t", request.toAttributePrefix() + "2", System.currentTimeMillis());
KeyAttribute keyAttributeSingleWildcard = KeyAttributes.ofStreamElement(update);
KeyAttribute keyAttributeDelete = KeyAttributes.ofStreamElement(delete);
KeyAttribute missingGet = KeyAttributes.ofMissingAttribute(transaction, "t", request, "1");
long now = System.currentTimeMillis();
List<TransactionUpdate> transactionUpdates = Collections.singletonList(new TransactionUpdate("stateFamily", StreamElement.upsert(transaction, state, UUID.randomUUID().toString(), "t", state.getName(), System.currentTimeMillis(), new byte[] {})));
Request someRequest = newRequest(keyAttribute, Request.Flags.OPEN);
List<Pair<Object, AttributeDescriptor<?>>> toVerify = Arrays.asList(Pair.of(newRequest(keyAttribute, Request.Flags.OPEN), request), Pair.of(newRequest(keyAttributeSingleWildcard, Request.Flags.OPEN), request), Pair.of(newRequest(keyAttribute, Request.Flags.COMMIT), request), Pair.of(newRequest(keyAttributeSingleWildcard, Request.Flags.COMMIT), request), Pair.of(newRequest(keyAttribute, Request.Flags.UPDATE), request), Pair.of(newRequest(keyAttributeSingleWildcard, Request.Flags.UPDATE), request), Pair.of(newRequest(wildcardQuery, Request.Flags.OPEN), request), Pair.of(newRequest(Request.Flags.ROLLBACK), request), Pair.of(Response.forRequest(someRequest).open(1L, now), response), Pair.of(Response.forRequest(someRequest).updated(), response), Pair.of(Response.forRequest(someRequest).committed(), response), Pair.of(Response.forRequest(someRequest).aborted(), response), Pair.of(Response.forRequest(someRequest).duplicate(100L), response), Pair.of(Response.empty(), response), Pair.of(Commit.of(1L, System.currentTimeMillis(), Arrays.asList(update, delete)), commit), Pair.of(State.open(1L, now, Sets.newHashSet(keyAttribute)), state), Pair.of(State.open(1L, now, Sets.newHashSet(keyAttribute, keyAttributeSingleWildcard)).committed(Sets.newHashSet(keyAttribute)), state), Pair.of(State.empty(), state), Pair.of(State.open(1L, now, Sets.newHashSet(keyAttribute)).update(Collections.singletonList(keyAttributeSingleWildcard)), state), Pair.of(State.open(1L, now, Sets.newHashSet(keyAttribute)).aborted(), state), Pair.of(State.open(1L, now, Sets.newHashSet(missingGet)).aborted(), state), Pair.of(State.open(1L, now, Sets.newHashSet(keyAttributeSingleWildcard)).committed(Sets.newHashSet(keyAttributeSingleWildcard)), state), Pair.of(State.open(1L, now, Collections.emptyList()).committed(Sets.newHashSet(keyAttributeDelete)), state), Pair.of(Commit.of(transactionUpdates), commit));
toVerify.forEach(p -> {
ValueSerializer<Object> serializer = (ValueSerializer<Object>) p.getSecond().getValueSerializer();
byte[] bytes = serializer.serialize(p.getFirst());
assertNotNull(bytes);
// we do not serialize the target partition for responses
if (p.getFirst() instanceof Response) {
compareResponses((Response) p.getFirst(), (Response) Optionals.get(serializer.deserialize(bytes)));
} else if (p.getFirst() instanceof Commit) {
compareCommit((Commit) p.getFirst(), (Commit) Optionals.get(serializer.deserialize(bytes)));
} else {
assertEquals(p.getFirst(), Optionals.get(serializer.deserialize(bytes)));
}
});
}
Aggregations