Search in sources :

Example 11 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse in project grakn by graknlabs.

the class RemoteConceptsTest method whenCallingAllRolePlayers_GetTheExpectedResult.

@Test
public void whenCallingAllRolePlayers_GetTheExpectedResult() {
    Role foo = RemoteConcepts.createRole(tx, ConceptId.of("foo"));
    Role bar = RemoteConcepts.createRole(tx, ConceptId.of("bar"));
    Thing a = RemoteConcepts.createEntity(tx, A);
    Thing b = RemoteConcepts.createRelationship(tx, B);
    Thing c = RemoteConcepts.createAttribute(tx, C);
    Stream<RolePlayer> mockedResponse = Stream.of(RolePlayer.create(foo, a), RolePlayer.create(bar, b), RolePlayer.create(bar, c));
    TxResponse response = GET_ROLE_PLAYERS.createTxResponse(server.grpcIterators(), mockedResponse);
    server.setResponse(GrpcUtil.runConceptMethodRequest(ID, GET_ROLE_PLAYERS), response);
    Map<Role, Set<Thing>> allRolePlayers = relationship.allRolePlayers();
    Map<Role, Set<Thing>> expected = ImmutableMap.of(foo, ImmutableSet.of(a), bar, ImmutableSet.of(b, c));
    assertEquals(expected, allRolePlayers);
}
Also used : Role(ai.grakn.concept.Role) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) RolePlayer(ai.grakn.grpc.RolePlayer) Thing(ai.grakn.concept.Thing) Test(org.junit.Test)

Example 12 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse in project grakn by graknlabs.

the class RemoteConceptsTest method whenCallingIsDeleted_GetTheExpectedResult.

@Test
public void whenCallingIsDeleted_GetTheExpectedResult() {
    TxResponse response = TxResponse.newBuilder().setOptionalConcept(convertOptionalConcept(Optional.of(concept))).build();
    server.setResponse(GrpcUtil.getConceptRequest(ID), response);
    assertFalse(entity.isDeleted());
    TxResponse nullResponse = TxResponse.newBuilder().setOptionalConcept(convertOptionalConcept(Optional.empty())).build();
    server.setResponse(GrpcUtil.getConceptRequest(ID), nullResponse);
    assertTrue(entity.isDeleted());
}
Also used : TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 13 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse in project grakn by graknlabs.

the class GrpcServerMock method before.

@Override
protected void before() throws Throwable {
    when(service.tx(any())).thenAnswer(args -> {
        serverResponses = args.getArgument(0);
        return serverRequests;
    });
    doAnswer(args -> {
        StreamObserver<DeleteResponse> deleteResponses = args.getArgument(1);
        deleteResponses.onNext(GrpcUtil.deleteResponse());
        deleteResponses.onCompleted();
        return null;
    }).when(service).delete(any(), any());
    // Return a default "done" response to every message from the client
    doAnswer(args -> {
        if (serverResponses == null) {
            throw new IllegalArgumentException("Set-up of rule not called");
        }
        TxRequest request = args.getArgument(0);
        Optional<TxResponse> next = grpcIterators.next(request.getNext().getIteratorId());
        serverResponses.onNext(next.orElse(GrpcUtil.doneResponse()));
        return null;
    }).when(serverRequests).onNext(any());
    // Return a default "complete" response to every "complete" message from the client
    doAnswer(args -> {
        if (serverResponses == null) {
            throw new IllegalArgumentException("Set-up of rule not called");
        }
        serverResponses.onCompleted();
        return null;
    }).when(serverRequests).onCompleted();
    serverRule.getServiceRegistry().addService(service);
}
Also used : DeleteResponse(ai.grakn.rpc.generated.GrpcGrakn.DeleteResponse) TxRequest(ai.grakn.rpc.generated.GrpcGrakn.TxRequest) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse)

Example 14 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQueryWithABooleanResult_GetABoolBack.

@Test
public void whenExecutingAQueryWithABooleanResult_GetABoolBack() {
    Query<?> query = match(var("x").isa("person")).aggregate(ask());
    String queryString = query.toString();
    TxResponse response = TxResponse.newBuilder().setQueryResult(QueryResult.newBuilder().setOtherResult("true")).build();
    server.setResponseSequence(GrpcUtil.execQueryRequest(query), response);
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        assertTrue(tx.graql().<Query<Boolean>>parse(queryString).execute());
    }
}
Also used : GraknTx(ai.grakn.GraknTx) DefineQuery(ai.grakn.graql.DefineQuery) GetQuery(ai.grakn.graql.GetQuery) Query(ai.grakn.graql.Query) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Example 15 with TxResponse

use of ai.grakn.rpc.generated.GrpcGrakn.TxResponse in project grakn by graknlabs.

the class RemoteGraknTxTest method whenExecutingAQuery_GetAResultBack.

@Test
public void whenExecutingAQuery_GetAResultBack() {
    Query<?> query = match(var("x").isa("person")).get();
    String queryString = query.toString();
    GrpcConcept.Concept v123 = GrpcConcept.Concept.newBuilder().setId(V123).build();
    GrpcGrakn.Answer grpcAnswer = GrpcGrakn.Answer.newBuilder().putAnswer("x", v123).build();
    QueryResult queryResult = QueryResult.newBuilder().setAnswer(grpcAnswer).build();
    TxResponse response = TxResponse.newBuilder().setQueryResult(queryResult).build();
    server.setResponseSequence(GrpcUtil.execQueryRequest(query), response);
    List<Answer> results;
    try (GraknTx tx = RemoteGraknTx.create(session, GrpcUtil.openRequest(KEYSPACE, GraknTxType.WRITE))) {
        // The open request
        verify(server.requests()).onNext(any());
        results = tx.graql().<GetQuery>parse(queryString).execute();
    }
    Answer answer = Iterables.getOnlyElement(results);
    assertEquals(answer.vars(), ImmutableSet.of(var("x")));
    assertEquals(ConceptId.of("V123"), answer.get(var("x")).getId());
}
Also used : Answer(ai.grakn.graql.admin.Answer) GraknTx(ai.grakn.GraknTx) QueryResult(ai.grakn.rpc.generated.GrpcGrakn.QueryResult) GrpcGrakn(ai.grakn.rpc.generated.GrpcGrakn) GrpcConcept(ai.grakn.rpc.generated.GrpcConcept) GetQuery(ai.grakn.graql.GetQuery) TxResponse(ai.grakn.rpc.generated.GrpcGrakn.TxResponse) Test(org.junit.Test)

Aggregations

TxResponse (ai.grakn.rpc.generated.GrpcGrakn.TxResponse)21 Test (org.junit.Test)10 IteratorId (ai.grakn.rpc.generated.GrpcIterator.IteratorId)9 GrpcConcept (ai.grakn.rpc.generated.GrpcConcept)8 Concept (ai.grakn.concept.Concept)6 QueryResult (ai.grakn.rpc.generated.GrpcGrakn.QueryResult)6 Answer (ai.grakn.graql.admin.Answer)5 GraknTx (ai.grakn.GraknTx)4 TxGrpcCommunicator (ai.grakn.grpc.TxGrpcCommunicator)4 GetQuery (ai.grakn.graql.GetQuery)3 GrpcGrakn (ai.grakn.rpc.generated.GrpcGrakn)3 Attribute (ai.grakn.concept.Attribute)2 Role (ai.grakn.concept.Role)2 DefineQuery (ai.grakn.graql.DefineQuery)2 QueryBuilder (ai.grakn.graql.QueryBuilder)2 QueryAnswer (ai.grakn.graql.internal.query.QueryAnswer)2 TxRequest (ai.grakn.rpc.generated.GrpcGrakn.TxRequest)2 AttributeType (ai.grakn.concept.AttributeType)1 EntityType (ai.grakn.concept.EntityType)1 Label (ai.grakn.concept.Label)1