-
Notifications
You must be signed in to change notification settings - Fork 1
[th2-2469] builders for parsed/raw messages and events #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 32 commits
3eb34a0
b01c412
e0cbdd7
393f1ef
be9dc7a
4edafbb
3fc7367
82ee1da
23eafc2
439b341
80059af
744222e
de82519
19ff75b
696709a
6804b78
89d06a2
f9634b9
dc557a1
eb39015
bbfc803
d1e71cf
2a6b5ec
a2fb40d
b4c4eec
43b1e13
58fd435
6377c44
7edd785
50b66bb
11057a9
610ada4
3f434f6
20113be
d116412
f494df3
7c3e6c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ public class Event { | |
| protected static final ThreadLocal<ObjectMapper> OBJECT_MAPPER = ThreadLocal.withInitial(() -> new ObjectMapper().setSerializationInclusion(NON_NULL)); | ||
|
|
||
| protected final String id = generateUUID(); | ||
| protected String bookName; | ||
| protected final List<Event> subEvents = new ArrayList<>(); | ||
| protected final List<MessageID> attachedMessageIDS = new ArrayList<>(); | ||
| protected final List<IBodyData> body = new ArrayList<>(); | ||
|
|
@@ -69,34 +70,73 @@ public class Event { | |
| protected String name; | ||
| protected String description; | ||
| protected Status status = Status.PASSED; | ||
| private final IEventFactory eventFactory; | ||
|
|
||
| protected Event(Instant startTimestamp, @Nullable Instant endTimestamp) { | ||
| Event(Instant startTimestamp, @Nullable Instant endTimestamp, IEventFactory eventFactory) { | ||
| this.startTimestamp = startTimestamp; | ||
| this.endTimestamp = endTimestamp; | ||
| this.eventFactory = eventFactory; | ||
| } | ||
|
|
||
| protected Event(Instant startTimestamp) { | ||
| this(startTimestamp, null); | ||
| Event(Instant startTimestamp, IEventFactory eventFactory) { | ||
| this(startTimestamp, null, eventFactory); | ||
| } | ||
|
|
||
| protected Event() { | ||
| this(Instant.now()); | ||
| Event(IEventFactory eventFactory) { | ||
| this(Instant.now(), eventFactory); | ||
| } | ||
|
|
||
| /** | ||
| * Creates event with current time as start | ||
| * @param eventFactory | ||
| * @return new event | ||
| */ | ||
| public static Event start(IEventFactory eventFactory) { | ||
| return eventFactory.start(); | ||
| } | ||
|
Comment on lines
+94
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need this method here at all. We can simply call
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @andreydrobynin @Nikita-Smirnov-Exactpro what do you think about that?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @OptimumOpium as I remember @Nikita-Smirnov-Exactpro asked to save back compatibility.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a new method. How do we save backward compatibility here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant we want to have a new method with same name as an old. I think @Nikita-Smirnov-Exactpro can clarify it correctly than me.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From my point of view, it looks a bit strange. Why do I have to write |
||
|
|
||
| /** | ||
| * Creates event with current time as start | ||
| * @return new event | ||
| */ | ||
| @Deprecated | ||
| public static Event start() { | ||
| return new Event(); | ||
| // TODO eventFactory == null? | ||
| return new Event(null); | ||
| } | ||
|
|
||
| /** | ||
| * Sets event book name if passed {@code bookName} is not blank. | ||
| * @return current event | ||
| * @throws IllegalStateException if book name already set | ||
| */ | ||
| public Event bookName(String bookName) { | ||
| if (isNotBlank(bookName)) { | ||
| if (this.bookName != null) { | ||
| throw new IllegalStateException(formatStateException("Book name", this.bookName)); | ||
| } | ||
| this.bookName = bookName; | ||
| } | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * Creates event with passed time as start | ||
| * @param eventFactory | ||
| * @return new event | ||
| */ | ||
| public static Event from(Instant startTimestamp, IEventFactory eventFactory) { | ||
| return eventFactory.from(startTimestamp); | ||
| } | ||
|
Comment on lines
+128
to
+130
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same as for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also back compatibility |
||
|
|
||
| /** | ||
| * Creates event with passed time as start | ||
| * @return new event | ||
| */ | ||
| @Deprecated | ||
| public static Event from(Instant startTimestamp) { | ||
| return new Event(startTimestamp); | ||
| // TODO eventFactory == null? | ||
| return new Event(startTimestamp, null); | ||
| } | ||
|
|
||
| @Contract("null -> null") | ||
|
|
@@ -185,7 +225,7 @@ public Event status(Status eventStatus) { | |
| */ | ||
| @SuppressWarnings("NonBooleanMethodNameMayNotStartWithQuestion") | ||
| public Event addSubEventWithSamePeriod() { | ||
| return addSubEvent(new Event(startTimestamp, endTimestamp)); | ||
| return addSubEvent(eventFactory.from(startTimestamp, endTimestamp, eventFactory)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -245,7 +285,7 @@ public Event messageID(MessageID attachedMessageID) { | |
| */ | ||
| @Deprecated | ||
| public List<com.exactpro.th2.common.grpc.Event> toProtoEvents(@Nullable String parentID) throws JsonProcessingException { | ||
| return toListProto(toEventID(parentID)); | ||
| return toListProto(toEventID(parentID, bookName)); | ||
| } | ||
|
|
||
|
|
||
|
|
@@ -258,7 +298,7 @@ public List<com.exactpro.th2.common.grpc.Event> toListProto(@Nullable EventID pa | |
| */ | ||
| @Deprecated | ||
| public com.exactpro.th2.common.grpc.Event toProtoEvent(@Nullable String parentID) throws JsonProcessingException { | ||
| return toProto(toEventID(parentID)); | ||
| return toProto(toEventID(parentID, bookName)); | ||
| } | ||
|
|
||
| public com.exactpro.th2.common.grpc.Event toProto(@Nullable EventID parentID) throws JsonProcessingException { | ||
|
|
@@ -271,7 +311,7 @@ public com.exactpro.th2.common.grpc.Event toProto(@Nullable EventID parentID) th | |
| .append(description); | ||
| } | ||
| var eventBuilder = com.exactpro.th2.common.grpc.Event.newBuilder() | ||
| .setId(toEventID(id)) | ||
| .setId(toEventID(id, bookName)) | ||
| .setName(nameBuilder.toString()) | ||
| .setType(defaultIfBlank(type, UNKNOWN_EVENT_TYPE)) | ||
| .setStartTimestamp(toTimestamp(startTimestamp)) | ||
|
|
@@ -349,13 +389,13 @@ public Instant getEndTimestamp() { | |
| */ | ||
| @Deprecated | ||
| protected List<com.exactpro.th2.common.grpc.Event> collectSubEvents(List<com.exactpro.th2.common.grpc.Event> protoEvents, @Nullable String parentID) throws JsonProcessingException { | ||
| return collectSubEvents(protoEvents, toEventID(parentID)); | ||
| return collectSubEvents(protoEvents, toEventID(parentID, bookName)); | ||
| } | ||
|
|
||
| protected List<com.exactpro.th2.common.grpc.Event> collectSubEvents(List<com.exactpro.th2.common.grpc.Event> protoEvents, @Nullable EventID parentID) throws JsonProcessingException { | ||
| protoEvents.add(toProto(parentID)); // collect current level | ||
| for (Event subEvent : subEvents) { | ||
| subEvent.collectSubEvents(protoEvents, toEventID(id)); // collect sub level | ||
| subEvent.collectSubEvents(protoEvents, toEventID(id, bookName)); // collect sub level | ||
| } | ||
| return protoEvents; | ||
| } | ||
|
|
@@ -365,7 +405,7 @@ protected byte[] buildBody() throws JsonProcessingException { | |
| } | ||
|
|
||
| protected String formatStateException(String fieldName, Object value) { | ||
| return fieldName + " in event '" + id + "' already sed with value '" + value + '\''; | ||
| return fieldName + " in event '" + id + "' already set with value '" + value + '\''; | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| public enum Direction { | ||
| FIRST(0), | ||
| SECOND(1); | ||
|
|
||
| private final int value; | ||
|
|
||
| Direction(int value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| public int getValue() { | ||
| return value; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.function.Consumer; | ||
|
|
||
| public interface MessageBodyBuilder { | ||
| MessageBodyBuilder putSimpleField(String name, Object value); | ||
|
|
||
| MessageBodyBuilder putSimpleField(String name, Collection<Object> value); | ||
|
|
||
| MessageBodyBuilder putMessage(String name, Consumer<MessageBodyBuilder> setup); | ||
|
|
||
| MessageBodyBuilder addMessage(String name, Consumer<MessageBodyBuilder> setup); | ||
|
|
||
| MessageBodyBuilder putMessages(String name, Collection<Consumer<MessageBodyBuilder>> setup); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| public interface MessageBuilder<Builder extends MessageBuilder<Builder, MB, R>, MB extends MetadataBuilder<MB>, R> { | ||
| Builder setParentEventId(String id, String bookName); | ||
andrew-drobynin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| MB metadataBuilder(); | ||
|
|
||
| R build(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| import com.exactpro.th2.common.message.impl.ParsedMessageBuilderImpl; | ||
| import com.exactpro.th2.common.message.impl.RawMessageBuilderImpl; | ||
|
|
||
| public class MessageFactory { | ||
| public ParsedMessageBuilderImpl createParsedMessage() { | ||
| return new ParsedMessageBuilderImpl(); | ||
| } | ||
|
|
||
| public RawMessageBuilderImpl createRawMessage() { | ||
| return new RawMessageBuilderImpl(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| import java.time.Instant; | ||
|
|
||
| public interface MetadataBuilder<Builder extends MetadataBuilder<Builder>> { | ||
| Builder setSessionAlias(String alias); | ||
|
|
||
| Builder setDirection(Direction direction); | ||
|
|
||
| Builder setSequence(long sequence); | ||
|
|
||
| Builder addSubsequence(int subSequence); | ||
|
|
||
| Builder setBookName(String bookName); | ||
|
|
||
| Builder setTimestamp(Instant timestamp); | ||
|
|
||
| Builder putProperty(String key, String value); | ||
|
|
||
| Builder setProtocol(String protocol); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| public interface ParsedMessageBuilder<R> extends MessageBuilder<ParsedMessageBuilder<R>, ParsedMetadataBuilder, R>, | ||
| MessageBodyBuilder { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2021-2021 Exactpro (Exactpro Systems Limited) | ||
| * | ||
| * 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 com.exactpro.th2.common.message; | ||
|
|
||
| public interface ParsedMetadataBuilder extends MetadataBuilder<ParsedMetadataBuilder> { | ||
| ParsedMetadataBuilder setMessageType(String messageType); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should make it final and set in the constructor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Nikita-Smirnov-Exactpro asked to have builder methods (e.g.
bookName(),endTimestamp())There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand why we need the
endTimestampsetter. But why do we need a setter forbookName? Why shouldn't we set it once in the constructor?