Search in sources :

Example 6 with MatchedExecutionHeaders

use of org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders in project kogito-apps by kiegroup.

the class AbstractTrustyServiceIT method testStoreAndRetrieveExecution.

@Test
public void testStoreAndRetrieveExecution() {
    storeExecution("myExecution", 1591692958000L);
    OffsetDateTime from = OffsetDateTime.ofInstant(Instant.ofEpochMilli(1591692957000L), ZoneOffset.UTC);
    OffsetDateTime to = OffsetDateTime.ofInstant(Instant.ofEpochMilli(1591692959000L), ZoneOffset.UTC);
    MatchedExecutionHeaders result = trustyService.getExecutionHeaders(from, to, 100, 0, "");
    Assertions.assertEquals(1, result.getExecutions().size());
    Assertions.assertEquals("myExecution", result.getExecutions().get(0).getExecutionId());
}
Also used : MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) OffsetDateTime(java.time.OffsetDateTime) Test(org.junit.jupiter.api.Test)

Example 7 with MatchedExecutionHeaders

use of org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders in project kogito-apps by kiegroup.

the class TrustyServiceImpl method getExecutionHeaders.

@Override
public MatchedExecutionHeaders getExecutionHeaders(OffsetDateTime from, OffsetDateTime to, int limit, int offset, String prefix) {
    Storage<String, Decision> storage = storageService.getDecisionsStorage();
    List<AttributeFilter<?>> filters = new ArrayList<>();
    filters.add(QueryFilterFactory.like(Execution.EXECUTION_ID_FIELD, prefix + "*"));
    filters.add(QueryFilterFactory.greaterThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, from.toInstant().toEpochMilli()));
    filters.add(QueryFilterFactory.lessThanEqual(Execution.EXECUTION_TIMESTAMP_FIELD, to.toInstant().toEpochMilli()));
    ArrayList result = new ArrayList<>(storage.query().sort(asList(orderBy(Execution.EXECUTION_TIMESTAMP_FIELD, DESC))).filter(filters).execute());
    if (result.size() < offset) {
        throw new IllegalArgumentException("Out of bound start offset in result");
    }
    return new MatchedExecutionHeaders(result.subList(offset, Math.min(offset + limit, result.size())), result.size());
}
Also used : MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) AttributeFilter(org.kie.kogito.persistence.api.query.AttributeFilter) ArrayList(java.util.ArrayList) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision)

Example 8 with MatchedExecutionHeaders

use of org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders in project kogito-apps by kiegroup.

the class ExecutionsApiV1IT method givenRequestWithoutLimitAndOffsetParametersWhenExecutionEndpointIsCalledThenTheDefaultValuesAreCorrect.

@Test
void givenRequestWithoutLimitAndOffsetParametersWhenExecutionEndpointIsCalledThenTheDefaultValuesAreCorrect() {
    Mockito.when(executionService.getExecutionHeaders(any(OffsetDateTime.class), any(OffsetDateTime.class), any(Integer.class), any(Integer.class), any(String.class))).thenReturn(new MatchedExecutionHeaders(new ArrayList<>(), 0));
    ExecutionsResponse response = given().contentType(ContentType.JSON).when().get("/executions?from=2000-01-01T00:00:00Z&to=2021-01-01T00:00:00Z").as(ExecutionsResponse.class);
    Assertions.assertEquals(100, response.getLimit());
    Assertions.assertEquals(0, response.getOffset());
    Assertions.assertEquals(0, response.getHeaders().size());
}
Also used : MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) OffsetDateTime(java.time.OffsetDateTime) ExecutionsResponse(org.kie.kogito.trusty.service.common.responses.ExecutionsResponse) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) QuarkusTest(io.quarkus.test.junit.QuarkusTest) Test(org.junit.jupiter.api.Test)

Example 9 with MatchedExecutionHeaders

use of org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders in project kogito-apps by kiegroup.

the class TrustyServiceTest method givenADecisionWhenADecisionIsStoredAndRetrievedThenTheOriginalObjectIsReturned.

@Test
@SuppressWarnings("unchecked")
void givenADecisionWhenADecisionIsStoredAndRetrievedThenTheOriginalObjectIsReturned() {
    Decision decision = new Decision();
    decision.setExecutionId(TEST_EXECUTION_ID);
    Query queryMock = mock(Query.class);
    when(queryMock.filter(any(List.class))).thenReturn(queryMock);
    when(queryMock.offset(any(Integer.class))).thenReturn(queryMock);
    when(queryMock.sort(any(List.class))).thenReturn(queryMock);
    when(queryMock.execute()).thenReturn(List.of(decision));
    Storage storageMock = mock(Storage.class);
    when(storageMock.put(eq(TEST_EXECUTION_ID), any(Object.class))).thenReturn(decision);
    when(storageMock.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(false);
    when(storageMock.query()).thenReturn(queryMock);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
    trustyService.storeDecision(TEST_EXECUTION_ID, decision);
    MatchedExecutionHeaders result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 100, 0, "");
    assertEquals(1, result.getExecutions().size());
    assertEquals(decision.getExecutionId(), result.getExecutions().get(0).getExecutionId());
}
Also used : Storage(org.kie.kogito.persistence.api.Storage) MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) Decision(org.kie.kogito.trusty.storage.api.model.decision.Decision) Test(org.junit.jupiter.api.Test)

Example 10 with MatchedExecutionHeaders

use of org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders in project kogito-apps by kiegroup.

the class TrustyServiceTest method givenNoExecutionsNoExceptionsAreRaised.

@Test
@SuppressWarnings("unchecked")
void givenNoExecutionsNoExceptionsAreRaised() {
    Query queryMock = mock(Query.class);
    when(queryMock.filter(any(List.class))).thenReturn(queryMock);
    when(queryMock.sort(any(List.class))).thenReturn(queryMock);
    when(queryMock.execute()).thenReturn(new ArrayList<>());
    Storage storageMock = mock(Storage.class);
    when(storageMock.query()).thenReturn(queryMock);
    when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
    MatchedExecutionHeaders result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 100, 0, "");
    assertEquals(0, result.getExecutions().size());
    assertEquals(0, result.getAvailableResults());
}
Also used : Storage(org.kie.kogito.persistence.api.Storage) MatchedExecutionHeaders(org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders) Query(org.kie.kogito.persistence.api.query.Query) List(java.util.List) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Aggregations

MatchedExecutionHeaders (org.kie.kogito.trusty.service.common.models.MatchedExecutionHeaders)13 Test (org.junit.jupiter.api.Test)11 OffsetDateTime (java.time.OffsetDateTime)8 ArrayList (java.util.ArrayList)8 QuarkusTest (io.quarkus.test.junit.QuarkusTest)4 Decision (org.kie.kogito.trusty.storage.api.model.decision.Decision)4 List (java.util.List)3 Storage (org.kie.kogito.persistence.api.Storage)3 Query (org.kie.kogito.persistence.api.query.Query)3 ExecutionsResponse (org.kie.kogito.trusty.service.common.responses.ExecutionsResponse)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 Instant (java.time.Instant)1 DateTimeParseException (java.time.format.DateTimeParseException)1 GET (javax.ws.rs.GET)1 Produces (javax.ws.rs.Produces)1 Operation (org.eclipse.microprofile.openapi.annotations.Operation)1 APIResponses (org.eclipse.microprofile.openapi.annotations.responses.APIResponses)1 AttributeFilter (org.kie.kogito.persistence.api.query.AttributeFilter)1 ExecutionHeaderResponse (org.kie.kogito.trusty.service.common.responses.ExecutionHeaderResponse)1 Execution (org.kie.kogito.trusty.storage.api.model.Execution)1