Skip to content

Commit

Permalink
Re-enable test-case with list of payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
pankalog committed Feb 5, 2024
1 parent d84222d commit 9e9d721
Showing 1 changed file with 56 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.openremote.test.custom

import com.fasterxml.jackson.databind.ObjectMapper
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import org.openremote.agent.protocol.mqtt.MQTTMessage
import org.openremote.agent.protocol.mqtt.MQTT_IOClient
import org.openremote.container.Container
Expand All @@ -10,6 +12,8 @@ import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.event.ClientEventService
import org.openremote.manager.mqtt.MQTTBrokerService
import org.openremote.model.teltonika.TeltonikaDataPayloadModel
import telematics.teltonika.TeltonikaDataPayload
import telematics.teltonika.TeltonikaMQTTHandler
import org.openremote.manager.setup.SetupService
import org.openremote.manager.setup.custom.CustomKeycloakSetup
Expand Down Expand Up @@ -358,7 +362,7 @@ class TeltonikaMQTTProtocolTest extends Specification implements ManagerContaine

cleanup: "delete asset and disconnect client from broker"

assetStorageService.delete(asset.getId() as List<String>);
assetStorageService.delete([asset.getId()]);

client.removeAllMessageConsumers()
client.disconnect()
Expand Down Expand Up @@ -446,13 +450,13 @@ class TeltonikaMQTTProtocolTest extends Specification implements ManagerContaine
asset = assetStorageService.find(asset.getId())
Optional<Attribute<?>> attr = asset.getAttribute(getTELTONIKA_DEVICE_RECEIVE_COMMAND_ATTRIBUTE_NAME());
assert attr.isPresent();
assert attr.get().getValue(String.class).get() == testResponseValue;
assert attr.get().getValue(String.class).get() == testResponseValue
}


cleanup: "delete asset and disconnect client from broker"

boolean result = assetStorageService.delete(asset.getId() as List<String>);
boolean result = assetStorageService.delete([asset.getId()]);

conditions.eventually {
result
Expand All @@ -467,50 +471,60 @@ class TeltonikaMQTTProtocolTest extends Specification implements ManagerContaine
// Asset state duration, bidirectional messages, etc.


// def "the handler stores all attributes with the correct timestamp"() {
def "the handler stores all attributes with the correct timestamp"() {
// when: "remove the asset, if it exists"
// then: "asset is not there"
// conditions.eventually {
// assert assetStorageService.delete([UniqueIdentifierGenerator.generateId(TELTONIKA_DEVICE_IMEI)])
// assert assetStorageService.find(UniqueIdentifierGenerator.generateId(TELTONIKA_DEVICE_IMEI)) == null
// }
//
// when: "the device connects to the MQTT broker to a data topic with a RX endpoint"
//
// String correctTopic1 = "${Constants.MASTER_REALM}/${mqttClientId}/${TELTONIKA_DEVICE_TOKEN}/${TELTONIKA_DEVICE_IMEI}/${TELTONIKA_DEVICE_RECEIVE_TOPIC}".toString();
// client.connect();
// then: "mqtt connection should exist"
// conditions.eventually {
// assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
// def connection = mqttBrokerService.getConnectionFromClientID(mqttClientId)
// assert connection != null
// }
// when: "a client subscribes to the correct data topic"
// client.addMessageConsumer(correctTopic1, { _ -> return });
//
// then: "A subscription should exist"
// conditions.eventually {
// assert client.topicConsumerMap.get(correctTopic1) != null
// assert handler.connectionSubscriberInfoMap.containsKey(getTELTONIKA_DEVICE_IMEI());
// }
//
// and: "the JSON with all the payloads is parsed"
// def slurper = new JsonSlurper()
// ArrayList<LazyMap> payloads = slurper.parseText(getClass().getResource("/teltonika/SortedPayloads.json").text) as ArrayList<Object>;
//
// and: "the device starts publishing payloads"
// payloads.each { LazyMap payload ->
// // Your logic here, for example:
// client.sendMessage(new MQTTMessage<String>(correctTopic1, JsonOutput.toJson(payload)))
// sleep(1000);
// }
//
// cleanup: "disconnect client from broker"
// client.disconnect()
// client.removeAllMessageConsumers();
//
//
// }

when: "the device connects to the MQTT broker to a data topic with a RX endpoint"

String correctTopic1 = "${Constants.MASTER_REALM}/${mqttClientId}/${TELTONIKA_DEVICE_TOKEN}/${TELTONIKA_DEVICE_IMEI}/${TELTONIKA_DEVICE_RECEIVE_TOPIC}".toString();
client.connect();
then: "mqtt connection should exist"
conditions.eventually {
assert client.getConnectionStatus() == ConnectionStatus.CONNECTED
def connection = mqttBrokerService.getConnectionFromClientID(mqttClientId)
assert connection != null
}
when: "a client subscribes to the correct data topic"
client.addMessageConsumer(correctTopic1, { _ -> return });

then: "A subscription should exist"
conditions.eventually {
assert client.topicConsumerMap.get(correctTopic1) != null
assert handler.connectionSubscriberInfoMap.containsKey(getTELTONIKA_DEVICE_IMEI());
}

when: "the JSON with all the payloads is parsed"
def slurp = new JsonSlurper()
ArrayList<Object> payloads = slurp.parseText(getClass().getResource("/teltonika/SortedPayloads.json").text) as ArrayList<Object>;
then: "Assert that the payloads are correct"
payloads.stream().forEach { Object payload ->
getLOG().debug(payload.toString())
}

when: "the device starts publishing payloads"
payloads.stream().forEach { Object payload ->
// Your logic here, for example:
getLOG().debug(JsonOutput.toJson(payload));

client.sendMessage(new MQTTMessage<String>(correctTopic1, JsonOutput.toJson(payload)))
sleep(100);
}
then: "it's done"
sleep(1000)



cleanup: "disconnect client from broker"
client.disconnect()
client.removeAllMessageConsumers();


}

//TODO: Write a test for AssetStateDuration (Multiple trip payloads etc.)
}
}

0 comments on commit 9e9d721

Please sign in to comment.