/
MessageMapperFactoryTest.java
229 lines (185 loc) · 8.32 KB
/
MessageMapperFactoryTest.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
* 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.amqpbridge.mapping.mapper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import org.eclipse.ditto.model.amqpbridge.AmqpBridgeModelFactory;
import org.eclipse.ditto.model.amqpbridge.MappingContext;
import org.eclipse.ditto.services.amqpbridge.mapping.mapper.test.Mappers;
import org.eclipse.ditto.services.amqpbridge.mapping.mapper.test.MappingContexts;
import org.eclipse.ditto.services.amqpbridge.mapping.mapper.test.MockMapper;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import akka.actor.ActorSystem;
import akka.actor.ExtendedActorSystem;
import akka.event.DiagnosticLoggingAdapter;
import akka.testkit.javadsl.TestKit;
public class MessageMapperFactoryTest {
private static ActorSystem system;
private DefaultMessageMapperFactory factory;
@BeforeClass
public static void setup() {
system = ActorSystem.create();
}
@AfterClass
public static void teardown() {
TestKit.shutdownActorSystem(system);
system = null;
}
@Before
public void setUp() {
DiagnosticLoggingAdapter log = mock(DiagnosticLoggingAdapter.class);
factory = DefaultMessageMapperFactory.of(((ExtendedActorSystem) system).dynamicAccess(), Mappers.class, log);
}
@After
public void tearDown() {
factory = null;
}
@Test
public void loadMapperFromFactoryMethod() throws Exception {
final String contentType = "test";
final boolean isContentTypeRequired = true;
final Map<String, String> options = new HashMap<>();
// add this to pass configuration checks
options.put(MockMapper.OPT_IS_VALID, String.valueOf(true));
// looking for a method containing 'test' and returning a MessageMapper in Mappers.class
final MappingContext ctx = MappingContexts.mock(contentType, "test", options);
final Optional<MessageMapper> underTest = factory.mapperOf(ctx);
assertThat(underTest).isPresent();
assertThat(underTest.get().getContentType().get()).isEqualTo(contentType);
}
@Test
public void loadMapperFromClass() throws Exception {
final String contentType = "test";
final boolean isContentTypeRequired = true;
final MappingContext ctx = MappingContexts.mock(contentType, true);
final Optional<MessageMapper> underTest = factory.mapperOf(ctx);
assertThat(underTest).isPresent();
assertThat(underTest.get().getContentType().get()).isEqualTo(contentType);
}
@Test
public void loadMissingMapper() throws Exception {
final MappingContext ctx = MappingContexts.mock("test", "not-a-class",
Collections.emptyMap());
assertThat(factory.mapperOf(ctx)).isEmpty();
}
@Test
public void loadNonMessageMapperClass() {
final MappingContext ctx = MappingContexts.mock("test", String.class.getCanonicalName(),
Collections.emptyMap());
assertThat(factory.mapperOf(ctx)).isEmpty();
}
@Test
public void createWithFactoryMethod() throws Exception {
// looking for a method containing 'test' and returning a MessageMapper in Mappers.class
final MappingContext ctx = MappingContexts.mock("test", "test", Collections.emptyMap());
assertThat(factory.findFactoryMethodAndCreateInstance(ctx)).isPresent();
}
@Test
public void createWithFactoryMethodFindsNoMethod() throws Exception {
// looking for a message containing 'strong-smell-wasabi' which does not exist in Mappers.class and therefore
// expect an empty optional
final MappingContext ctx = MappingContexts.mock("test", "strong-smell-wasabi",
Collections.emptyMap());
assertThat(factory.findFactoryMethodAndCreateInstance(ctx)).isEmpty();
}
@Test
public void createWithClassName() throws Exception {
// MockMapper extends MessageMapper and can be loaded
final MappingContext ctx = MappingContexts.mock("test", MockMapper.class,
Collections.emptyMap());
assertThat(factory.findClassAndCreateInstance(ctx)).isPresent();
}
@Test
public void createWithClassNameFindsNoClass() throws Exception {
final MappingContext ctx = MappingContexts.mock("test", "not-a-class",
Collections.emptyMap());
assertThat(factory.findClassAndCreateInstance(ctx)).isEmpty();
}
@Test
public void createWithClassNameFailsForNonMapperClass() {
// load string as a MessageMapper -> should fail
final MappingContext ctx = MappingContexts.mock("test", String.class.getCanonicalName(),
Collections.emptyMap());
assertThatExceptionOfType(ClassCastException.class).isThrownBy(
() -> factory.findClassAndCreateInstance(ctx));
}
@Test
public void loadMapperWithInvalidConfig() {
final String contentType = "test";
final MappingContext ctx = MappingContexts.mock(contentType, false);
assertThat(factory.mapperOf(ctx)).isEmpty();
}
@Test
public void loadMapperWithoutContentType() {
final String contentType = "test";
Map<String, String> opts = new HashMap<>();
final MappingContext ctx =
AmqpBridgeModelFactory.newMappingContext(contentType, MockMapper.class.getCanonicalName(), opts);
assertThat(factory.mapperOf(ctx)).isEmpty();
}
@Test
public void loadMappers() {
final List<MappingContext> contexts = Arrays.asList(
MappingContexts.mock("foo", true),
MappingContexts.mock("bar", true)
);
final List<MessageMapper> mappers = factory.mappersOf(contexts);
assertThat(mappers).isNotEmpty().hasSize(2);
assertThat(mappers.stream().map(MessageMapper::getContentType).map(Optional::get).collect(Collectors.toList()))
.contains("foo", "bar");
}
@Test
public void loadMappersWithFails() {
final List<MappingContext> contexts = Arrays.asList(
MappingContexts.mock("foo", false),
MappingContexts.mock("bar", true)
);
final List<MessageMapper> mappers = factory.mappersOf(contexts);
assertThat(mappers).isNotEmpty().hasSize(1);
assertThat(mappers.stream().map(MessageMapper::getContentType).map(Optional::get).collect(Collectors.toList())).contains("bar");
}
@Test
public void loadRegistry() {
final MappingContext fooCtx = MappingContexts.mock("foo", true);
final MappingContext barCtx = MappingContexts.mock("bar", true);
final String fooMessage = "foo";
final String barMessage = "bar";
final String otherMessage = "other";
final List<MappingContext> contexts = Arrays.asList(fooCtx, barCtx);
MessageMapperRegistry underTest = factory.registryOf(DittoMessageMapper.CONTEXT, contexts);
assertThat(underTest.getMappers().size()).isEqualTo(2);
assertThat(underTest.getDefaultMapper()).isEqualTo(new DittoMessageMapper());
assertThat(underTest.findMapper(fooMessage)).isPresent()
.map(MessageMapper::getContentType)
.map(Optional::get)
.isEqualTo(Optional.of("foo"));
assertThat(underTest.findMapper(barMessage)).isPresent()
.map(MessageMapper::getContentType)
.map(Optional::get)
.isEqualTo(Optional.of("bar"));
assertThat(underTest.findMapper(otherMessage)).isEmpty();
//select uses default mapper
assertThat(underTest.selectMapper(otherMessage)).isEqualTo(new DittoMessageMapper());
}
}