/
SendTimestampedTest.java
144 lines (126 loc) · 6.16 KB
/
SendTimestampedTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*******************************************************************************
* Copyright (c) 2021 Orange.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* and Eclipse Distribution License v1.0 which accompany this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v20.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.html.
*
* Contributors:
* Orange - Send with multiple-timestamped values
*******************************************************************************/
package org.eclipse.leshan.integration.tests.send;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing;
import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.Instant;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.eclipse.leshan.client.send.ManualDataSender;
import org.eclipse.leshan.client.servers.LwM2mServer;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.model.ResourceModel.Type;
import org.eclipse.leshan.core.node.LwM2mChildNode;
import org.eclipse.leshan.core.node.LwM2mNode;
import org.eclipse.leshan.core.node.LwM2mPath;
import org.eclipse.leshan.core.node.LwM2mSingleResource;
import org.eclipse.leshan.core.node.TimestampedLwM2mNodes;
import org.eclipse.leshan.core.request.ContentFormat;
import org.eclipse.leshan.core.util.TestLwM2mId;
import org.eclipse.leshan.integration.tests.util.LeshanTestClient;
import org.eclipse.leshan.integration.tests.util.LeshanTestServer;
import org.eclipse.leshan.integration.tests.util.junit5.extensions.ArgumentsUtil;
import org.eclipse.leshan.integration.tests.util.junit5.extensions.BeforeEachParameterizedResolver;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
@ExtendWith(BeforeEachParameterizedResolver.class)
public class SendTimestampedTest {
/*---------------------------------/
* Parameterized Tests
* -------------------------------*/
@ParameterizedTest(name = "{0} over {1} - Client using {2} - Server using {3}")
@MethodSource("transports")
@Retention(RetentionPolicy.RUNTIME)
private @interface TestAllCases {
}
static Stream<Arguments> transports() {
Object[][] transports = new Object[][] {
// ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider
{ Protocol.COAP, "Californium", "Californium" }, //
{ Protocol.COAP, "Californium", "java-coap" }, //
{ Protocol.COAP, "java-coap", "Californium" }, //
{ Protocol.COAP, "java-coap", "java-coap" } };
Object[] contentFormats = new Object[] { //
ContentFormat.SENML_JSON, //
ContentFormat.SENML_CBOR };
// for each transport, create 1 test by format.
return Stream.of(ArgumentsUtil.combine(contentFormats, transports));
}
/*---------------------------------/
* Set-up and Tear-down Tests
* -------------------------------*/
LeshanTestServer server;
LeshanTestClient client;
@BeforeEach
public void start(ContentFormat format, Protocol givenProtocol, String givenClientEndpointProvider,
String givenServerEndpointProvider) {
server = givenServerUsing(givenProtocol).with(givenServerEndpointProvider).build();
server.start();
client = givenClientUsing(givenProtocol).with(givenClientEndpointProvider).connectingTo(server).build();
client.start();
server.waitForNewRegistrationOf(client);
client.waitForRegistrationTo(server);
}
@AfterEach
public void stop() throws InterruptedException {
if (client != null)
client.destroy(false);
if (server != null)
server.destroy();
}
/*---------------------------------/
* Tests
* -------------------------------*/
@TestAllCases
public void server_handle_multiple_timestamped_node(ContentFormat contentFormat, Protocol givenProtocol,
String givenClientEndpointProvider, String givenServerEndpointProvider)
throws InterruptedException, TimeoutException {
// Send Data
LwM2mServer registeredServer = client.getRegisteredServers().values().iterator().next();
ManualDataSender sender = client.getSendService().getDataSender(ManualDataSender.DEFAULT_NAME,
ManualDataSender.class);
sender.collectData(Arrays.asList(getExamplePath()));
Thread.sleep(1000);
sender.collectData(Arrays.asList(getExamplePath()));
sender.sendCollectedData(registeredServer, contentFormat, 1000, false);
TimestampedLwM2mNodes data = server.waitForData(client.getEndpointName(), 1, TimeUnit.SECONDS);
// Verify SendListener data received
assertThat(data.getTimestamps()).hasSize(2);
for (Instant ts : data.getTimestamps()) {
assertNotNull(ts);
Map<LwM2mPath, LwM2mNode> pathNodeMap = data.getNodesAt(ts);
assertThat(pathNodeMap).containsKey(getExamplePath());
LwM2mChildNode node = (LwM2mChildNode) pathNodeMap.get(getExamplePath());
assertThat(node.getId()).isEqualTo(TestLwM2mId.FLOAT_VALUE);
assertThat(node).isInstanceOfSatisfying(LwM2mSingleResource.class,
n -> assertThat(n.getType()).isEqualTo(Type.FLOAT));
}
}
private static LwM2mPath getExamplePath() {
return new LwM2mPath(TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.FLOAT_VALUE);
}
}