use of org.kie.kogito.persistence.api.Storage in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenAModelWhenStoreModelIsCalledMoreThanOnceForSameModelThenExceptionIsThrown.
@Test
@SuppressWarnings("unchecked")
void givenAModelWhenStoreModelIsCalledMoreThanOnceForSameModelThenExceptionIsThrown() {
String model = TEST_MODEL;
Storage storageMock = mock(Storage.class);
DMNModelWithMetadata dmnModelWithMetadata = buildDmnModel(model);
DMNModelMetadata modelIdentifier = dmnModelWithMetadata.getModelMetaData();
when(storageMock.containsKey(modelIdentifier.getIdentifier())).thenReturn(true);
when(storageMock.put(any(Object.class), any(Object.class))).thenReturn(model);
when(trustyStorageServiceMock.getModelStorage(DMNModelWithMetadata.class)).thenReturn(storageMock);
assertThrows(IllegalArgumentException.class, () -> trustyService.storeModel(dmnModelWithMetadata));
}
use of org.kie.kogito.persistence.api.Storage 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());
}
use of org.kie.kogito.persistence.api.Storage in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload.
@Test
@SuppressWarnings("unchecked")
void givenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventHasCorrectPayload() {
Storage<String, Decision> decisionStorage = mock(Storage.class);
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
Decision decision = new Decision(TEST_EXECUTION_ID, TEST_SOURCE_URL, TEST_SERVICE_URL, 0L, true, null, "model", "modelNamespace", List.of(new DecisionInput("IN1", "yearsOfService", new UnitValue("integer", "integer", new IntNode(10)))), List.of(new DecisionOutcome("OUT1", "salary", "SUCCEEDED", new UnitValue("integer", "integer", new IntNode(1000)), Collections.emptyList(), Collections.emptyList())));
when(decisionStorage.containsKey(eq(TEST_EXECUTION_ID))).thenReturn(true);
when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(decisionStorage);
when(trustyStorageServiceMock.getCounterfactualRequestStorage()).thenReturn(counterfactualStorage);
when(decisionStorage.get(eq(TEST_EXECUTION_ID))).thenReturn(decision);
trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("salary", new UnitValue("integer", "integer", new IntNode(2000)))), List.of(new CounterfactualSearchDomain("yearsOfService", new CounterfactualSearchDomainUnitValue("integer", "integer", false, new CounterfactualDomainRange(new IntNode(10), new IntNode(30))))));
verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
assertEquals(TEST_SERVICE_URL, request.getServiceUrl());
// Check original input value has been copied into CF request
assertEquals(1, request.getOriginalInputs().size());
assertTrue(request.getOriginalInputs().stream().anyMatch(i -> i.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(decision.getInputs().iterator().next().getValue().toUnit().getValue().asInt(), request.getOriginalInputs().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF goals have been copied into CF request
assertEquals(1, request.getGoals().size());
assertTrue(request.getGoals().stream().anyMatch(g -> g.getName().equals("salary")));
// It is safe to use the iterator unchecked as the collection only contains one item
assertEquals(2000, request.getGoals().iterator().next().getValue().toUnit().getValue().asInt());
// Check CF search domains have been copied into CF request
assertEquals(1, request.getSearchDomains().size());
assertTrue(request.getSearchDomains().stream().anyMatch(sd -> sd.getName().equals("yearsOfService")));
// It is safe to use the iterator unchecked as the collection only contains one item
CounterfactualSearchDomainValue searchDomain = request.getSearchDomains().iterator().next().getValue();
assertTrue(searchDomain instanceof CounterfactualSearchDomainUnitValue);
CounterfactualSearchDomainUnitValue unit = (CounterfactualSearchDomainUnitValue) searchDomain;
assertFalse(unit.isFixed());
assertNotNull(unit.getDomain());
assertTrue(unit.getDomain() instanceof CounterfactualDomainRange);
CounterfactualDomainRange range = (CounterfactualDomainRange) unit.getDomain();
assertEquals(10, range.getLowerBound().asInt());
assertEquals(30, range.getUpperBound().asInt());
// Check Max Running Time Seconds
assertEquals(MAX_RUNNING_TIME_SECONDS, request.getMaxRunningTimeSeconds());
}
use of org.kie.kogito.persistence.api.Storage 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());
}
use of org.kie.kogito.persistence.api.Storage in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenAModelWhenStoreModelIsCalledThenNoExceptionsAreThrown.
@Test
@SuppressWarnings("unchecked")
void givenAModelWhenStoreModelIsCalledThenNoExceptionsAreThrown() {
String model = TEST_MODEL;
Storage storageMock = mock(Storage.class);
when(storageMock.put(any(Object.class), any(Object.class))).thenReturn(model);
when(trustyStorageServiceMock.getModelStorage(DMNModelWithMetadata.class)).thenReturn(storageMock);
Assertions.assertDoesNotThrow(() -> trustyService.storeModel(buildDmnModel(model)));
}
Aggregations