From 1149ae6434d88a60eaeb892a9332ce7525cea64e Mon Sep 17 00:00:00 2001 From: Enrique Gonzalez Martinez Date: Mon, 9 Oct 2023 20:16:54 +0200 Subject: [PATCH] fix mongodb debezium --- .../events/mongodb/MongoDBEventPublisher.java | 20 +-- .../codec/EventMongoDBCodecProvider.java | 13 +- .../codec/NodeInstanceDataEventCodec.java | 99 ----------- .../codec/ProcessInstanceDataEventCodec.java | 68 +++----- .../codec/UserTaskInstanceDataEventCodec.java | 59 +++---- .../codec/VariableInstanceDataEventCodec.java | 84 --------- .../codec/EventMongoDBCodecProviderTest.java | 10 +- .../codec/NodeInstanceDataEventCodecTest.java | 159 ------------------ .../ProcessInstanceDataEventCodecTest.java | 53 +++--- .../UserTaskInstanceDataEventCodecTest.java | 47 +++--- .../VariableInstanceDataEventCodecTest.java | 145 ---------------- 11 files changed, 124 insertions(+), 633 deletions(-) delete mode 100644 addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodec.java delete mode 100644 addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodec.java delete mode 100644 addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodecTest.java delete mode 100644 addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodecTest.java diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/MongoDBEventPublisher.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/MongoDBEventPublisher.java index 8aa0421b51c..5ee97577427 100644 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/MongoDBEventPublisher.java +++ b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/MongoDBEventPublisher.java @@ -25,8 +25,8 @@ import org.bson.codecs.pojo.PojoCodecProvider; import org.kie.kogito.event.DataEvent; import org.kie.kogito.event.EventPublisher; -import org.kie.kogito.event.process.ProcessInstanceStateDataEvent; -import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent; +import org.kie.kogito.event.process.ProcessInstanceDataEvent; +import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent; import org.kie.kogito.events.mongodb.codec.EventMongoDBCodecProvider; import org.kie.kogito.mongodb.transaction.AbstractTransactionManager; import org.slf4j.Logger; @@ -46,8 +46,8 @@ public abstract class MongoDBEventPublisher implements EventPublisher { static final String ID = "_id"; - private MongoCollection processInstanceDataEventCollection; - private MongoCollection userTaskInstanceDataEventCollection; + private MongoCollection processInstanceDataEventCollection; + private MongoCollection userTaskInstanceDataEventCollection; protected abstract MongoClient mongoClient(); @@ -67,19 +67,19 @@ protected void configure() { CodecRegistry registry = CodecRegistries.fromRegistries(MongoClientSettings.getDefaultCodecRegistry(), fromProviders(new EventMongoDBCodecProvider(), PojoCodecProvider.builder().automatic(true).build())); MongoDatabase mongoDatabase = mongoClient().getDatabase(eventsDatabaseName()).withCodecRegistry(registry); - processInstanceDataEventCollection = mongoDatabase.getCollection(processInstancesEventsCollection(), ProcessInstanceStateDataEvent.class).withCodecRegistry(registry); - userTaskInstanceDataEventCollection = mongoDatabase.getCollection(userTasksEventsCollection(), UserTaskInstanceStateDataEvent.class).withCodecRegistry(registry); + processInstanceDataEventCollection = mongoDatabase.getCollection(processInstancesEventsCollection(), ProcessInstanceDataEvent.class).withCodecRegistry(registry); + userTaskInstanceDataEventCollection = mongoDatabase.getCollection(userTasksEventsCollection(), UserTaskInstanceDataEvent.class).withCodecRegistry(registry); } @Override public void publish(DataEvent event) { - if (this.processInstancesEvents() && event instanceof ProcessInstanceStateDataEvent) { - publishEvent(processInstanceDataEventCollection, (ProcessInstanceStateDataEvent) event); + if (this.processInstancesEvents() && event instanceof ProcessInstanceDataEvent) { + publishEvent(processInstanceDataEventCollection, (ProcessInstanceDataEvent) event); return; } - if (this.userTasksEvents() && event instanceof UserTaskInstanceStateDataEvent) { - publishEvent(userTaskInstanceDataEventCollection, (UserTaskInstanceStateDataEvent) event); + if (this.userTasksEvents() && event instanceof UserTaskInstanceDataEvent) { + publishEvent(userTaskInstanceDataEventCollection, (UserTaskInstanceDataEvent) event); return; } diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProvider.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProvider.java index 4f13287e787..778b1722a16 100644 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProvider.java +++ b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProvider.java @@ -21,28 +21,23 @@ import org.bson.codecs.Codec; import org.bson.codecs.configuration.CodecProvider; import org.bson.codecs.configuration.CodecRegistry; -import org.kie.kogito.event.process.ProcessInstanceStateDataEvent; -import org.kie.kogito.event.process.ProcessInstanceVariableDataEvent; -import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent; +import org.kie.kogito.event.process.ProcessInstanceDataEvent; +import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent; public class EventMongoDBCodecProvider implements CodecProvider { private static final ProcessInstanceDataEventCodec PROCESS_INSTANCE_DATA_EVENT_CODEC = new ProcessInstanceDataEventCodec(); private static final UserTaskInstanceDataEventCodec USER_TASK_INSTANCE_DATA_EVENT_CODEC = new UserTaskInstanceDataEventCodec(); - private static final VariableInstanceDataEventCodec VARIABLE_INSTANCE_DATA_EVENT_CODEC = new VariableInstanceDataEventCodec(); @SuppressWarnings("unchecked") @Override public Codec get(Class aClass, CodecRegistry codecRegistry) { - if (aClass == ProcessInstanceStateDataEvent.class) { + if (aClass.isAssignableFrom(ProcessInstanceDataEvent.class)) { return (Codec) PROCESS_INSTANCE_DATA_EVENT_CODEC; } - if (aClass == UserTaskInstanceStateDataEvent.class) { + if (aClass.isAssignableFrom(UserTaskInstanceDataEvent.class)) { return (Codec) USER_TASK_INSTANCE_DATA_EVENT_CODEC; } - if (aClass == ProcessInstanceVariableDataEvent.class) { - return (Codec) VARIABLE_INSTANCE_DATA_EVENT_CODEC; - } return null; } } diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodec.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodec.java deleted file mode 100644 index 409aec80a62..00000000000 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodec.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.kie.kogito.events.mongodb.codec; - -import org.bson.BsonReader; -import org.bson.BsonString; -import org.bson.BsonValue; -import org.bson.BsonWriter; -import org.bson.Document; -import org.bson.codecs.CollectibleCodec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; -import org.kie.kogito.event.process.ProcessInstanceNodeDataEvent; -import org.kie.kogito.event.process.ProcessInstanceNodeEventBody; - -import static org.kie.kogito.events.mongodb.codec.CodecUtils.codec; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.encodeDataEvent; - -public class NodeInstanceDataEventCodec implements CollectibleCodec { - - @Override - public ProcessInstanceNodeDataEvent generateIdIfAbsentFromDocument(ProcessInstanceNodeDataEvent nodeInstanceDataEvent) { - return nodeInstanceDataEvent; - } - - @Override - public boolean documentHasId(ProcessInstanceNodeDataEvent processInstanceDataEvent) { - return processInstanceDataEvent.getId() != null; - } - - @Override - public BsonValue getDocumentId(ProcessInstanceNodeDataEvent processInstanceDataEvent) { - return new BsonString(processInstanceDataEvent.getId()); - } - - @Override - public ProcessInstanceNodeDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { - // The events persist in an outbox collection - // The events are deleted immediately (in the same transaction) - // "decode" is not supposed to take place in any scenario - return null; - } - - @Override - public void encode(BsonWriter bsonWriter, ProcessInstanceNodeDataEvent nodeInstanceDataEvent, EncoderContext encoderContext) { - Document doc = new Document(); - encodeDataEvent(doc, nodeInstanceDataEvent); - doc.put("kogitoProcessType", nodeInstanceDataEvent.getKogitoProcessType()); - doc.put("kogitoProcessInstanceVersion", nodeInstanceDataEvent.getKogitoProcessInstanceVersion()); - doc.put("kogitoParentProcessinstanceId", nodeInstanceDataEvent.getKogitoParentProcessInstanceId()); - doc.put("kogitoProcessinstanceState", nodeInstanceDataEvent.getKogitoProcessInstanceState()); - doc.put("kogitoReferenceId", nodeInstanceDataEvent.getKogitoReferenceId()); - doc.put("kogitoStartFromNode", nodeInstanceDataEvent.getKogitoStartFromNode()); - doc.put("data", encodeData(nodeInstanceDataEvent.getData())); - codec().encode(bsonWriter, doc, encoderContext); - } - - private Document encodeData(ProcessInstanceNodeEventBody data) { - - Document doc = new Document(); - doc.put("processInstanceId", data.getProcessInstanceId()); - doc.put("connectionNodeInstanceId", data.getConnectionNodeInstanceId()); - doc.put("id", data.getNodeInstanceId()); - doc.put("nodeId", data.getNodeDefinitionId()); - doc.put("nodeDefinitionId", data.getNodeDefinitionId()); - doc.put("nodeName", data.getNodeName()); - doc.put("nodeType", data.getNodeType()); - doc.put("eventTime", data.getEventDate()); - doc.put("eventType", data.getEventType()); - - if (!data.getData().isEmpty()) { - doc.put("data", new Document(data.getData())); - } - - return doc; - } - - @Override - public Class getEncoderClass() { - return ProcessInstanceNodeDataEvent.class; - } -} diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodec.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodec.java index bb461fde217..8fbf46a1b88 100644 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodec.java +++ b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodec.java @@ -26,31 +26,37 @@ import org.bson.codecs.CollectibleCodec; import org.bson.codecs.DecoderContext; import org.bson.codecs.EncoderContext; -import org.kie.kogito.event.process.ProcessInstanceStateDataEvent; -import org.kie.kogito.event.process.ProcessInstanceStateEventBody; +import org.kie.kogito.event.process.ProcessInstanceDataEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import static org.kie.kogito.events.mongodb.codec.CodecUtils.codec; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.encodeDataEvent; -public class ProcessInstanceDataEventCodec implements CollectibleCodec { +public class ProcessInstanceDataEventCodec implements CollectibleCodec { + + private static final Logger LOGGER = LoggerFactory.getLogger(ProcessInstanceDataEventCodec.class); @Override - public ProcessInstanceStateDataEvent generateIdIfAbsentFromDocument(ProcessInstanceStateDataEvent processInstanceDataEvent) { + public ProcessInstanceDataEvent generateIdIfAbsentFromDocument(ProcessInstanceDataEvent processInstanceDataEvent) { return processInstanceDataEvent; } @Override - public boolean documentHasId(ProcessInstanceStateDataEvent processInstanceDataEvent) { + public boolean documentHasId(ProcessInstanceDataEvent processInstanceDataEvent) { return processInstanceDataEvent.getId() != null; } @Override - public BsonValue getDocumentId(ProcessInstanceStateDataEvent processInstanceDataEvent) { + public BsonValue getDocumentId(ProcessInstanceDataEvent processInstanceDataEvent) { return new BsonString(processInstanceDataEvent.getId()); } @Override - public ProcessInstanceStateDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { + public ProcessInstanceDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { // The events persist in an outbox collection // The events are deleted immediately (in the same transaction) // "decode" is not supposed to take place in any scenario @@ -58,41 +64,21 @@ public ProcessInstanceStateDataEvent decode(BsonReader bsonReader, DecoderContex } @Override - public void encode(BsonWriter bsonWriter, ProcessInstanceStateDataEvent processInstanceDataEvent, EncoderContext encoderContext) { - Document doc = new Document(); - encodeDataEvent(doc, processInstanceDataEvent); - doc.put("kogitoProcessType", processInstanceDataEvent.getKogitoProcessType()); - doc.put("kogitoProcessInstanceVersion", processInstanceDataEvent.getKogitoProcessInstanceVersion()); - doc.put("kogitoParentProcessinstanceId", processInstanceDataEvent.getKogitoParentProcessInstanceId()); - doc.put("kogitoProcessinstanceState", processInstanceDataEvent.getKogitoProcessInstanceState()); - doc.put("kogitoReferenceId", processInstanceDataEvent.getKogitoReferenceId()); - doc.put("kogitoStartFromNode", processInstanceDataEvent.getKogitoStartFromNode()); - doc.put("kogitoIdentity", processInstanceDataEvent.getKogitoIdentity()); - doc.put("data", encodeData(processInstanceDataEvent.getData())); - codec().encode(bsonWriter, doc, encoderContext); - } - - private Document encodeData(ProcessInstanceStateEventBody data) { - Document doc = new Document(); - doc.put("id", data.getProcessInstanceId()); - doc.put("version", data.getProcessVersion()); - doc.put("parentInstanceId", data.getParentInstanceId()); - doc.put("rootInstanceId", data.getRootProcessInstanceId()); - doc.put("processId", data.getProcessId()); - doc.put("processType", data.getProcessType()); - doc.put("rootProcessId", data.getRootProcessId()); - doc.put("processName", data.getProcessName()); - doc.put("eventDate", data.getEventDate()); - doc.put("state", data.getState()); - doc.put("businessKey", data.getBusinessKey()); - doc.put("roles", data.getRoles()); - doc.put("identity", data.getEventUser()); - - return doc; + public void encode(BsonWriter bsonWriter, ProcessInstanceDataEvent processInstanceDataEvent, EncoderContext encoderContext) { + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.findAndRegisterModules(); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + Document document = Document.parse(mapper.writeValueAsString(processInstanceDataEvent)); + document.put(CodecUtils.ID, processInstanceDataEvent.getId()); + codec().encode(bsonWriter, document, encoderContext); + } catch (JsonProcessingException e) { + LOGGER.error("Could not process json event", e); + } } @Override - public Class getEncoderClass() { - return ProcessInstanceStateDataEvent.class; + public Class getEncoderClass() { + return ProcessInstanceDataEvent.class; } } diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodec.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodec.java index 26930feabab..24087cf4397 100644 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodec.java +++ b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodec.java @@ -26,31 +26,37 @@ import org.bson.codecs.CollectibleCodec; import org.bson.codecs.DecoderContext; import org.bson.codecs.EncoderContext; -import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent; -import org.kie.kogito.event.usertask.UserTaskInstanceStateEventBody; +import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; import static org.kie.kogito.events.mongodb.codec.CodecUtils.codec; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.encodeDataEvent; -public class UserTaskInstanceDataEventCodec implements CollectibleCodec { +public class UserTaskInstanceDataEventCodec implements CollectibleCodec { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserTaskInstanceDataEventCodec.class); @Override - public UserTaskInstanceStateDataEvent generateIdIfAbsentFromDocument(UserTaskInstanceStateDataEvent userTaskInstanceDataEvent) { + public UserTaskInstanceDataEvent generateIdIfAbsentFromDocument(UserTaskInstanceDataEvent userTaskInstanceDataEvent) { return userTaskInstanceDataEvent; } @Override - public boolean documentHasId(UserTaskInstanceStateDataEvent userTaskInstanceDataEvent) { + public boolean documentHasId(UserTaskInstanceDataEvent userTaskInstanceDataEvent) { return userTaskInstanceDataEvent.getId() != null; } @Override - public BsonValue getDocumentId(UserTaskInstanceStateDataEvent userTaskInstanceDataEvent) { + public BsonValue getDocumentId(UserTaskInstanceDataEvent userTaskInstanceDataEvent) { return new BsonString(userTaskInstanceDataEvent.getId()); } @Override - public UserTaskInstanceStateDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { + public UserTaskInstanceDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { // The events persist in an outbox collection // The events are deleted immediately (in the same transaction) // "decode" is not supposed to take place in any scenario @@ -58,32 +64,21 @@ public UserTaskInstanceStateDataEvent decode(BsonReader bsonReader, DecoderConte } @Override - public void encode(BsonWriter bsonWriter, UserTaskInstanceStateDataEvent userTaskInstanceDataEvent, EncoderContext encoderContext) { - Document doc = new Document(); - encodeDataEvent(doc, userTaskInstanceDataEvent); - doc.put("kogitoUserTaskinstanceId", userTaskInstanceDataEvent.getKogitoUserTaskInstanceId()); - doc.put("kogitoUserTaskinstanceState", userTaskInstanceDataEvent.getKogitoUserTaskInstanceState()); - doc.put("data", encodeData(userTaskInstanceDataEvent.getData())); - codec().encode(bsonWriter, doc, encoderContext); - } - - private Document encodeData(UserTaskInstanceStateEventBody data) { - Document doc = new Document(); - doc.put("id", data.getUserTaskInstanceId()); - doc.put("taskName", data.getUserTaskName()); - doc.put("taskDescription", data.getUserTaskDescription()); - doc.put("taskPriority", data.getUserTaskPriority()); - doc.put("referenceName", data.getUserTaskReferenceName()); - doc.put("eventDate", data.getEventDate()); - doc.put("state", data.getState()); - doc.put("actualOwner", data.getActualOwner()); - doc.put("processInstanceId", data.getProcessInstanceId()); - doc.put("identity", data.getEventUser()); - return doc; + public void encode(BsonWriter bsonWriter, UserTaskInstanceDataEvent userTaskInstanceDataEvent, EncoderContext encoderContext) { + try { + ObjectMapper mapper = new ObjectMapper(); + mapper.findAndRegisterModules(); + mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); + Document document = Document.parse(mapper.writeValueAsString(userTaskInstanceDataEvent)); + document.put(CodecUtils.ID, userTaskInstanceDataEvent.getId()); + codec().encode(bsonWriter, document, encoderContext); + } catch (JsonProcessingException e) { + LOGGER.error("Could not process json event", e); + } } @Override - public Class getEncoderClass() { - return UserTaskInstanceStateDataEvent.class; + public Class getEncoderClass() { + return UserTaskInstanceDataEvent.class; } } diff --git a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodec.java b/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodec.java deleted file mode 100644 index 622ab424c73..00000000000 --- a/addons/common/events/mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodec.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.kie.kogito.events.mongodb.codec; - -import org.bson.BsonReader; -import org.bson.BsonString; -import org.bson.BsonValue; -import org.bson.BsonWriter; -import org.bson.Document; -import org.bson.codecs.CollectibleCodec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; -import org.kie.kogito.event.process.ProcessInstanceVariableDataEvent; -import org.kie.kogito.event.process.ProcessInstanceVariableEventBody; - -import static org.kie.kogito.events.mongodb.codec.CodecUtils.codec; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.encodeDataEvent; - -public class VariableInstanceDataEventCodec implements CollectibleCodec { - - @Override - public ProcessInstanceVariableDataEvent generateIdIfAbsentFromDocument(ProcessInstanceVariableDataEvent variableInstanceDataEvent) { - return variableInstanceDataEvent; - } - - @Override - public boolean documentHasId(ProcessInstanceVariableDataEvent variableInstanceDataEvent) { - return variableInstanceDataEvent.getId() != null; - } - - @Override - public BsonValue getDocumentId(ProcessInstanceVariableDataEvent variableInstanceDataEvent) { - return new BsonString(variableInstanceDataEvent.getId()); - } - - @Override - public ProcessInstanceVariableDataEvent decode(BsonReader bsonReader, DecoderContext decoderContext) { - // The events persist in an outbox collection - // The events are deleted immediately (in the same transaction) - // "decode" is not supposed to take place in any scenario - return null; - } - - @Override - public void encode(BsonWriter bsonWriter, ProcessInstanceVariableDataEvent variableInstanceDataEvent, EncoderContext encoderContext) { - Document doc = new Document(); - encodeDataEvent(doc, variableInstanceDataEvent); - doc.put("kogitoVariableName", variableInstanceDataEvent.getKogitoVariableName()); - doc.put("data", encodeData(variableInstanceDataEvent.getData())); - codec().encode(bsonWriter, doc, encoderContext); - } - - private Document encodeData(ProcessInstanceVariableEventBody data) { - Document doc = new Document(); - doc.put("variableName", data.getVariableName()); - doc.put("variableValue", data.getVariableValue()); - doc.put("eventDate", data.getEventDate()); - doc.put("processInstanceId", data.getProcessInstanceId()); - doc.put("processId", data.getProcessId()); - doc.put("identity", data.getEventUser()); - return doc; - } - - @Override - public Class getEncoderClass() { - return ProcessInstanceVariableDataEvent.class; - } -} diff --git a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProviderTest.java b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProviderTest.java index d2025823a6d..55229a1a8a8 100644 --- a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProviderTest.java +++ b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/EventMongoDBCodecProviderTest.java @@ -19,9 +19,8 @@ package org.kie.kogito.events.mongodb.codec; import org.junit.jupiter.api.Test; -import org.kie.kogito.event.process.ProcessInstanceStateDataEvent; -import org.kie.kogito.event.process.ProcessInstanceVariableDataEvent; -import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent; +import org.kie.kogito.event.process.ProcessInstanceDataEvent; +import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent; import static org.assertj.core.api.Assertions.assertThat; @@ -31,9 +30,8 @@ class EventMongoDBCodecProviderTest { void get() { EventMongoDBCodecProvider provider = new EventMongoDBCodecProvider(); - assertThat(provider.get(ProcessInstanceStateDataEvent.class, null).getClass()).isEqualTo(ProcessInstanceDataEventCodec.class); - assertThat(provider.get(UserTaskInstanceStateDataEvent.class, null).getClass()).isEqualTo(UserTaskInstanceDataEventCodec.class); - assertThat(provider.get(ProcessInstanceVariableDataEvent.class, null).getClass()).isEqualTo(VariableInstanceDataEventCodec.class); + assertThat(provider.get(ProcessInstanceDataEvent.class, null).getClass()).isEqualTo(ProcessInstanceDataEventCodec.class); + assertThat(provider.get(UserTaskInstanceDataEvent.class, null).getClass()).isEqualTo(UserTaskInstanceDataEventCodec.class); assertThat(provider.get(String.class, null)).isNull(); } } \ No newline at end of file diff --git a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodecTest.java b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodecTest.java deleted file mode 100644 index 849aeba7584..00000000000 --- a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodecTest.java +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.kie.kogito.events.mongodb.codec; - -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.bson.BsonReader; -import org.bson.BsonString; -import org.bson.BsonWriter; -import org.bson.Document; -import org.bson.codecs.Codec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.kie.kogito.event.process.ProcessInstanceEventMetadata; -import org.kie.kogito.event.process.ProcessInstanceNodeDataEvent; -import org.kie.kogito.event.process.ProcessInstanceNodeEventBody; -import org.mockito.ArgumentCaptor; -import org.mockito.MockedStatic; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.ID; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -class NodeInstanceDataEventCodecTest { - - private NodeInstanceDataEventCodec codec; - - private ProcessInstanceNodeDataEvent event; - - @BeforeEach - void setUp() { - codec = new NodeInstanceDataEventCodec(); - - String source = "testSource"; - String kogitoAddons = "testKogitoAddons"; - - Map metaData = new HashMap<>(); - metaData.put(ProcessInstanceEventMetadata.PROCESS_INSTANCE_ID_META_DATA, "testKogitoProcessInstanceId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_VERSION_META_DATA, "testKogitoProcessInstanceVersion"); - metaData.put(ProcessInstanceEventMetadata.ROOT_PROCESS_INSTANCE_ID_META_DATA, "testKogitoRootProcessInstanceId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_ID_META_DATA, "testKogitoProcessId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_TYPE_META_DATA, "testKogitoProcessType"); - metaData.put(ProcessInstanceEventMetadata.ROOT_PROCESS_ID_META_DATA, "testKogitoRootProcessId"); - metaData.put(ProcessInstanceEventMetadata.PARENT_PROCESS_INSTANCE_ID_META_DATA, "testKogitoParentProcessInstanceId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_INSTANCE_STATE_META_DATA, "testKogitoProcessInstanceState"); - - ProcessInstanceNodeEventBody body = ProcessInstanceNodeEventBody.create() - .nodeInstanceId("testId") - .processInstanceId("testProcessInstanceId") - .connectionNodeInstanceId("connectionNodeInstanceId") - .eventDate(new Date()) - .eventType(1) - .data("test", 2) - .nodeDefinitionId("testNodeDefinitionId") - .nodeName("testNodeName") - .nodeType("testNodeType") - .build(); - - event = new ProcessInstanceNodeDataEvent(source, kogitoAddons, "identity", metaData, body); - } - - @Test - void generateIdIfAbsentFromDocument() { - assertThat(codec.generateIdIfAbsentFromDocument(event)).isEqualTo(event); - } - - @Test - void documentHasId() { - assertThat(codec.documentHasId(event)).isTrue(); - } - - @Test - void getDocumentId() { - assertThat(codec.getDocumentId(event)).isEqualTo(new BsonString(event.getId())); - } - - @Test - void decode() { - assertThat(codec.decode(mock(BsonReader.class), DecoderContext.builder().build())).isNull(); - } - - @Test - void encode() { - try (MockedStatic codecUtils = mockStatic(CodecUtils.class)) { - Codec mockCodec = mock(Codec.class); - codecUtils.when(CodecUtils::codec).thenReturn(mockCodec); - codecUtils.when(() -> CodecUtils.encodeDataEvent(any(), any())).thenCallRealMethod(); - BsonWriter writer = mock(BsonWriter.class); - EncoderContext context = EncoderContext.builder().build(); - - codec.encode(writer, event, context); - - ArgumentCaptor captor = ArgumentCaptor.forClass(Document.class); - verify(mockCodec, times(1)).encode(eq(writer), captor.capture(), eq(context)); - Document doc = captor.getValue(); - - assertThat(doc).containsEntry(ID, event.getId()) - .containsEntry("specversion", event.getSpecVersion().toString()) - .containsEntry("source", event.getSource().toString()) - .containsEntry("type", event.getType()) - .containsEntry("time", event.getTime()) - .containsEntry("subject", event.getSubject()) - .containsEntry("dataContentType", event.getDataContentType()) - .containsEntry("dataSchema", event.getDataSchema()) - .containsEntry("kogitoProcessinstanceId", event.getKogitoProcessInstanceId()) - .containsEntry("kogitoProcessInstanceVersion", event.getKogitoProcessInstanceVersion()) - .containsEntry("kogitoRootProcessinstanceId", event.getKogitoRootProcessInstanceId()) - .containsEntry("kogitoProcessId", event.getKogitoProcessId()) - .containsEntry("kogitoProcessType", event.getKogitoProcessType()) - .containsEntry("kogitoRootProcessId", event.getKogitoRootProcessId()) - .containsEntry("kogitoAddons", event.getKogitoAddons()) - .containsEntry("kogitoParentProcessinstanceId", event.getKogitoParentProcessInstanceId()) - .containsEntry("kogitoProcessinstanceState", event.getKogitoProcessInstanceState()) - .containsEntry("kogitoReferenceId", event.getKogitoReferenceId()) - .containsEntry("kogitoStartFromNode", event.getKogitoStartFromNode()); - - assertThat(((Document) doc.get("data"))) - .containsEntry("id", event.getData().getNodeInstanceId()) - .containsEntry("processInstanceId", event.getData().getProcessInstanceId()) - .containsEntry("connectionNodeInstanceId", event.getData().getConnectionNodeInstanceId()) - .containsEntry("nodeDefinitionId", event.getData().getNodeDefinitionId()) - .containsEntry("nodeName", event.getData().getNodeName()) - .containsEntry("nodeType", event.getData().getNodeType()) - .containsEntry("eventTime", event.getData().getEventDate()) - .containsEntry("eventType", event.getData().getEventType()); - } - } - - @Test - void getEncoderClass() { - assertThat(codec.getEncoderClass()).isEqualTo(ProcessInstanceNodeDataEvent.class); - } -} diff --git a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodecTest.java b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodecTest.java index 7a4a3905a47..a7f3371526c 100644 --- a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodecTest.java +++ b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/ProcessInstanceDataEventCodecTest.java @@ -31,12 +31,16 @@ import org.bson.codecs.EncoderContext; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.kie.kogito.event.process.ProcessInstanceDataEvent; import org.kie.kogito.event.process.ProcessInstanceEventMetadata; import org.kie.kogito.event.process.ProcessInstanceStateDataEvent; import org.kie.kogito.event.process.ProcessInstanceStateEventBody; import org.mockito.ArgumentCaptor; import org.mockito.MockedStatic; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.assertj.core.api.Assertions.assertThat; import static org.kie.kogito.events.mongodb.codec.CodecUtils.ID; import static org.mockito.ArgumentMatchers.any; @@ -123,46 +127,41 @@ void encode() { ArgumentCaptor captor = ArgumentCaptor.forClass(Document.class); verify(mockCodec, times(1)).encode(eq(writer), captor.capture(), eq(context)); Document doc = captor.getValue(); + Map node = new ObjectMapper().readValue(doc.toJson(), Map.class); - assertThat(doc).containsEntry(ID, event.getId()) + assertThat(node).containsEntry(ID, event.getId()) .containsEntry("specversion", event.getSpecVersion().toString()) .containsEntry("source", event.getSource().toString()) .containsEntry("type", event.getType()) - .containsEntry("time", event.getTime()) - .containsEntry("subject", event.getSubject()) - .containsEntry("dataContentType", event.getDataContentType()) - .containsEntry("dataSchema", event.getDataSchema()) - .containsEntry("kogitoProcessinstanceId", event.getKogitoProcessInstanceId()) - .containsEntry("kogitoProcessInstanceVersion", event.getKogitoProcessInstanceVersion()) - .containsEntry("kogitoRootProcessinstanceId", event.getKogitoRootProcessInstanceId()) - .containsEntry("kogitoProcessId", event.getKogitoProcessId()) - .containsEntry("kogitoProcessType", event.getKogitoProcessType()) - .containsEntry("kogitoRootProcessId", event.getKogitoRootProcessId()) - .containsEntry("kogitoAddons", event.getKogitoAddons()) - .containsEntry("kogitoParentProcessinstanceId", event.getKogitoParentProcessInstanceId()) - .containsEntry("kogitoProcessinstanceState", event.getKogitoProcessInstanceState()) - .containsEntry("kogitoReferenceId", event.getKogitoReferenceId()) - .containsEntry("kogitoIdentity", event.getKogitoIdentity()) - .containsEntry("kogitoStartFromNode", event.getKogitoStartFromNode()); - - assertThat(((Document) doc.get("data"))).containsEntry("id", event.getData().getProcessInstanceId()) - .containsEntry("version", event.getData().getProcessVersion()) - .containsEntry("parentInstanceId", event.getData().getParentInstanceId()) - .containsEntry("rootInstanceId", event.getData().getRootProcessInstanceId()) + .containsEntry("datacontenttype", event.getDataContentType()) + .containsEntry("kogitoprocinstanceid", event.getKogitoProcessInstanceId()) + .containsEntry("kogitoprocversion", event.getKogitoProcessInstanceVersion()) + .containsEntry("kogitorootprociid", event.getKogitoRootProcessInstanceId()) + .containsEntry("kogitoprocid", event.getKogitoProcessId()) + .containsEntry("kogitoproctype", event.getKogitoProcessType()) + .containsEntry("kogitorootprocid", event.getKogitoRootProcessId()) + .containsEntry("kogitoaddons", event.getKogitoAddons()) + .containsEntry("kogitoparentprociid", event.getKogitoParentProcessInstanceId()) + .containsEntry("kogitoprocist", event.getKogitoProcessInstanceState()) + .containsEntry("kogitoidentity", event.getKogitoIdentity()); + + assertThat(((Document) doc.get("data"))) + .containsEntry("processVersion", event.getData().getProcessVersion()) + .containsEntry("rootProcessInstanceId", event.getData().getRootProcessInstanceId()) .containsEntry("processId", event.getData().getProcessId()) .containsEntry("rootProcessId", event.getData().getRootProcessId()) .containsEntry("processName", event.getData().getProcessName()) - .containsEntry("identity", event.getData().getEventUser()) - .containsEntry("eventDate", event.getData().getEventDate()) + .containsEntry("eventUser", event.getData().getEventUser()) .containsEntry("state", event.getData().getState()) - .containsEntry("businessKey", event.getData().getBusinessKey()) - .containsEntry("roles", event.getData().getRoles()); + .containsEntry("businessKey", event.getData().getBusinessKey()); + } catch (JsonProcessingException e) { + e.printStackTrace(); } } @Test void getEncoderClass() { - assertThat(codec.getEncoderClass()).isEqualTo(ProcessInstanceStateDataEvent.class); + assertThat(codec.getEncoderClass()).isEqualTo(ProcessInstanceDataEvent.class); } } diff --git a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodecTest.java b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodecTest.java index 4b82945414e..7aeada7f966 100644 --- a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodecTest.java +++ b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/UserTaskInstanceDataEventCodecTest.java @@ -32,12 +32,16 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.kie.kogito.event.process.ProcessInstanceEventMetadata; +import org.kie.kogito.event.usertask.UserTaskInstanceDataEvent; import org.kie.kogito.event.usertask.UserTaskInstanceEventMetadata; import org.kie.kogito.event.usertask.UserTaskInstanceStateDataEvent; import org.kie.kogito.event.usertask.UserTaskInstanceStateEventBody; import org.mockito.ArgumentCaptor; import org.mockito.MockedStatic; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + import static org.assertj.core.api.Assertions.assertThat; import static org.kie.kogito.events.mongodb.codec.CodecUtils.ID; import static org.mockito.ArgumentMatchers.any; @@ -121,39 +125,40 @@ void encode() { verify(mockCodec, times(1)).encode(eq(writer), captor.capture(), eq(context)); Document doc = captor.getValue(); + Map node = new ObjectMapper().readValue(doc.toJson(), Map.class); + assertThat(doc).containsEntry(ID, event.getId()) .containsEntry("specversion", event.getSpecVersion().toString()) .containsEntry("source", event.getSource().toString()) .containsEntry("type", event.getType()) - .containsEntry("time", event.getTime()) - .containsEntry("subject", event.getSubject()) - .containsEntry("dataContentType", event.getDataContentType()) - .containsEntry("dataSchema", event.getDataSchema()) - .containsEntry("kogitoProcessinstanceId", event.getKogitoProcessInstanceId()) - .containsEntry("kogitoRootProcessinstanceId", event.getKogitoRootProcessInstanceId()) - .containsEntry("kogitoProcessId", event.getKogitoProcessId()) - .containsEntry("kogitoRootProcessId", event.getKogitoRootProcessId()) - .containsEntry("kogitoAddons", event.getKogitoAddons()) - .containsEntry("kogitoUserTaskinstanceId", event.getKogitoUserTaskInstanceId()) - .containsEntry("kogitoUserTaskinstanceState", event.getKogitoUserTaskInstanceState()) - .containsEntry("kogitoIdentity", event.getKogitoIdentity()); - - assertThat(((Document) doc.get("data"))).containsEntry("id", event.getData().getUserTaskInstanceId()) - .containsEntry("taskName", event.getData().getUserTaskName()) - .containsEntry("taskDescription", event.getData().getUserTaskDescription()) - .containsEntry("taskPriority", event.getData().getUserTaskPriority()) - .containsEntry("referenceName", event.getData().getUserTaskReferenceName()) - .containsEntry("eventDate", event.getData().getEventDate()) + .containsEntry("datacontenttype", event.getDataContentType()) + .containsEntry("kogitoprocinstanceid", event.getKogitoProcessInstanceId()) + .containsEntry("kogitoprocversion", event.getKogitoProcessInstanceVersion()) + .containsEntry("kogitorootprociid", event.getKogitoRootProcessInstanceId()) + .containsEntry("kogitoprocid", event.getKogitoProcessId()) + .containsEntry("kogitorootprocid", event.getKogitoRootProcessId()) + .containsEntry("kogitoaddons", event.getKogitoAddons()) + .containsEntry("kogitoidentity", event.getKogitoIdentity()) + .containsEntry("kogitousertaskiid", event.getKogitoUserTaskInstanceId()) + .containsEntry("kogitousertaskist", event.getKogitoUserTaskInstanceState()); + + assertThat(((Document) doc.get("data"))) + .containsEntry("userTaskName", event.getData().getUserTaskName()) + .containsEntry("userTaskDescription", event.getData().getUserTaskDescription()) + .containsEntry("userTaskPriority", event.getData().getUserTaskPriority()) + .containsEntry("userTaskReferenceName", event.getData().getUserTaskReferenceName()) .containsEntry("state", event.getData().getState()) .containsEntry("actualOwner", event.getData().getActualOwner()) .containsEntry("processInstanceId", event.getData().getProcessInstanceId()) - .containsEntry("identity", event.getData().getEventUser()); + .containsEntry("eventUser", event.getData().getEventUser()); + } catch (JsonProcessingException e) { + e.printStackTrace(); } } @Test void getEncoderClass() { - assertThat(codec.getEncoderClass()).isEqualTo(UserTaskInstanceStateDataEvent.class); + assertThat(codec.getEncoderClass()).isEqualTo(UserTaskInstanceDataEvent.class); } } diff --git a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodecTest.java b/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodecTest.java deleted file mode 100644 index 027758d6bcc..00000000000 --- a/addons/common/events/mongodb/src/test/java/org/kie/kogito/events/mongodb/codec/VariableInstanceDataEventCodecTest.java +++ /dev/null @@ -1,145 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.kie.kogito.events.mongodb.codec; - -import java.util.Date; -import java.util.HashMap; -import java.util.Map; - -import org.bson.BsonReader; -import org.bson.BsonString; -import org.bson.BsonWriter; -import org.bson.Document; -import org.bson.codecs.Codec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.kie.kogito.event.process.ProcessInstanceEventMetadata; -import org.kie.kogito.event.process.ProcessInstanceVariableDataEvent; -import org.kie.kogito.event.process.ProcessInstanceVariableEventBody; -import org.mockito.ArgumentCaptor; -import org.mockito.MockedStatic; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.kie.kogito.events.mongodb.codec.CodecUtils.ID; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.mockStatic; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; - -class VariableInstanceDataEventCodecTest { - - private VariableInstanceDataEventCodec codec; - - private ProcessInstanceVariableDataEvent event; - - @BeforeEach - void setUp() { - codec = new VariableInstanceDataEventCodec(); - - String source = "testSource"; - String kogitoAddons = "testKogitoAddons"; - - Map metaData = new HashMap<>(); - metaData.put(ProcessInstanceEventMetadata.PROCESS_INSTANCE_ID_META_DATA, "testKogitoProcessInstanceId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_VERSION_META_DATA, "testKogitoProcessInstanceVersion"); - metaData.put(ProcessInstanceEventMetadata.ROOT_PROCESS_INSTANCE_ID_META_DATA, "testKogitoRootProcessInstanceId"); - metaData.put(ProcessInstanceEventMetadata.PROCESS_ID_META_DATA, "testKogitoProcessId"); - metaData.put(ProcessInstanceEventMetadata.ROOT_PROCESS_ID_META_DATA, "testKogitoRootProcessId"); - - ProcessInstanceVariableEventBody body = ProcessInstanceVariableEventBody.create() - .eventDate(new Date()) - //.changedByNodeName("testChangedByNodeName") - .processId("testKogitoProcessId") - .processInstanceId("testKogitoProcessInstanceId") - .variableName("testVariableName") - .variableValue("testVariableValue") - .build(); - - event = new ProcessInstanceVariableDataEvent(source, kogitoAddons, "identity", metaData, body); - } - - @Test - void generateIdIfAbsentFromDocument() { - assertThat(codec.generateIdIfAbsentFromDocument(event)).isEqualTo(event); - } - - @Test - void documentHasId() { - assertThat(codec.documentHasId(event)).isTrue(); - } - - @Test - void getDocumentId() { - assertThat(codec.getDocumentId(event)).isEqualTo(new BsonString(event.getId())); - } - - @Test - void decode() { - assertThat(codec.decode(mock(BsonReader.class), DecoderContext.builder().build())).isNull(); - } - - @Test - void encode() { - try (MockedStatic codecUtils = mockStatic(CodecUtils.class)) { - Codec mockCodec = mock(Codec.class); - codecUtils.when(CodecUtils::codec).thenReturn(mockCodec); - codecUtils.when(() -> CodecUtils.encodeDataEvent(any(), any())).thenCallRealMethod(); - BsonWriter writer = mock(BsonWriter.class); - EncoderContext context = EncoderContext.builder().build(); - - codec.encode(writer, event, context); - - ArgumentCaptor captor = ArgumentCaptor.forClass(Document.class); - verify(mockCodec, times(1)).encode(eq(writer), captor.capture(), eq(context)); - Document doc = captor.getValue(); - - assertThat(doc).containsEntry(ID, event.getId()) - .containsEntry("specversion", event.getSpecVersion().toString()) - .containsEntry("source", event.getSource().toString()) - .containsEntry("type", event.getType()) - .containsEntry("time", event.getTime()) - .containsEntry("subject", event.getSubject()) - .containsEntry("dataContentType", event.getDataContentType()) - .containsEntry("dataSchema", event.getDataSchema()) - .containsEntry("kogitoProcessinstanceId", event.getKogitoProcessInstanceId()) - .containsEntry("kogitoRootProcessinstanceId", event.getKogitoRootProcessInstanceId()) - .containsEntry("kogitoProcessId", event.getKogitoProcessId()) - .containsEntry("kogitoRootProcessId", event.getKogitoRootProcessId()) - .containsEntry("kogitoAddons", event.getKogitoAddons()) - .containsEntry("kogitoVariableName", event.getKogitoVariableName()) - .containsEntry("kogitoIdentity", event.getKogitoIdentity()); - - assertThat(((Document) doc.get("data"))).containsEntry("variableName", event.getData().getVariableName()) - .containsEntry("variableValue", event.getData().getVariableValue()) - .containsEntry("eventDate", event.getData().getEventDate()) - .containsEntry("processInstanceId", event.getData().getProcessInstanceId()) - .containsEntry("processId", event.getData().getProcessId()) - .containsEntry("identity", event.getData().getEventUser()); - } - } - - @Test - void getEncoderClass() { - assertThat(codec.getEncoderClass()).isEqualTo(ProcessInstanceVariableDataEvent.class); - } -}