-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPrimitivesToIdentifierConverterUnitTests.java
More file actions
139 lines (105 loc) · 5.24 KB
/
Copy pathPrimitivesToIdentifierConverterUnitTests.java
File metadata and controls
139 lines (105 loc) · 5.24 KB
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
/*
* Copyright 2021-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jmolecules.spring;
import static org.assertj.core.api.Assertions.*;
import lombok.Value;
import java.util.UUID;
import org.jmolecules.ddd.types.Identifier;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.convert.ConverterNotFoundException;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
class PrimitivesToIdentifierConverterUnitTests {
static final TypeDescriptor UUID_DESCRIPTOR = TypeDescriptor.valueOf(UUID.class);
static final TypeDescriptor STRING_DESCRIPTOR = TypeDescriptor.valueOf(String.class);
static final TypeDescriptor INT_DESCRIPTOR = TypeDescriptor.valueOf(int.class);
static final TypeDescriptor UUID_IDENTIFIER_DESCRIPTOR = TypeDescriptor.valueOf(SampleIdentifier.class);
static final TypeDescriptor INT_IDENTIFIER_DESCRIPTOR = TypeDescriptor.valueOf(SampleIntegerIdentifier.class);
ConfigurableConversionService conversionService;
PrimitivesToIdentifierConverter converter;
@BeforeEach
void setUp() {
this.conversionService = new DefaultConversionService();
this.converter = new PrimitivesToIdentifierConverter(() -> conversionService);
this.conversionService.addConverter(converter);
}
@Test // #16
void convertsToUuidIdentifierFromPrimitiveSources() {
UUID uuid = UUID.randomUUID();
assertThat(conversionService.convert(uuid, SampleIdentifier.class).getId()).isEqualTo(uuid);
assertThat(conversionService.convert(uuid.toString(), SampleIdentifier.class).getId()).isEqualTo(uuid);
assertThatExceptionOfType(ConverterNotFoundException.class)
.isThrownBy(() -> converter.convert(1L, INT_DESCRIPTOR, UUID_IDENTIFIER_DESCRIPTOR));
}
@Test // #16
void convertsToIntegerIdentifierFromPrimitiveSources() {
int id = 1;
assertThat(conversionService.convert(id, SampleIntegerIdentifier.class).getId()).isEqualTo(id);
assertThat(conversionService.convert(Integer.valueOf(id), SampleIntegerIdentifier.class).getId()).isEqualTo(id);
assertThat(conversionService.convert(Long.valueOf(id), SampleIntegerIdentifier.class).getId()).isEqualTo(id);
assertThat(conversionService.convert(Long.valueOf(id).longValue(), SampleIntegerIdentifier.class).getId()).isEqualTo(id);
UUID uuid = UUID.randomUUID();
assertThatExceptionOfType(ConverterNotFoundException.class)
.isThrownBy(() -> converter.convert(uuid, UUID_DESCRIPTOR, INT_IDENTIFIER_DESCRIPTOR));
}
@Test // #16
void matchesPrimitives() {
assertThat(converter.matches(UUID_DESCRIPTOR, UUID_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(STRING_DESCRIPTOR, UUID_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(INT_DESCRIPTOR, INT_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(TypeDescriptor.valueOf(Integer.class), INT_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(TypeDescriptor.valueOf(long.class), INT_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(TypeDescriptor.valueOf(Long.class), INT_IDENTIFIER_DESCRIPTOR)).isTrue();
assertThat(converter.matches(INT_DESCRIPTOR, UUID_IDENTIFIER_DESCRIPTOR)).isFalse();
assertThat(converter.matches(UUID_DESCRIPTOR, INT_IDENTIFIER_DESCRIPTOR)).isFalse();
}
@Test // #16
void handlesNullValues() {
assertThat(converter.convert(null, UUID_DESCRIPTOR, UUID_IDENTIFIER_DESCRIPTOR)).isNull();
}
@Test // #46
void handlesIdentifierWithoutFactoryMethod() {
TypeDescriptor descriptor = TypeDescriptor.valueOf(IdentifierWithoutFactoryMethod.class);
assertThat(converter.matches(STRING_DESCRIPTOR, descriptor)).isTrue();
assertThat(converter.matches(UUID_DESCRIPTOR, descriptor)).isTrue();
assertThat(converter.matches(TypeDescriptor.valueOf(Long.class), descriptor)).isFalse();
UUID uuid = UUID.randomUUID();
IdentifierWithoutFactoryMethod expected = new IdentifierWithoutFactoryMethod(uuid);
assertThat(converter.convert(uuid, UUID_DESCRIPTOR, descriptor)).isEqualTo(expected);
assertThat(converter.convert(uuid.toString(), STRING_DESCRIPTOR, descriptor)).isEqualTo(expected);
}
@Test // GH-191
void doesNotUseFactoryMethodOfSupertypes() {
TypeDescriptor descriptor = TypeDescriptor.valueOf(Implementation.class);
assertThat(converter.convert("id", STRING_DESCRIPTOR, descriptor)).isEqualTo(new Implementation("id"));
}
@Value
static class IdentifierWithoutFactoryMethod implements Identifier {
UUID id;
}
// GH-191
interface WithFactoryMethod {
static Object of(String source) {
return source;
}
}
@Value
static class Implementation implements WithFactoryMethod, Identifier {
String id;
}
}