-
Notifications
You must be signed in to change notification settings - Fork 219
/
ReferencePlaceholderTest.java
85 lines (70 loc) · 3.95 KB
/
ReferencePlaceholderTest.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
/*
* Copyright (c) 2017-2018 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
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.concierge.enforcement.placeholders.references;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.util.Optional;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.assertions.DittoJsonAssertions;
import org.eclipse.ditto.signals.commands.base.exceptions.GatewayPlaceholderReferenceNotSupportedException;
import org.junit.Test;
/**
* Tests {@link ReferencePlaceholder}.
*/
public class ReferencePlaceholderTest {
@Test
public void fromCharSequence() {
final Optional<ReferencePlaceholder> referencePlaceholder =
ReferencePlaceholder.fromCharSequence("{{ref:things/namespace:thingid/policyId}}");
assertThat(referencePlaceholder).isPresent();
assertThat(referencePlaceholder.get().getReferencedEntityId()).isEqualTo("namespace:thingid");
DittoJsonAssertions.assertThat(referencePlaceholder.get().getReferencedField())
.isEqualTo( JsonPointer.of("policyId"));
assertThat(referencePlaceholder.get().getReferencedEntityType()).isEqualTo(
ReferencePlaceholder.ReferencedEntityType.THINGS);
}
@Test
public void fromCharSequenceWithSpacesAfterBraces() {
final Optional<ReferencePlaceholder> referencePlaceholder =
ReferencePlaceholder.fromCharSequence("{{ ref:things/namespace:thingid/policyId }}");
assertThat(referencePlaceholder).isPresent();
assertThat(referencePlaceholder.get().getReferencedEntityId()).isEqualTo("namespace:thingid");
DittoJsonAssertions.assertThat(referencePlaceholder.get().getReferencedField())
.isEqualTo( JsonPointer.of("policyId"));
assertThat(referencePlaceholder.get().getReferencedEntityType()).isEqualTo(
ReferencePlaceholder.ReferencedEntityType.THINGS);
}
@Test
public void fromCharSequenceWithInvalidPlaceholderIsEmpty() {
assertThat(ReferencePlaceholder.fromCharSequence("{{things/namespace:thingid/policyId}}")).isNotPresent();
assertThat(ReferencePlaceholder.fromCharSequence("{{ref:thingsnamespace:thingid/policyId}}")).isNotPresent();
assertThat(ReferencePlaceholder.fromCharSequence("{{ref:things/ namespace:thingid/policyId}}")).isNotPresent();
assertThat(ReferencePlaceholder.fromCharSequence("{{ref:things/namespace:thingid}}")).isNotPresent();
assertThat(ReferencePlaceholder.fromCharSequence(null)).isNotPresent();
}
@Test
public void fromCharSequenceWithDeepPointer() {
final Optional<ReferencePlaceholder> referencePlaceholder =
ReferencePlaceholder.fromCharSequence(
"{{ref:things/namespace:thingid/features/properties/policyFeature/policyId}}");
assertThat(referencePlaceholder).isPresent();
assertThat(referencePlaceholder.get().getReferencedEntityId()).isEqualTo("namespace:thingid");
DittoJsonAssertions.assertThat(referencePlaceholder.get().getReferencedField()).isEqualTo(
JsonPointer.of("features/properties/policyFeature/policyId"));
assertThat(referencePlaceholder.get().getReferencedEntityType()).isEqualTo(
ReferencePlaceholder.ReferencedEntityType.THINGS);
}
@Test
public void fromCharSequenceWithUnsupportedEntityTypeThrowsException() {
assertThatThrownBy(() -> ReferencePlaceholder.fromCharSequence("{{ref:topologies/namespace:thingid/policyId}}"))
.isInstanceOf(GatewayPlaceholderReferenceNotSupportedException.class);
}
}