Search in sources :

Example 11 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenOpeningTxTwice_Throw.

@Test
public void whenOpeningTxTwice_Throw() throws Throwable {
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        exception.expect(hasStatus(Status.FAILED_PRECONDITION));
        throw tx.receive().error();
    }
}
Also used : TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Test(org.junit.Test)

Example 12 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenSendingNextBeforeQuery_Throw.

@Test
public void whenSendingNextBeforeQuery_Throw() throws Throwable {
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(nextRequest(IteratorId.getDefaultInstance()));
        exception.expect(hasStatus(Status.FAILED_PRECONDITION));
        throw tx.receive().error();
    }
}
Also used : TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Test(org.junit.Test)

Example 13 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenSendingNextAfterStop_Throw.

@Test
public void whenSendingNextAfterStop_Throw() throws Throwable {
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(execQueryRequest(QUERY, null));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(stopRequest(iterator));
        tx.receive();
        tx.send(nextRequest(iterator));
        exception.expect(hasStatus(Status.FAILED_PRECONDITION));
        throw tx.receive().error();
    }
}
Also used : IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) Test(org.junit.Test)

Example 14 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenRemovingRolePlayer_RolePlayerIsRemoved.

@Test
public void whenRemovingRolePlayer_RolePlayerIsRemoved() throws InterruptedException {
    ConceptId conceptId = ConceptId.of("V123456");
    ConceptId roleId = ConceptId.of("ROLE");
    ConceptId playerId = ConceptId.of("PLAYER");
    Concept concept = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(conceptId)).thenReturn(concept);
    when(concept.isRelationship()).thenReturn(true);
    Role role = mock(Role.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(roleId)).thenReturn(role);
    when(role.isRole()).thenReturn(true);
    when(role.asRole()).thenReturn(role);
    when(role.getId()).thenReturn(roleId);
    Entity player = mock(Entity.class, RETURNS_DEEP_STUBS);
    when(tx.getConcept(playerId)).thenReturn(player);
    when(player.isEntity()).thenReturn(true);
    when(player.asEntity()).thenReturn(player);
    when(player.isThing()).thenReturn(true);
    when(player.asThing()).thenReturn(player);
    when(player.getId()).thenReturn(playerId);
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.READ));
        tx.receive().ok();
        ConceptMethod<Void> conceptMethod = ConceptMethods.removeRolePlayer(RolePlayer.create(role, player));
        tx.send(GrpcUtil.runConceptMethodRequest(conceptId, conceptMethod));
        tx.receive().ok();
        verify(concept.asRelationship()).removeRolePlayer(role, player);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Role(ai.grakn.concept.Role) Entity(ai.grakn.concept.Entity) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) ConceptId(ai.grakn.concept.ConceptId) Test(org.junit.Test)

Example 15 with TxGrpcCommunicator

use of ai.grakn.grpc.TxGrpcCommunicator in project grakn by graknlabs.

the class GrpcServerTest method whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned.

// This tests uses an endless stream, so a failure may cause it to never terminate
@Test(timeout = 1000)
public void whenExecutingAQueryRemotelyAndAskingForOneResult_OnlyOneResultIsReturned() throws InterruptedException {
    Concept conceptX = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptX.getId()).thenReturn(ConceptId.of("V123"));
    when(conceptX.isEntity()).thenReturn(true);
    when(conceptX.asEntity().type().getLabel()).thenReturn(Label.of("L123"));
    Concept conceptY = mock(Concept.class, RETURNS_DEEP_STUBS);
    when(conceptY.getId()).thenReturn(ConceptId.of("V456"));
    when(conceptY.isEntity()).thenReturn(true);
    when(conceptY.asEntity().type().getLabel()).thenReturn(Label.of("L456"));
    ImmutableList<Answer> answers = ImmutableList.of(new QueryAnswer(ImmutableMap.of(Graql.var("x"), conceptX)), new QueryAnswer(ImmutableMap.of(Graql.var("y"), conceptY)));
    // TODO: reduce wtf
    when(query.results(any())).thenAnswer(params -> query.stream().map(params.<GrpcConverter>getArgument(0)::convert));
    // Produce an endless stream of results - this means if the behaviour is not lazy this will never terminate
    when(query.stream()).thenAnswer(params -> Stream.generate(answers::stream).flatMap(Function.identity()));
    try (TxGrpcCommunicator tx = TxGrpcCommunicator.create(stub)) {
        tx.send(openRequest(MYKS, GraknTxType.WRITE));
        tx.receive();
        tx.send(execQueryRequest(QUERY, null));
        IteratorId iterator = tx.receive().ok().getIteratorId();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(nextRequest(iterator));
        tx.receive().ok();
        tx.send(stopRequest(iterator));
        TxResponse response = tx.receive().ok();
        assertEquals(doneResponse(), response);
    }
}
Also used : GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) Concept(ai.grakn.concept.Concept) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Answer(ai.grakn.graql.admin.Answer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) QueryAnswer(ai.grakn.graql.internal.query.QueryAnswer) IteratorId(ai.grakn.rpc.generated.GrpcIterator.IteratorId) TxGrpcCommunicator(ai.grakn.grpc.TxGrpcCommunicator) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Aggregations

TxGrpcCommunicator (ai.grakn.grpc.TxGrpcCommunicator)31 Test (org.junit.Test)31 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)9 Concept (ai.grakn.concept.Concept)8 ConceptId (ai.grakn.concept.ConceptId)8 IteratorId (ai.grakn.rpc.generated.GrpcIterator.IteratorId)7 TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)4 GraknException (ai.grakn.exception.GraknException)3 Answer (ai.grakn.graql.admin.Answer)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 Mockito.doAnswer (org.mockito.Mockito.doAnswer)2 Entity (ai.grakn.concept.Entity)1 Label (ai.grakn.concept.Label)1 Role (ai.grakn.concept.Role)1 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)1 Open (ai.grakn.rpc.generated.GrpcGrakn.Open)1 QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)1 ArrayList (java.util.ArrayList)1