Skip to content
Open
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3eb34a0
[th2-2469] builders for parsed/raw messages and events
Oct 6, 2021
b01c412
code review: back rename EventBuilder -> Event
Oct 11, 2021
e0cbdd7
code review: event/message factories
Oct 11, 2021
393f1ef
code review: event constructors with EventFactory
Oct 11, 2021
be9dc7a
code review: subsequences as list
Oct 11, 2021
4edafbb
code review: support message and list value
Oct 11, 2021
3fc7367
code review: eventFactory as field in Event
Oct 12, 2021
82ee1da
code review: raw/parsed builders new constructors and lateinit fields
Oct 12, 2021
23eafc2
[th2-2469] Corrected logging in the ConfigurationManager
Nikita-Smirnov-Exactpro Oct 13, 2021
439b341
nullable parentEventId
Oct 13, 2021
80059af
protocol = "" and messageType = "" by default
Oct 13, 2021
744222e
added missed setter for sessionAlias
Oct 13, 2021
de82519
moved setters to parent builder
Oct 13, 2021
19ff75b
builders with generic to avoid duplicate code
Oct 14, 2021
696709a
list of simple and messages as vararg
Oct 15, 2021
6804b78
builder for inner messages only with fields
Oct 15, 2021
89d06a2
ParsedInnerMessageBuilder as field in ParsedMessageBuilder
Oct 15, 2021
f9634b9
ParsedInnerMessageBuilder creation in factory
Oct 15, 2021
dc557a1
corrected javadoc
Oct 15, 2021
eb39015
added interfaces for builders
Oct 15, 2021
bbfc803
Added test with builders
Oct 18, 2021
d1e71cf
Added builders from https://gitlab.exactpro.com/oleg.smirnov/message-…
Oct 19, 2021
2a6b5ec
Uncommented code in kotlin builder extension
Oct 19, 2021
a2fb40d
Direction as enum in common
Oct 20, 2021
b4c4eec
Added all fields for parsed message, renamed "message" -> "parsed mes…
Oct 20, 2021
43b1e13
Added all fields for raw message
Oct 20, 2021
58fd435
Moved old builders to "old" package, use new MessageFactory
Oct 20, 2021
6377c44
Moved common code for message metadata building
Oct 21, 2021
7edd785
Moved common code for message building
Oct 21, 2021
50b66bb
Removed first builders approach
Oct 22, 2021
11057a9
Added tests for event
Oct 25, 2021
610ada4
Added bookName to MessageID and EventID
Oct 25, 2021
3f434f6
code review: logger lambda
Nov 10, 2021
20113be
code review: deprecated method
Nov 10, 2021
d116412
code review: EventFactory without CommonFactory
Nov 10, 2021
f494df3
Merge remote-tracking branch 'origin/master' into th2-2469
Nikita-Smirnov-Exactpro Sep 3, 2022
7c3e6c9
[th2-2468] Added the addMessages method into MessageBodyBuilder
Nikita-Smirnov-Exactpro Sep 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ test {
dependencies {
api platform('com.exactpro.th2:bom:3.0.0')
api "com.exactpro.th2:cradle-core:${cradleVersion}"
api 'com.exactpro.th2:grpc-common:3.7.0'
api 'com.exactpro.th2:grpc-common:3.8.0-th2-2567-1380894854-SNAPSHOT'

implementation 'com.google.protobuf:protobuf-java-util'
implementation 'com.exactpro.th2:grpc-service-generator:3.1.12'
Expand Down
68 changes: 54 additions & 14 deletions src/main/java/com/exactpro/th2/common/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor

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

Copy link
Contributor Author

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())

Copy link
Contributor

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 endTimestamp setter. But why do we need a setter for bookName? Why shouldn't we set it once in the constructor?

protected final List<Event> subEvents = new ArrayList<>();
protected final List<MessageID> attachedMessageIDS = new ArrayList<>();
protected final List<IBodyData> body = new ArrayList<>();
Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 IEventFactory.start() instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreydrobynin @Nikita-Smirnov-Exactpro what do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@OptimumOpium as I remember @Nikita-Smirnov-Exactpro asked to save back compatibility.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a new method. How do we save backward compatibility here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 Event.start(eventFactory) instead of eventFactory.start() in my code?


/**
* 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as for start method

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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")
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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));
}


Expand All @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 + '\'';
}

/**
Expand Down
15 changes: 11 additions & 4 deletions src/main/java/com/exactpro/th2/common/event/EventUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2020 Exactpro (Exactpro Systems Limited)
* Copyright 2020-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.
Expand Down Expand Up @@ -38,11 +38,18 @@ public static Message createMessageBean(String text) {

@Contract("null -> null; !null -> !null")
public static @Nullable EventID toEventID(@Nullable String id) {
return toEventID(id, null);
}

@Contract("null, null -> null; null, !null -> null; !null, !null -> !null; !null, null -> !null")
public static @Nullable EventID toEventID(@Nullable String id, @Nullable String bookName) {
if (id == null) {
return null;
}
return EventID.newBuilder()
.setId(id)
.build();
EventID.Builder builder = EventID.newBuilder().setId(id);
if (bookName != null) {
builder.setBookName(bookName);
}
return builder.build();
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/exactpro/th2/common/message/Direction.java
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);

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);
}
Loading