use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.
the class TraceEventConverterTest method doTest.
private static void doTest(TraceEvent traceEvent, Decision expectedDecision) throws JsonProcessingException, JSONException {
Decision actualDecision = TraceEventConverter.toDecision(traceEvent, TrustyServiceTestUtils.CLOUDEVENT_SOURCE, TrustyServiceTestUtils.CLOUDEVENT_SERVICE);
JSONAssert.assertEquals(MAPPER.writeValueAsString(expectedDecision), MAPPER.writeValueAsString(actualDecision), false);
}
use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.
the class DecisionMarshallerTest method testWriteAndRead.
@Test
public void testWriteAndRead() throws IOException {
List<DecisionInput> inputs = Collections.singletonList(new DecisionInput("id", "in", new UnitValue("nameIn", "number", JsonNodeFactory.instance.numberNode(10))));
List<DecisionOutcome> outcomes = Collections.singletonList(new DecisionOutcome("id", "out", DMNDecisionResult.DecisionEvaluationStatus.SUCCEEDED.toString(), new UnitValue("nameOut", "number", JsonNodeFactory.instance.numberNode(10)), List.of(new NamedTypedValue("nameOut", new UnitValue("number", "number", JsonNodeFactory.instance.numberNode(10)))), new ArrayList<>()));
Decision decision = new Decision("executionId", "source", "serviceUrl", 0L, true, "executor", "model", "namespace", inputs, outcomes);
DecisionMarshaller marshaller = new DecisionMarshaller(new ObjectMapper());
marshaller.writeTo(writer, decision);
Decision retrieved = marshaller.readFrom(reader);
Assertions.assertEquals(decision.getExecutionId(), retrieved.getExecutionId());
Assertions.assertEquals(decision.getSourceUrl(), retrieved.getSourceUrl());
Assertions.assertEquals(decision.getServiceUrl(), retrieved.getServiceUrl());
Assertions.assertEquals(decision.getExecutedModelName(), retrieved.getExecutedModelName());
Assertions.assertEquals(inputs.get(0).getName(), retrieved.getInputs().stream().findFirst().get().getName());
Assertions.assertEquals(inputs.get(0).getValue().getType(), retrieved.getInputs().stream().findFirst().get().getValue().getType());
Assertions.assertEquals(inputs.get(0).getValue().toUnit().getBaseType(), retrieved.getInputs().stream().findFirst().get().getValue().toUnit().getBaseType());
Assertions.assertEquals(outcomes.get(0).getOutcomeId(), retrieved.getOutcomes().stream().findFirst().get().getOutcomeId());
Assertions.assertTrue(retrieved.getOutcomes().stream().findFirst().get().getMessages().isEmpty());
}
use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.
the class TrustyServiceTest method doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest.
@SuppressWarnings("unchecked")
void doGivenStoredExecutionWhenCounterfactualRequestIsMadeThenExplainabilityEventIsEmittedTest(CounterfactualDomain domain) {
Storage<String, Decision> decisionStorage = mock(Storage.class);
Storage<String, CounterfactualExplainabilityRequest> counterfactualStorage = mock(Storage.class);
ArgumentCaptor<BaseExplainabilityRequest> explainabilityEventArgumentCaptor = ArgumentCaptor.forClass(BaseExplainabilityRequest.class);
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(TrustyServiceTestUtils.buildCorrectDecision(TEST_EXECUTION_ID));
// The Goals structures must be comparable to the original decisions outcomes.
// The Search Domain structures must be identical those of the original decision inputs.
trustyService.requestCounterfactuals(TEST_EXECUTION_ID, List.of(new NamedTypedValue("Fine", new StructureValue("tFine", Map.of("Amount", new UnitValue("number", "number", new IntNode(0)), "Points", new UnitValue("number", "number", new IntNode(0))))), new NamedTypedValue("Should the driver be suspended?", new UnitValue("string", "string", new TextNode("No")))), List.of(new CounterfactualSearchDomain("Violation", new CounterfactualSearchDomainStructureValue("tViolation", Map.of("Type", new CounterfactualSearchDomainUnitValue("string", "string", true, domain), "Actual Speed", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Speed Limit", new CounterfactualSearchDomainUnitValue("number", "number", true, domain)))), new CounterfactualSearchDomain("Driver", new CounterfactualSearchDomainStructureValue("tDriver", Map.of("Age", new CounterfactualSearchDomainUnitValue("number", "number", true, domain), "Points", new CounterfactualSearchDomainUnitValue("number", "number", true, domain))))));
verify(explainabilityRequestProducerMock).sendEvent(explainabilityEventArgumentCaptor.capture());
BaseExplainabilityRequest event = explainabilityEventArgumentCaptor.getValue();
assertNotNull(event);
assertTrue(event instanceof CounterfactualExplainabilityRequest);
CounterfactualExplainabilityRequest request = (CounterfactualExplainabilityRequest) event;
assertEquals(TEST_EXECUTION_ID, request.getExecutionId());
}
use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenADecisionWhenADecisionIsStoredAndRetrievedByIdThenTheOriginalObjectIsReturned.
@Test
@SuppressWarnings("unchecked")
void givenADecisionWhenADecisionIsStoredAndRetrievedByIdThenTheOriginalObjectIsReturned() {
Decision decision = new Decision();
decision.setExecutionId(TEST_EXECUTION_ID);
@SuppressWarnings("unchecked") Storage storageMock = new StorageImplMock(Decision.class);
when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
trustyService.storeDecision(TEST_EXECUTION_ID, decision);
Decision result = trustyService.getDecisionById(TEST_EXECUTION_ID);
assertEquals(TEST_EXECUTION_ID, result.getExecutionId());
}
use of org.kie.kogito.trusty.storage.api.model.decision.Decision in project kogito-apps by kiegroup.
the class TrustyServiceTest method givenManyExecutionsThenPaginationWorksProperly.
@Test
@SuppressWarnings("unchecked")
void givenManyExecutionsThenPaginationWorksProperly() {
List<Decision> decisions = new ArrayList<>();
IntStream.range(0, 10).forEach(x -> {
Decision d = new Decision();
d.setExecutionId(String.valueOf(x));
decisions.add(d);
});
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(decisions);
Storage storageMock = mock(Storage.class);
decisions.forEach(x -> {
when(storageMock.put(eq(x.getExecutionId()), any(Object.class))).thenReturn(x);
when(storageMock.containsKey(eq(x.getExecutionId()))).thenReturn(false);
});
when(storageMock.query()).thenReturn(queryMock);
when(trustyStorageServiceMock.getDecisionsStorage()).thenReturn(storageMock);
decisions.forEach(x -> trustyService.storeDecision(x.getExecutionId(), x));
MatchedExecutionHeaders result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 3, 5, "");
assertEquals(3, result.getExecutions().size());
assertEquals(decisions.size(), result.getAvailableResults());
result = trustyService.getExecutionHeaders(OffsetDateTime.now().minusDays(1), OffsetDateTime.now(), 100, 5, "");
assertEquals(5, result.getExecutions().size());
assertEquals(decisions.size(), result.getAvailableResults());
}
Aggregations