1
1
package com .hivemq .edge .adapters .s7 .config ;
2
2
3
3
import com .fasterxml .jackson .databind .ObjectMapper ;
4
- import com .hivemq .adapter .sdk .api .config .MqttUserProperty ;
5
4
import com .hivemq .adapter .sdk .api .factories .ProtocolAdapterFactoryInput ;
6
5
import com .hivemq .adapter .sdk .api .tag .Tag ;
7
6
import com .hivemq .configuration .entity .HiveMQConfigEntity ;
8
7
import com .hivemq .configuration .entity .adapter .ProtocolAdapterEntity ;
9
8
import com .hivemq .configuration .reader .ConfigFileReaderWriter ;
10
9
import com .hivemq .configuration .reader .ConfigurationFile ;
11
10
import com .hivemq .edge .adapters .s7 .S7ProtocolAdapterFactory ;
12
- import com .hivemq .protocols .AdapterConfigAndTags ;
11
+ import com .hivemq .protocols .ProtocolAdapterConfig ;
12
+ import com .hivemq .protocols .ProtocolAdapterConfigConverter ;
13
+ import com .hivemq .protocols .ProtocolAdapterFactoryManager ;
13
14
import org .jetbrains .annotations .NotNull ;
14
15
import org .junit .jupiter .api .Test ;
15
16
16
17
import java .io .File ;
18
+ import java .net .URISyntaxException ;
17
19
import java .net .URL ;
18
20
import java .nio .file .Path ;
19
21
import java .util .List ;
20
22
import java .util .Map ;
23
+ import java .util .Optional ;
21
24
22
- import static com .hivemq .adapter .sdk .api .config .MessageHandlingOptions .MQTTMessagePerSubscription ;
23
- import static com .hivemq .adapter .sdk .api .config .MessageHandlingOptions .MQTTMessagePerTag ;
24
- import static com .hivemq .edge .adapters .s7 .S7ProtocolAdapterInformation .PROTOCOL_ID ;
25
25
import static com .hivemq .protocols .ProtocolAdapterUtils .createProtocolAdapterMapper ;
26
26
import static org .assertj .core .api .Assertions .assertThat ;
27
27
import static org .mockito .Mockito .mock ;
@@ -34,51 +34,21 @@ class S7AdapterConfigTest {
34
34
@ Test
35
35
public void convertConfigObject_fullConfig_valid () throws Exception {
36
36
final URL resource = getClass ().getResource ("/s7-adapter-full-config.xml" );
37
- final File path = Path .of (resource .toURI ()).toFile ();
38
-
39
- final HiveMQConfigEntity configEntity = loadConfig (path );
40
- final ProtocolAdapterEntity adapter = configEntity .getProtocolAdapterConfig ().get (0 );
41
-
42
- final ProtocolAdapterFactoryInput mockInput = mock (ProtocolAdapterFactoryInput .class );
43
- when (mockInput .isWritingEnabled ()).thenReturn (false );
44
- final S7ProtocolAdapterFactory s7ProtocolAdapterFactory = new S7ProtocolAdapterFactory (mockInput );
45
37
46
- final AdapterConfigAndTags adapterConfigAndTags =
47
- AdapterConfigAndTags .fromAdapterConfigMap ((Map <String , Object >) adapters .get (PROTOCOL_ID ),
48
- false ,
49
- mapper ,
50
- s7ProtocolAdapterFactory );
38
+ final ProtocolAdapterConfig protocolAdapterConfig = getProtocolAdapterConfig (resource );
51
39
52
- final S7AdapterConfig config = (S7AdapterConfig ) adapterConfigAndTags .getAdapterConfig ();
40
+ final S7AdapterConfig config = (S7AdapterConfig ) protocolAdapterConfig .getAdapterConfig ();
41
+ assertThat (protocolAdapterConfig .missingTags ())
42
+ .isEmpty ();
53
43
54
- assertThat (config .getId ()).isEqualTo ("my-s7-protocol-adapter" );
55
44
assertThat (config .getPort ()).isEqualTo (1234 );
56
45
assertThat (config .getHost ()).isEqualTo ("my.s7-server.com" );
57
46
assertThat (config .getControllerType ()).isEqualTo (S7AdapterConfig .ControllerType .S7_400 );
58
- assertThat (config .getPollingIntervalMillis ()).isEqualTo (10 );
59
- assertThat (config .getMaxPollingErrorsBeforeRemoval ()).isEqualTo (9 );
60
- assertThat (config .getPublishChangedDataOnly ()).isFalse ();
61
- assertThat (config .getS7ToMqttMappings ()).satisfiesExactly (mapping -> {
62
- assertThat (mapping .getMqttTopic ()).isEqualTo ("my/topic" );
63
- assertThat (mapping .getMqttQos ()).isEqualTo (1 );
64
- assertThat (mapping .getMessageHandlingOptions ()).isEqualTo (MQTTMessagePerSubscription );
65
- assertThat (mapping .getIncludeTimestamp ()).isTrue ();
66
- assertThat (mapping .getIncludeTagNames ()).isTrue ();
67
- assertThat (mapping .getTagName ()).isEqualTo ("tag-name" );
68
-
69
- }, mapping -> {
70
- assertThat (mapping .getMqttTopic ()).isEqualTo ("my/topic/2" );
71
- assertThat (mapping .getMqttQos ()).isEqualTo (1 );
72
- assertThat (mapping .getMessageHandlingOptions ()).isEqualTo (MQTTMessagePerSubscription );
73
- assertThat (mapping .getIncludeTimestamp ()).isTrue ();
74
- assertThat (mapping .getIncludeTagNames ()).isTrue ();
75
- assertThat (mapping .getTagName ()).isEqualTo ("tag-name" );
76
- });
47
+ assertThat (config .getS7ToMqttConfig ().getPollingIntervalMillis ()).isEqualTo (10 );
48
+ assertThat (config .getS7ToMqttConfig ().getMaxPollingErrorsBeforeRemoval ()).isEqualTo (9 );
49
+ assertThat (config .getS7ToMqttConfig ().getPublishChangedDataOnly ()).isFalse ();
77
50
78
-
79
- assertThat (adapterConfigAndTags .missingTags ()).isEmpty ();
80
-
81
- assertThat (adapterConfigAndTags .getTags ())
51
+ assertThat (protocolAdapterConfig .getTags ())
82
52
.allSatisfy (t -> {
83
53
assertThat (t )
84
54
.isInstanceOf (S7Tag .class )
@@ -90,44 +60,21 @@ public void convertConfigObject_fullConfig_valid() throws Exception {
90
60
@ Test
91
61
public void convertConfigObject_defaults_valid () throws Exception {
92
62
final URL resource = getClass ().getResource ("/s7-adapter-minimal-config.xml" );
93
- final File path = Path .of (resource .toURI ()).toFile ();
94
-
95
- final HiveMQConfigEntity configEntity = loadConfig (path );
96
- final Map <String , Object > adapters = configEntity .getProtocolAdapterConfig ();
97
-
98
- final ProtocolAdapterFactoryInput mockInput = mock (ProtocolAdapterFactoryInput .class );
99
- when (mockInput .isWritingEnabled ()).thenReturn (false );
100
- final S7ProtocolAdapterFactory s7ProtocolAdapterFactory = new S7ProtocolAdapterFactory (mockInput );
101
-
102
- final AdapterConfigAndTags adapterConfigAndTags =
103
- AdapterConfigAndTags .fromAdapterConfigMap ((Map <String , Object >) adapters .get (PROTOCOL_ID ),
104
- false ,
105
- mapper ,
106
- s7ProtocolAdapterFactory );
63
+ final ProtocolAdapterConfig protocolAdapterConfig = getProtocolAdapterConfig (resource );
107
64
108
- final S7AdapterConfig config = (S7AdapterConfig ) adapterConfigAndTags .getAdapterConfig ();
65
+ final S7AdapterConfig config = (S7AdapterConfig ) protocolAdapterConfig .getAdapterConfig ();
66
+ assertThat (protocolAdapterConfig .missingTags ())
67
+ .isEmpty ();
109
68
110
69
assertThat (config ).isNotNull ();
111
- assertThat (config .getId ()).isEqualTo ("my-s7-protocol-adapter" );
112
70
assertThat (config .getPort ()).isEqualTo (1234 );
113
71
assertThat (config .getHost ()).isEqualTo ("my.s7-server.com" );
114
72
assertThat (config .getControllerType ()).isEqualTo (S7AdapterConfig .ControllerType .S7_400 );
115
- assertThat (config .getPollingIntervalMillis ()).isEqualTo (1000 );
116
- assertThat (config .getMaxPollingErrorsBeforeRemoval ()).isEqualTo (10 );
117
- assertThat (config .getPublishChangedDataOnly ()).isTrue ();
118
- assertThat (config .getS7ToMqttMappings ()).satisfiesExactly (mapping -> {
119
- assertThat (mapping .getMqttTopic ()).isEqualTo ("my/topic" );
120
- assertThat (mapping .getMqttQos ()).isEqualTo (0 );
121
- assertThat (mapping .getMessageHandlingOptions ()).isEqualTo (MQTTMessagePerTag );
122
- assertThat (mapping .getIncludeTimestamp ()).isTrue ();
123
- assertThat (mapping .getIncludeTagNames ()).isFalse ();
124
- assertThat (mapping .getTagName ()).isEqualTo ("tag-name" );
125
- });
126
-
73
+ assertThat (config .getS7ToMqttConfig ().getPollingIntervalMillis ()).isEqualTo (1000 );
74
+ assertThat (config .getS7ToMqttConfig ().getMaxPollingErrorsBeforeRemoval ()).isEqualTo (10 );
75
+ assertThat (config .getS7ToMqttConfig ().getPublishChangedDataOnly ()).isTrue ();
127
76
128
- assertThat (adapterConfigAndTags .missingTags ()).isEmpty ();
129
-
130
- assertThat (adapterConfigAndTags .getTags ())
77
+ assertThat (protocolAdapterConfig .getTags ())
131
78
.allSatisfy (t -> {
132
79
assertThat (t )
133
80
.isInstanceOf (S7Tag .class )
@@ -138,26 +85,21 @@ public void convertConfigObject_defaults_valid() throws Exception {
138
85
139
86
@ Test
140
87
public void unconvertConfigObject_full_valid () {
141
- final S7ToMqttConfig pollingContext = new S7ToMqttConfig ("my/destination" ,
88
+ final S7ToMqttConfig pollingContext = new S7ToMqttConfig (
89
+ 3000 ,
142
90
1 ,
143
- MQTTMessagePerSubscription ,
144
- false ,
145
- true ,
146
- "tag-name" ,
147
- List .of (new MqttUserProperty ("my-name" , "my-value" ))
91
+ false
148
92
);
149
93
150
- final S7AdapterConfig s7AdapterConfig = new S7AdapterConfig ("my-s7-adapter" ,
94
+ final S7AdapterConfig s7AdapterConfig = new S7AdapterConfig (
95
+ "my-s7-adapter" ,
151
96
14 ,
152
97
"my.host.com" ,
153
98
S7AdapterConfig .ControllerType .S7_1500 ,
154
99
1 ,
155
100
2 ,
156
101
3 ,
157
- 4 ,
158
- 5 ,
159
- false ,
160
- List .of (pollingContext ));
102
+ pollingContext );
161
103
162
104
final ProtocolAdapterFactoryInput mockInput = mock (ProtocolAdapterFactoryInput .class );
163
105
when (mockInput .isWritingEnabled ()).thenReturn (false );
@@ -191,6 +133,29 @@ public void unconvertConfigObject_full_valid() {
191
133
});
192
134
}
193
135
136
+ private @ NotNull ProtocolAdapterConfig getProtocolAdapterConfig (final @ NotNull URL resource ) throws
137
+ URISyntaxException {
138
+ final File path = Path .of (resource .toURI ()).toFile ();
139
+
140
+ final HiveMQConfigEntity configEntity = loadConfig (path );
141
+ final ProtocolAdapterEntity adapterEntity = configEntity .getProtocolAdapterConfig ().get (0 );
142
+
143
+ final ProtocolAdapterConfigConverter converter = createConverter ();
144
+
145
+ return converter .fromEntity (adapterEntity );
146
+ }
147
+
148
+ private @ NotNull ProtocolAdapterConfigConverter createConverter () {
149
+ final ProtocolAdapterFactoryInput mockInput = mock (ProtocolAdapterFactoryInput .class );
150
+ when (mockInput .isWritingEnabled ()).thenReturn (true );
151
+
152
+ S7ProtocolAdapterFactory protocolAdapterFactory = new S7ProtocolAdapterFactory (mockInput );
153
+ ProtocolAdapterFactoryManager manager = mock (ProtocolAdapterFactoryManager .class );
154
+ when (manager .get ("s7-new" )).thenReturn (Optional .of (protocolAdapterFactory ));
155
+ ProtocolAdapterConfigConverter converter = new ProtocolAdapterConfigConverter (manager , mapper );
156
+ return converter ;
157
+ }
158
+
194
159
private @ NotNull HiveMQConfigEntity loadConfig (final @ NotNull File configFile ) {
195
160
final ConfigFileReaderWriter readerWriter = new ConfigFileReaderWriter (new ConfigurationFile (configFile ),
196
161
mock (),
0 commit comments