Skip to content

Commit 7bae1e7

Browse files
committed
Add a test for DsStorageRules
1 parent 3fcc7d7 commit 7bae1e7

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 2019, TeamDev. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package io.spine.server.storage.datastore.type;
22+
23+
import com.google.cloud.datastore.Blob;
24+
import com.google.cloud.datastore.BlobValue;
25+
import com.google.cloud.datastore.BooleanValue;
26+
import com.google.cloud.datastore.DoubleValue;
27+
import com.google.cloud.datastore.LongValue;
28+
import com.google.cloud.datastore.NullValue;
29+
import com.google.cloud.datastore.StringValue;
30+
import com.google.cloud.datastore.TimestampValue;
31+
import com.google.cloud.datastore.Value;
32+
import com.google.protobuf.ByteString;
33+
import com.google.protobuf.Timestamp;
34+
import io.spine.base.Time;
35+
import io.spine.core.Version;
36+
import io.spine.server.entity.storage.ColumnStorageRule;
37+
import io.spine.server.entity.storage.ColumnStorageRules;
38+
import io.spine.string.Stringifiers;
39+
import io.spine.test.storage.Project;
40+
import io.spine.test.storage.Project.Status;
41+
import org.junit.jupiter.api.DisplayName;
42+
import org.junit.jupiter.api.Nested;
43+
import org.junit.jupiter.api.Test;
44+
45+
import static com.google.cloud.Timestamp.ofTimeSecondsAndNanos;
46+
import static com.google.common.truth.Truth.assertThat;
47+
import static java.nio.charset.StandardCharsets.UTF_8;
48+
49+
@DisplayName("`DsStorageRules` should")
50+
class DsStorageRulesTest {
51+
52+
private final ColumnStorageRules<Value<?>> storageRules = new DsStorageRules();
53+
54+
@Nested
55+
@DisplayName("persist")
56+
class Persist {
57+
58+
@Test
59+
@DisplayName("`String` as `StringValue`")
60+
void stringAsStringValue() {
61+
String str = "test-string";
62+
assertConverts(str, StringValue.of(str));
63+
}
64+
65+
@Test
66+
@DisplayName("`Integer` as `LongValue`")
67+
void integerAsLongValue() {
68+
int num = 42;
69+
assertConverts(num, LongValue.of(num));
70+
}
71+
72+
@Test
73+
@DisplayName("`Long` as `LongValue`")
74+
void longAsLongValue() {
75+
long num = 42L;
76+
assertConverts(num, LongValue.of(num));
77+
}
78+
79+
@Test
80+
@DisplayName("`Float` as `DoubleValue`")
81+
void floatAsDoubleValue() {
82+
float num = 42.0F;
83+
assertConverts(num, DoubleValue.of(num));
84+
}
85+
86+
@Test
87+
@DisplayName("`Double` as `DoubleValue`")
88+
void doubleAsDoubleValue() {
89+
double num = 42.0;
90+
assertConverts(num, DoubleValue.of(num));
91+
}
92+
93+
@Test
94+
@DisplayName("`Boolean` as `BooleanValue`")
95+
void booleanAsBooleanValue() {
96+
boolean value = false;
97+
assertConverts(value, BooleanValue.of(value));
98+
}
99+
100+
@Test
101+
@DisplayName("`ByteString` as `BlobValue`")
102+
void byteStringAsBlobValue() {
103+
String str = "some-test-string";
104+
ByteString bytes = ByteString.copyFromUtf8(str);
105+
Blob blob = Blob.copyFrom(str.getBytes(UTF_8));
106+
BlobValue blobValue = BlobValue.of(blob);
107+
assertConverts(bytes, blobValue);
108+
}
109+
110+
@Test
111+
@DisplayName("`Enum` as `LongValue`")
112+
void enumAsLongValue() {
113+
Status value = Status.CREATED;
114+
assertConverts(value, LongValue.of(value.ordinal()));
115+
}
116+
117+
@Test
118+
@DisplayName("generic `Message` as `StringValue`")
119+
void messageAsStringValue() {
120+
Project project = Project
121+
.newBuilder()
122+
.setName("project-name")
123+
.build();
124+
String messageAsString = Stringifiers.toString(project);
125+
assertConverts(project, StringValue.of(messageAsString));
126+
}
127+
128+
@Test
129+
@DisplayName("`Timestamp` as `TimestampValue`")
130+
void timestampAsTimestampValue() {
131+
Timestamp timestamp = Time.currentTime();
132+
assertConverts(timestamp, TimestampValue.of(
133+
ofTimeSecondsAndNanos(timestamp.getSeconds(), timestamp.getNanos())
134+
));
135+
}
136+
137+
@Test
138+
@DisplayName("`Version` as `LongValue`")
139+
void versionAsLongValue() {
140+
int number = 42;
141+
Version version = Version
142+
.newBuilder()
143+
.setNumber(number)
144+
.build();
145+
assertConverts(version, LongValue.of(number));
146+
}
147+
148+
@Test
149+
@DisplayName("`null` as `NullValue`")
150+
void nullAsNullValue() {
151+
Value<?> persistedNull = storageRules.ofNull()
152+
.applyTo(null);
153+
assertThat(persistedNull).isEqualTo(NullValue.of());
154+
}
155+
156+
private void assertConverts(Object original, Value<?> expected) {
157+
ColumnStorageRule<?, ? extends Value<?>> rule = storageRules.of(original.getClass());
158+
Value<?> result = rule.applyTo(original);
159+
assertThat(result).isEqualTo(expected);
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)