/
JavaScriptMessageMapperRhinoBenchmarkTest.java
106 lines (88 loc) · 4.16 KB
/
JavaScriptMessageMapperRhinoBenchmarkTest.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
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.connectivity.mapping.javascript.benchmark;
import static org.assertj.core.api.Assertions.assertThat;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.connectivity.ExternalMessage;
import org.eclipse.ditto.protocoladapter.Adaptable;
import org.eclipse.ditto.protocoladapter.TopicPath;
import org.eclipse.ditto.services.connectivity.mapping.MessageMapper;
import org.junit.Test;
/**
* Simple Test executing the benchmark scenarios for
* {@link org.eclipse.ditto.services.connectivity.mapping.javascript.JavaScriptMessageMapperRhino}.
*/
public class JavaScriptMessageMapperRhinoBenchmarkTest {
private static final String MAPPING_INCOMING_NAMESPACE = "org.eclipse.ditto";
private static final String MAPPING_INCOMING_ID = "jmh-test";
@Test
public void simpleMapTextPayload() {
final Adaptable adaptable = runScenario(new SimpleMapTextPayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
assertThat(adaptable.getPayload().getValue()).contains(
JsonValue.of(SimpleMapTextPayloadToDitto.MAPPING_STRING));
}
@Test
public void test1DecodeBinaryPayloadToDitto() {
final Adaptable adaptable = runScenario(new Test1DecodeBinaryPayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
assertThat(adaptable.getPayload().getValue()).contains(JsonFactory.readFrom("{\"a\":11,\"b\":10,\"c\":99}"));
}
@Test
public void test2ParseJsonPayloadToDitto() {
final Adaptable adaptable = runScenario(new Test2ParseJsonPayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
assertThat(adaptable.getPayload().getValue()
.map(JsonValue::asObject)
.map(o -> o.getValue("attributes/manufacturer"))
.orElse(null)
).contains(JsonValue.of("myManufacturer"));
}
@Test
public void test3FormatJsonPayloadToDitto() {
final Adaptable adaptable = runScenario(new Test3FormatJsonPayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
}
@Test
public void test4ConstructJsonPayloadToDitto() {
final Adaptable adaptable = runScenario(new Test4ConstructJsonPayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
assertThat(adaptable.getPayload().getValue().map(JsonValue::asObject).map(o -> o.getValue("cc")).orElse(null))
.contains(JsonValue.of("xxx/456/yyy"));
}
@Test
public void test5MustacheTemplatePayloadToDitto() {
final Adaptable adaptable = runScenario(new Test5MustacheTemplatePayloadToDitto());
System.out.println(adaptable);
assertDefaults(adaptable);
assertThat(adaptable.getPayload().getValue()).contains(
JsonValue.of("Joe spends 6"));
}
private Adaptable runScenario(final MapToDittoProtocolScenario scenario) {
final MessageMapper messageMapper = scenario.getMessageMapper();
final ExternalMessage externalMessage = scenario.getExternalMessage();
return messageMapper.map(externalMessage).get();
}
private static void assertDefaults(final Adaptable adaptable) {
assertThat(adaptable.getTopicPath().getChannel()).isEqualTo(TopicPath.Channel.TWIN);
assertThat(adaptable.getTopicPath().getCriterion()).isEqualTo(TopicPath.Criterion.COMMANDS);
assertThat(adaptable.getTopicPath().getAction()).contains(TopicPath.Action.MODIFY);
assertThat(adaptable.getTopicPath().getNamespace()).isEqualTo(MAPPING_INCOMING_NAMESPACE);
assertThat(adaptable.getTopicPath().getId()).isEqualTo(MAPPING_INCOMING_ID);
}
}