We have set of python scripts and prepared .json files which are used to
send json to publish topics in google cloud pub/sub
after topics are processed by services in the cloud new data arrives on websocket of our service if service has subscribed over websocket for specific topic
Right now the tests are executed manually and hard to extend and maintain
I would like to have an automated testing framework which will create .json from predefined DataClasses with couple of testrelated adjusted fields(builder pattern?)
Send it to cloud pub/sub and capable of sending and validating responses from defined websocket
Tests themself should be described on higher business level(gherkin/cucumber)
The resulting automated tests will be executed from gitlab pipeline.
Which frameworks and Languages would you suggest?
Assistant
For your requirements, I would suggest using Python as the language for test automation since you already have a set of Python scripts and .json files in your current workflow. Python also has many testing frameworks that can be used for this type of automation.
One framework that would fit well with your use case is Behave, which is a Python-based BDD (Behavior-Driven Development) testing framework that allows you to write tests in a Gherkin-style language, such as Cucumber, and automate them using Python. Behave allows you to define your test scenarios in a clear, concise manner that is easy to read and understand by both technical and non-technical stakeholders.
To automate the sending and validating of messages to the cloud pub/sub and websockets, you could use the Google Cloud Pub/Sub and websocket libraries for Python. These libraries provide a Python interface to interact with the Google Cloud Pub/Sub and websockets, respectively.
To create the .json files from predefined DataClasses, you can use the json and dataclasses libraries in Python. These libraries allow you to easily serialize and deserialize Python objects to JSON and vice versa.
Overall, a recommended stack for your testing automation would be:
Language: Python
Testing Framework: Behave
Google Cloud Pub/Sub library for Python
Websocket library for Python
json and dataclasses libraries in Python
You can integrate this automated testing framework into your GitLab pipeline using GitLab's CI/CD features.
User
Ok i have an example of .json file:
{
"common_message_fields": {
"tenantId": "FeatureOverview-ywir8",
"siteName": "Site A",
"timestamp": "CURRENT_TIME"
},
"events": [
{
"topic": "ingress-raw-occupancy-changed",
"delay": 3,
"message": {
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": true
}
},
{
"topic": "ingress-raw-occupancy-changed",
"delay": 3,
"message": {
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": true
}
},
{
"topic": "ingress-raw-occupancy-changed",
"delay": 3,
"time_diff": -4,
"message": {
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": false
}
}
],
"cleanup": [
{
"topic": "ingress-raw-occupancy-changed",
"message": {
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": false
}
}
]
}
How would it look if we have it in form of a dataclass(es) in Python?
And then creation of an object of this type in the Behave test
Assistant
To represent the JSON data as a Python object, you can define a data class with attributes that correspond to the fields in the JSON data. Here's an example of how you could define a data class for the example JSON data:
python
from dataclasses import dataclass
from typing import List, Dict
@dataclass
class CommonMessageFields:
tenantId: str
siteName: str
timestamp: str
@dataclass
class Event:
topic: str
delay: int
time_diff: int = 0
message: Dict[str, str]
@dataclass
class Cleanup:
topic: str
message: Dict[str, str]
@dataclass
class TestData:
common_message_fields: CommonMessageFields
events: List[Event]
cleanup: List[Cleanup]
With this data class definition, you can create an instance of the TestData class in your Behave test by initializing each attribute with the corresponding data:
python
from behave import given, when, then
import json
from datetime import datetime
@given("a test data object")
def step_impl(context):
# Define the common message fields
common_fields = CommonMessageFields(
tenantId="FeatureOverview-ywir8",
siteName="Site A",
timestamp=str(datetime.now())
)
# Define the events
event1 = Event(
topic="ingress-raw-occupancy-changed",
delay=3,
message={
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": True
}
)
event2 = Event(
topic="ingress-raw-occupancy-changed",
delay=3,
message={
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": True
}
)
event3 = Event(
topic="ingress-raw-occupancy-changed",
delay=3,
time_diff=-4,
message={
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": False
}
)
events = [event1, event2, event3]
# Define the cleanup messages
cleanup = [Cleanup(
topic="ingress-raw-occupancy-changed",
message={
"controlSystemName": "CCS1",
"locationName": "101",
"occupancy": False
}
)]
# Create the test data object
test_data = TestData(
common_message_fields=common_fields,
events=events,
cleanup=cleanup
)
# Store the test data object in the context for use in subsequent steps
context.test_data = test_data
User
Does it make sense to have default set of values which would fit most of casesm for example locationName seems to be the same and topic for this set of events
Assistant
Yes, it can make sense to have default values for fields that are commonly used across multiple events in your test data. This can help reduce the amount of duplicated code in your test data definitions and make your code more readable and maintainable.
In your example, if the locationName and topic fields are commonly used across multiple events, you could define default values for these fields in the respective data classes:
python
from dataclasses import dataclass, field
from typing import List, Dict
@dataclass
class CommonMessageFields:
tenantId: str
siteName: str
timestamp: str
@dataclass
class Event:
topic: str = "ingress-raw-occupancy-changed"
delay: int = 3
time_diff: int = 0
message: Dict[str, str] = field(default_factory=dict(locationName="101"))
@dataclass
class Cleanup:
topic: str = "ingress-raw-occupancy-changed"
message: Dict[str, str] = field(default_factory=dict(locationName="101"))
@dataclass
class TestData:
common_message_fields: CommonMessageFields
events: List[Event]
cleanup: List[Cleanup]
Here, the locationName field is set to "101" by default for all events and cleanup messages. The topic field is also set to "ingress-raw-occupancy-changed" by default for all events and cleanup messages.
With these default values, you can create instances of the Event and Cleanup classes without explicitly setting the locationName and topic fields:
Here, the locationName field is set to "101" by default, and the topic field is set to "ingress-raw-occupancy-changed" by default.
User
If i don't want to use python for this, what would be other good options?
Assistant
If you don't want to use Python for your automated testing framework, there are many other options available. Here are a few suggestions:
Java - Java is a popular language for automated testing, and there are many testing frameworks available for it, such as Cucumber and JUnit. You can use the Google Cloud Pub/Sub and websocket libraries for Java to interact with the Google Cloud services.
JavaScript - JavaScript is commonly used for web automation and has a variety of testing frameworks available, such as Cucumber.js and Jest. You can use the Google Cloud Pub/Sub and websocket libraries for Node.js to interact with the Google Cloud services.
Ruby - Ruby is another popular language for automated testing, and there are several testing frameworks available for it, such as Cucumber and RSpec. You can use the Google Cloud Pub/Sub and websocket libraries for Ruby to interact with the Google Cloud services.
Go - Go is a newer language that is becoming popular for automated testing. It has several testing frameworks available, such as Ginkgo and GoConvey. You can use the Google Cloud Pub/Sub and websocket libraries for Go to interact with the Google Cloud services.
Other options - There are many other languages and testing frameworks available, such as PHP with Behat, C# with SpecFlow, and more. It's important to choose a language and framework that aligns with your team's expertise and the requirements of your project.
Regardless of which language and framework you choose, you can integrate your automated tests with GitLab CI/CD to run them automatically as part of your pipeline.
User
let's try to create a data classes from this json in java
Assistant
Sure, here's an example of how you could represent the example JSON data as Java classes using the Jackson JSON library:
java
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName;
private String timestamp;
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
public class Event {
private String topic;
private int delay;
private int time_diff;
private Map<String, String> message;
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
public int getTime_diff() {
return time_diff;
}
public void setTime_diff(int time_diff) {
this.time_diff = time_diff;
}
public Map<String, String> getMessage() {
return message;
}
public void setMessage(Map<String, String> message) {
this.message = message;
}
}
public class Cleanup {
private String topic;
private Map<String, String> message;
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public Map<String, String> getMessage() {
return message;
}
public void setMessage(Map<String, String> message) {
this.message = message;
}
}
User
Ok now let's have a default initialization which will create a valid object with those values which are most common taking as argument those which are never common
Assistant
Sure, here's an example of how you could add default initialization values to the Java classes:
java
User
It is always Site A for the siteName
Assistant
Thanks for pointing that out! Here's the updated example with the default value for siteName set to "Site A":
java
User
Ok so now we have a second json file it should have named static method which will create an object with common fields of this type, let's call it: deviceError
support for previous events etc should still work
Assistant
Sure, here's an updated version of the TestData class with a new method getDefaultDeviceError that creates an object with the common fields for the deviceError type:
java
User
If now i need a new device error object for Site B and with errorNumber 12341010001
how would i construct it in my testcase
Assistant
To create a new device error object for Site B with errorNumber set to 12341010001, you can use the getDefaultDeviceError method of the TestData class and then modify the appropriate fields using setter methods. Here's an example of how you could do it:
This code first creates a default device error object with tenantId set to "FeatureOverview-ywir8", using the TestData.getDefaultDeviceError method. It then modifies the siteName field of the commonMessageFields object to "Site B", and adds a new field "errorNumber" with value "12341010001" to the message object of the first event. Finally, it uses the modified testData object for testing.
User
I would like to simplify it, basically:
TestData testData = TestData.getDefaultDeviceError(tenantId)
.setSiteName("Site B")
.setErrorNumber("12341010001");
How would the class look like?
Assistant
To simplify the construction of a TestData object with modified fields, you can add setter methods to the TestData class that return the object itself, and use method chaining to set multiple fields at once. Here's an example of how the updated TestData class would look like:
java
User
Code seem to be incomplete
Assistant
Apologies for the mistake. Here's the complete code:
java
User
remaining code?
Assistant
I apologize for the mistake. Here's the rest of the code:
java
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
public class Event {
private String topic;
private int delay;
private Map<String, String> message;
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
public Map<String, String> getMessage() {
return message;
}
public void setMessage(Map<String, String> message) {
this.message = message;
}
}
public class Cleanup {
private String topic;
private Map<String, String> message;
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public Map<String, String> getMessage() {
return message;
}
public void setMessage(Map<String, String> message) {
this.message = message;
}
}
With these modifications, you can now create a new TestData object with modified fields using method chaining. Here's an example:
This code first creates a default device error TestData object with tenantId set to the tenantId variable, using the TestData.getDefaultDeviceError method. It then sets the siteName field to "Site B" and the errorNumber field to "12341010001" using method chaining. Finally, it assigns the resulting testData object to a variable for further use.
User
There are always 3 events, 2 of them have identical errorNumber, if its defined. Last one has null as errorNumber.
Before mentioned constructor should create an object which after serialization creates this json:
{
"common_message_fields": {
"tenantId": "FeatureOverview-ywir8",
"siteName": "Site B",
"timestamp": "CURRENT_TIME"
},
"events": [
{
"topic": "ingress-raw-device-error-changed",
"delay": 3,
"message": {
"controlSystemName": "CCS1",
"errorNumber": "12341010001",
"errorIndex": "401",
"deviceName": "e-stop-401",
"errorParameters": {
"var1": "e-stop-401",
"var2": "[dummy var2]"
}
}
},
{
"topic": "ingress-raw-device-error-changed",
"delay": 3,
"message": {
"controlSystemName": "CCS1",
"errorNumber": "12341010001",
"errorIndex": "401",
"deviceName": "e-stop-401",
"errorParameters": {
"var1": "e-stop-401",
"var2": "[dummy var2]"
}
}
},
{
"topic": "ingress-raw-device-error-changed",
"delay": 3,
"time_diff": -4,
"message": {
"controlSystemName": "CCS1",
"errorNumber": null,
"errorIndex": "401",
"deviceName": "e-stop-401",
"errorParameters": {}
}
}
],
"cleanup": [
{
"topic": "ingress-raw-device-error-changed",
"message": {
"controlSystemName": "CCS1",
"errorNumber": null,
"errorIndex": "401",
"deviceName": "e-stop-401",
"errorParameters": {}
}
}
]
}
Assistant
Based on the updated requirements, you can modify the getDefaultDeviceError method to create a TestData object with the specified fields. Here's how the updated method would look like:
java
public static TestData getDefaultDeviceError(String tenantId, String siteName, String errorNumber) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
commonFields.setSiteName(siteName);
Event event1 = new Event();
event1.setTopic("ingress-raw-device-error-changed");
event1.setDelay(3);
Map<String, Object> message1 = new HashMap<String, Object>();
message1.put("controlSystemName", "CCS1");
message1.put("errorNumber", errorNumber);
message1.put("errorIndex", "401");
message1.put("deviceName", "e-stop-401");
message1.put("errorParameters", new HashMap<String, Object>() {{
put("var1", "e-stop-401");
put("var2", "[dummy var2]");
}});
event1.setMessage(message1);
Event event2 = new Event();
event2.setTopic("ingress-raw-device-error-changed");
event2.setDelay(3);
Map<String, Object> message2 = new HashMap<String, Object>();
message2.put("controlSystemName", "CCS1");
message2.put("errorNumber", errorNumber);
message2.put("errorIndex", "401");
message2.put("deviceName", "e-stop-401");
message2.put("errorParameters", new HashMap<String, Object>() {{
put("var1", "e-stop-401");
put("var2", "[dummy var2]");
}});
event2.setMessage(message2);
Event event3 = new Event();
event3.setTopic("ingress-raw-device-error-changed");
event3.setDelay(3);
event3.setTime_diff(-4);
Map<String, Object> message3 = new HashMap<String, Object>();
message3.put("controlSystemName", "CCS1");
message3.put("errorNumber", null);
message3.put("errorIndex", "401");
message3.put("deviceName", "e-stop-401");
message3.put("errorParameters", new HashMap<String, Object>());
event3.setMessage(message3);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-device-error-changed");
cleanup.setMessage(message3);
return new TestData(commonFields, Arrays.asList(event1, event2, event3), Collections.singletonList(cleanup));
}
User
can you reduce repitions on message creation?
Assistant
Yes, you can reduce the repetition in the getDefaultDeviceError method by extracting the message creation code into a separate method. Here's an updated version of the method:
In this updated method, we create the message object once using a helper method createMessage that takes errorNumber as a parameter. This method creates and returns the message object with the specified errorNumber. We then use this message object to set the message field in the first two event objects, and create a new nullMessage object to set the message field in the third event object and the cleanup object.
With this refactoring, the getDefaultDeviceError method is now shorter and more readable, and the repetition in the message creation code is eliminated.
User
can you reduce repitions of event creations
Assistant
Yes, you can also reduce the repetition in the event creation code by using a loop instead of creating each event individually. Here's how the updated method would look like:
java
public static TestData getDefaultDeviceError(String tenantId, String siteName, String errorNumber) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
commonFields.setSiteName(siteName);
Map<String, Object> message = createMessage(errorNumber);
List<Event> events = new ArrayList<>();
for (int i = 0; i < 2; i++) {
Event event = new Event();
event.setTopic("ingress-raw-device-error-changed");
event.setDelay(3);
event.setMessage(message);
events.add(event);
}
Event event3 = new Event();
event3.setTopic("ingress-raw-device-error-changed");
event3.setDelay(3);
event3.setTime_diff(-4);
Map<String, Object> nullMessage = createMessage(null);
event3.setMessage(nullMessage);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-device-error-changed");
cleanup.setMessage(nullMessage);
events.add(event3);
return new TestData(commonFields, events, Collections.singletonList(cleanup));
}
private static Map<String, Object> createMessage(String errorNumber) {
Map<String, Object> message = new HashMap<>();
message.put("controlSystemName", "CCS1");
message.put("errorNumber", errorNumber);
message.put("errorIndex", "401");
message.put("deviceName", "e-stop-401");
message.put("errorParameters", new HashMap<String, Object>() {{
put("var1", "e-stop-401");
put("var2", "[dummy var2]");
}});
return message;
}
In this updated method, we create the first two event objects using a loop that runs twice. Inside the loop, we create a new event object, set its topic, delay, and message fields using the message object created by the createMessage helper method, and add it to the events list. After the loop, we create the third event object and the cleanup object as before.
With this further refactoring, the getDefaultDeviceError method is now even shorter and more readable, and the repetition in the event creation code is eliminated.
Dataset releases
Not yet included in a published release.
Now you have a test_data object in your context that can be used to send and validate messages in your Behave test. You can serialize this object to JSON using the json library, like this:
python
test_data_json = json.dumps(test_data.__dict__)
Note that the __dict__ attribute is used to get the dictionary representation of the object, which can then be serialized to JSON.
With these classes, you can deserialize the example JSON data into a TestData object using the ObjectMapper class from the Jackson library:
java
String json = "JSON data here";
TestData testData = TestData.fromJson(json);
Note that you'll need to add the Jackson JSON library to your project's dependencies for this to work.
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
public static TestData getDefault(String tenantId, String siteName) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
commonFields.setSiteName(siteName);
Event event = new Event();
event.setTopic("ingress-raw-occupancy-changed");
event.setDelay(3);
Map<String, String> message = new HashMap<String, String>();
message.put("locationName", "101");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-occupancy-changed");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public TestData(CommonMessageFields commonMessageFields, List<Event> events, List<Cleanup> cleanup) {
this.commonMessageFields = commonMessageFields;
this.events = events;
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName;
private String timestamp;
public CommonMessageFields() {
this.timestamp = String.valueOf(System.currentTimeMillis());
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
public class Event {
private String topic;
private int delay;
private int time_diff;
private Map<String, String> message;
public Event() {
this.time_diff = 0;
this.message = new HashMap<String, String>();
this.message.put("locationName", "101");
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
public int getTime_diff() {
return time_diff;
}
public void setTime_diff(int time_diff) {
this.time_diff = time_diff;
}
public Map<String, String> getMessage
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
public static TestData getDefault(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-raw-occupancy-changed");
event.setDelay(3);
Map<String, String> message = new HashMap<String, String>();
message.put("locationName", "101");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-occupancy-changed");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public TestData(CommonMessageFields commonMessageFields, List<Event> events, List<Cleanup> cleanup) {
this.commonMessageFields = commonMessageFields;
this.events = events;
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName = "Site A";
private String timestamp;
public CommonMessageFields() {
this.timestamp = String.valueOf(System.currentTimeMillis());
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
public class Event {
private String topic;
private int delay;
private int time_diff;
private Map<String, String> message;
public Event() {
this.time_diff = 0;
this.message = new HashMap<String, String>();
this.message.put("locationName", "101");
}
public String getTopic() {
return topic;
}
public void setTopic(String topic) {
this.topic = topic;
}
public int getDelay() {
return delay;
}
public void setDelay(int delay) {
this.delay = delay;
}
public int getTime_diff() {
return time_diff;
}
public void setTime_diff(int time_diff) {
this.time_diff = time_diff;
}
public Map<String, String> getMessage()
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
public static TestData getDefault(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-raw-occupancy-changed");
event.setDelay(3);
Map<String, String> message = new HashMap<String, String>();
message.put("locationName", "101");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-occupancy-changed");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public static TestData getDefaultDeviceError(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-device-error");
event.setDelay(5);
Map<String, String> message = new HashMap<String, String>();
message.put("deviceId", "device-001");
message.put("errorCode", "ERR-001");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-device-error");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public TestData(CommonMessageFields commonMessageFields, List<Event> events, List<Cleanup> cleanup) {
this.commonMessageFields = commonMessageFields;
this.events = events;
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName = "Site A";
private String timestamp;
public CommonMessageFields() {
this.timestamp = String.valueOf(System.currentTimeMillis());
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
}
public class Event {
private String topic;
private int delay;
private int time
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
public static TestData getDefault(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-raw-occupancy-changed");
event.setDelay(3);
Map<String, String> message = new HashMap<String, String>();
message.put("locationName", "101");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-occupancy-changed");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public static TestData getDefaultDeviceError(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-device-error");
event.setDelay(5);
Map<String, String> message = new HashMap<String, String>();
message.put("deviceId", "device-001");
message.put("errorCode", "ERR-001");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-device-error");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public TestData setSiteName(String siteName) {
this.commonMessageFields.setSiteName(siteName);
return this;
}
public TestData setErrorNumber(String errorNumber) {
Map<String, String> message = this.events.get(0).getMessage();
message.put("errorNumber", errorNumber);
return this;
}
public TestData(CommonMessageFields commonMessageFields, List<Event> events, List<Cleanup> cleanup) {
this.commonMessageFields = commonMessageFields;
this.events = events;
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName = "Site A";
private String timestamp;
public CommonMessageFields() {
this.timestamp = String.valueOf(System.currentTimeMillis());
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId =
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
public class TestData {
@JsonProperty("common_message_fields")
private CommonMessageFields commonMessageFields;
private List<Event> events;
private List<Cleanup> cleanup;
public static TestData fromJson(String json) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.readValue(json, TestData.class);
}
public CommonMessageFields getCommonMessageFields() {
return commonMessageFields;
}
public void setCommonMessageFields(CommonMessageFields commonMessageFields) {
this.commonMessageFields = commonMessageFields;
}
public List<Event> getEvents() {
return events;
}
public void setEvents(List<Event> events) {
this.events = events;
}
public List<Cleanup> getCleanup() {
return cleanup;
}
public void setCleanup(List<Cleanup> cleanup) {
this.cleanup = cleanup;
}
public static TestData getDefault(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-raw-occupancy-changed");
event.setDelay(3);
Map<String, String> message = new HashMap<String, String>();
message.put("locationName", "101");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-raw-occupancy-changed");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public static TestData getDefaultDeviceError(String tenantId) {
CommonMessageFields commonFields = new CommonMessageFields();
commonFields.setTenantId(tenantId);
Event event = new Event();
event.setTopic("ingress-device-error");
event.setDelay(5);
Map<String, String> message = new HashMap<String, String>();
message.put("deviceId", "device-001");
message.put("errorCode", "ERR-001");
event.setMessage(message);
Cleanup cleanup = new Cleanup();
cleanup.setTopic("ingress-device-error");
cleanup.setMessage(message);
return new TestData(commonFields, Collections.singletonList(event), Collections.singletonList(cleanup));
}
public TestData setSiteName(String siteName) {
this.commonMessageFields.setSiteName(siteName);
return this;
}
public TestData setErrorNumber(String errorNumber) {
Map<String, String> message = this.events.get(0).getMessage();
message.put("errorNumber", errorNumber);
return this;
}
public TestData(CommonMessageFields commonMessageFields, List<Event> events, List<Cleanup> cleanup) {
this.commonMessageFields = commonMessageFields;
this.events = events;
this.cleanup = cleanup;
}
}
public class CommonMessageFields {
private String tenantId;
private String siteName = "Site A";
private String timestamp;
public CommonMessageFields() {
this.timestamp = String.valueOf(System.currentTimeMillis());
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getSiteName() {
return siteName;
}
public void setSiteName(String siteName) {
this.siteName = siteName;
}
public String getTimestamp
This method takes in three parameters - tenantId, siteName, and errorNumber - and uses them to create a TestData object with the specified values. The siteName parameter sets the value of the siteName field in the common_message_fields object, while the errorNumber parameter sets the value of the errorNumber field in the first two event objects. If errorNumber is null, the errorNumber field is set to null in the third event object and the cleanup object.
You can then use this method to create a new TestData object with the desired fields: