-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[kie-issues-249] Data index improvements
- Loading branch information
1 parent
3d51e76
commit 42f53fd
Showing
32 changed files
with
729 additions
and
308 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...mongodb/src/main/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright 2021 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.NodeInstanceDataEvent; | ||
import org.kie.kogito.event.process.NodeInstanceEventBody; | ||
|
||
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<NodeInstanceDataEvent> { | ||
|
||
@Override | ||
public NodeInstanceDataEvent generateIdIfAbsentFromDocument(NodeInstanceDataEvent nodeInstanceDataEvent) { | ||
return nodeInstanceDataEvent; | ||
} | ||
|
||
@Override | ||
public boolean documentHasId(NodeInstanceDataEvent processInstanceDataEvent) { | ||
return processInstanceDataEvent.getId() != null; | ||
} | ||
|
||
@Override | ||
public BsonValue getDocumentId(NodeInstanceDataEvent processInstanceDataEvent) { | ||
return new BsonString(processInstanceDataEvent.getId()); | ||
} | ||
|
||
@Override | ||
public NodeInstanceDataEvent 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, NodeInstanceDataEvent 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(NodeInstanceEventBody data) { | ||
|
||
Document doc = new Document(); | ||
doc.put("processInstanceId", data.getProcessInstanceId()); | ||
doc.put("connectionNodeInstanceId", data.getConnectionNodeInstanceId()); | ||
doc.put("id", data.getId()); | ||
doc.put("nodeId", data.getNodeId()); | ||
doc.put("nodeDefinitionId", data.getNodeDefinitionId()); | ||
doc.put("nodeName", data.getNodeName()); | ||
doc.put("nodeType", data.getNodeType()); | ||
doc.put("eventTime", data.getEventTime()); | ||
doc.put("eventType", data.getEventType()); | ||
|
||
doc.put("exitType", data.getExitType()); | ||
|
||
if (!data.getData().isEmpty()) { | ||
doc.put("data", new Document(data.getData())); | ||
} | ||
|
||
return doc; | ||
} | ||
|
||
@Override | ||
public Class<NodeInstanceDataEvent> getEncoderClass() { | ||
return NodeInstanceDataEvent.class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...odb/src/test/java/org/kie/kogito/events/mongodb/codec/NodeInstanceDataEventCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
/* | ||
* Copyright 2021 Red Hat, Inc. and/or its affiliates. | ||
* | ||
* Licensed 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.NodeInstanceDataEvent; | ||
import org.kie.kogito.event.process.NodeInstanceEventBody; | ||
import org.kie.kogito.event.process.ProcessInstanceEventBody; | ||
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 NodeInstanceDataEvent event; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
codec = new NodeInstanceDataEventCodec(); | ||
|
||
String source = "testSource"; | ||
String kogitoAddons = "testKogitoAddons"; | ||
|
||
Map<String, Object> metaData = new HashMap<>(); | ||
metaData.put(ProcessInstanceEventBody.ID_META_DATA, "testKogitoProcessInstanceId"); | ||
metaData.put(ProcessInstanceEventBody.VERSION_META_DATA, "testKogitoProcessInstanceVersion"); | ||
metaData.put(ProcessInstanceEventBody.ROOT_ID_META_DATA, "testKogitoRootProcessInstanceId"); | ||
metaData.put(ProcessInstanceEventBody.PROCESS_ID_META_DATA, "testKogitoProcessId"); | ||
metaData.put(ProcessInstanceEventBody.PROCESS_TYPE_META_DATA, "testKogitoProcessType"); | ||
metaData.put(ProcessInstanceEventBody.ROOT_PROCESS_ID_META_DATA, "testKogitoRootProcessId"); | ||
metaData.put(ProcessInstanceEventBody.PARENT_ID_META_DATA, "testKogitoParentProcessInstanceId"); | ||
metaData.put(ProcessInstanceEventBody.STATE_META_DATA, "testKogitoProcessInstanceState"); | ||
|
||
NodeInstanceEventBody body = NodeInstanceEventBody.create() | ||
.id("testId") | ||
.processInstanceId("testProcessInstanceId") | ||
.connectionNodeInstanceId("connectionNodeInstanceId") | ||
.eventTime(new Date()) | ||
.eventType(1) | ||
.exitType(3) | ||
.data("test", 2) | ||
.nodeDefinitionId("testNodeDefinitionId") | ||
.nodeId("testNodeId") | ||
.nodeName("testNodeName") | ||
.nodeType("testNodeType") | ||
.build(); | ||
|
||
event = new NodeInstanceDataEvent(source, kogitoAddons, 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> codecUtils = mockStatic(CodecUtils.class)) { | ||
Codec<Document> 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<Document> 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().getId()) | ||
.containsEntry("processInstanceId", event.getData().getProcessInstanceId()) | ||
.containsEntry("connectionNodeInstanceId", event.getData().getConnectionNodeInstanceId()) | ||
.containsEntry("id", event.getData().getId()) | ||
.containsEntry("nodeId", event.getData().getNodeId()) | ||
.containsEntry("nodeDefinitionId", event.getData().getNodeDefinitionId()) | ||
.containsEntry("nodeName", event.getData().getNodeName()) | ||
.containsEntry("nodeType", event.getData().getNodeType()) | ||
.containsEntry("eventTime", event.getData().getEventTime()) | ||
.containsEntry("eventType", event.getData().getEventType()) | ||
.containsEntry("exitType", event.getData().getExitType()); | ||
} | ||
} | ||
|
||
@Test | ||
void getEncoderClass() { | ||
assertThat(codec.getEncoderClass()).isEqualTo(NodeInstanceDataEvent.class); | ||
} | ||
} |
Oops, something went wrong.