/
BooleanTemplate.java
165 lines (138 loc) · 8.17 KB
/
BooleanTemplate.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
/*
* Copyright 2019 Immutables Authors and Contributors
*
* 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
*
* http://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.immutables.criteria.typemodel;
import org.immutables.check.IterableChecker;
import org.immutables.criteria.backend.Backend;
import org.immutables.criteria.personmodel.CriteriaChecker;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import static org.immutables.check.Checkers.check;
/**
* Testing predicates, projections, sorting etc. on booleans
*/
public abstract class BooleanTemplate {
private final BooleanHolderRepository repository;
private final BooleanHolderCriteria holder;
private final Supplier<ImmutableBooleanHolder> generator;
protected BooleanTemplate(Backend backend) {
this.repository = new BooleanHolderRepository(backend);
this.holder = BooleanHolderCriteria.booleanHolder;
this.generator = TypeHolder.BooleanHolder.generator();
}
/**
* Check for empty results on empty backend
*/
@Test
void empty() {
ids(holder.value.isFalse()).isEmpty();
ids(holder.value.isTrue()).isEmpty();
ids(holder.value.is(true)).isEmpty();
ids(holder.value.is(true)).isEmpty();
ids(holder.value.is(false)).isEmpty();
ids(holder.nullable.isTrue()).isEmpty();
ids(holder.nullable.isFalse()).isEmpty();
ids(holder.optional.isTrue()).isEmpty();
ids(holder.optional.isFalse()).isEmpty();
}
@Test
void basic() {
repository.insert(generator.get().withId("id1").withValue(true));
repository.insert(generator.get().withId("id2").withValue(false));
ids(holder.value.is(true)).hasContentInAnyOrder("id1");
ids(holder.value.isTrue()).hasContentInAnyOrder("id1");
ids(holder.value.is(false)).hasContentInAnyOrder("id2");
ids(holder.value.isFalse()).hasContentInAnyOrder("id2");
}
@Test
protected void optional() {
repository.insert(generator.get().withId("id1").withValue(true).withOptional(false));
repository.insert(generator.get().withId("id2").withValue(false).withOptional(true));
repository.insert(generator.get().withId("id3").withValue(false).withOptional(Optional.empty()));
ids(holder.optional.isPresent()).hasContentInAnyOrder("id1", "id2");
ids(holder.optional.isAbsent()).hasContentInAnyOrder("id3");
ids(holder.optional.is(true)).hasContentInAnyOrder("id2");
ids(holder.optional.isTrue()).hasContentInAnyOrder("id2");
ids(holder.optional.is(false)).hasContentInAnyOrder("id1");
ids(holder.optional.isFalse()).hasContentInAnyOrder("id1");
// using OptionalValue matcher API
ids(holder.optional.is(Optional.empty())).isOf("id3");
ids(holder.optional.is(Optional.of(true))).isOf("id2");
ids(holder.optional.is(Optional.of(false))).isOf("id1");
}
@Test
void nullable() {
repository.insert(generator.get().withId("id1").withValue(true).withNullable(false));
repository.insert(generator.get().withId("id2").withValue(false).withNullable(true));
repository.insert(generator.get().withId("id3").withValue(false).withNullable(null));
ids(holder.nullable.isPresent()).hasContentInAnyOrder("id1", "id2");
ids(holder.nullable.isAbsent()).hasContentInAnyOrder("id3");
ids(holder.nullable.is(true)).hasContentInAnyOrder("id2");
ids(holder.nullable.isTrue()).hasContentInAnyOrder("id2");
ids(holder.nullable.is(false)).hasContentInAnyOrder("id1");
ids(holder.nullable.isFalse()).hasContentInAnyOrder("id1");
// using OptionalValue matcher API
ids(holder.nullable.is(Optional.empty())).isOf("id3");
ids(holder.nullable.is(Optional.of(true))).isOf("id2");
ids(holder.nullable.is(Optional.of(false))).isOf("id1");
}
@Test
void projection() {
repository.insert(generator.get().withId("id1").withValue(true).withNullable(false).withBoxed(Boolean.TRUE).withOptional(Optional.of(false)));
repository.insert(generator.get().withId("id2").withValue(false).withNullable(true).withBoxed(Boolean.FALSE).withOptional(Optional.of(true)));
repository.insert(generator.get().withId("id3").withValue(false).withNullable(null).withBoxed(Boolean.TRUE).withOptional(Optional.empty()));
// projection of one attribute
check(repository.findAll().select(holder.id).fetch()).hasContentInAnyOrder("id1", "id2", "id3");
check(repository.findAll().select(holder.value).fetch()).hasContentInAnyOrder(true, false, false);
check(repository.findAll().select(holder.nullable).asOptional().fetch()).hasContentInAnyOrder(Optional.empty(), Optional.of(true), Optional.of(false));
check(repository.findAll().select(holder.boxed).fetch()).hasContentInAnyOrder(Boolean.TRUE, Boolean.FALSE, Boolean.TRUE);
check(repository.findAll().select(holder.optional).fetch()).hasContentInAnyOrder(Optional.empty(), Optional.of(true), Optional.of(false));
// 2 attributes using tuple
check(repository.findAll().select(holder.nullable, holder.optional)
.map(tuple -> String.format("nullable=%s optional=%s", tuple.get(holder.nullable), tuple.get(holder.optional).map(Objects::toString).orElse("<empty>"))).fetch())
.hasContentInAnyOrder("nullable=false optional=false", "nullable=true optional=true", "nullable=null optional=<empty>");
// 2 attributes using mapper
check(repository.findAll().select(holder.nullable, holder.optional)
.map((nullable, optional) -> String.format("nullable=%s optional=%s", nullable, optional.map(Objects::toString).orElse("<empty>"))).fetch())
.hasContentInAnyOrder("nullable=false optional=false", "nullable=true optional=true", "nullable=null optional=<empty>");
// 3 attributes using tuple
check(repository.findAll().select(holder.id, holder.nullable, holder.optional)
.map(tuple -> String.format("id=%s nullable=%s optional=%s", tuple.get(holder.id), tuple.get(holder.nullable), tuple.get(holder.optional).map(Objects::toString).orElse("<empty>"))).fetch())
.hasContentInAnyOrder("id=id1 nullable=false optional=false", "id=id2 nullable=true optional=true", "id=id3 nullable=null optional=<empty>");
// 3 attributes using mapper
check(repository.findAll().select(holder.id, holder.nullable, holder.optional)
.map((id, nullable, optional) -> String.format("id=%s nullable=%s optional=%s", id, nullable, optional.map(Objects::toString).orElse("<empty>"))).fetch())
.hasContentInAnyOrder("id=id1 nullable=false optional=false", "id=id2 nullable=true optional=true", "id=id3 nullable=null optional=<empty>");
// 4 attributes using mapper
check(repository.findAll().select(holder.id, holder.value, holder.nullable, holder.optional)
.map((id, value, nullable, optional) -> String.format("id=%s value=%s nullable=%s optional=%s", id, value, nullable, optional.map(Objects::toString).orElse("<empty>"))).fetch())
.hasContentInAnyOrder("id=id1 value=true nullable=false optional=false", "id=id2 value=false nullable=true optional=true", "id=id3 value=false nullable=null optional=<empty>");
}
@Test
void isNot() {
repository.insert(generator.get().withId("id1").withValue(true).withNullable(false).withBoxed(Boolean.TRUE).withOptional(Optional.of(false)));
repository.insert(generator.get().withId("id2").withValue(false).withNullable(null).withBoxed(Boolean.FALSE).withOptional(Optional.empty()));
ids(holder.value.isNot(true)).isOf("id2");
ids(holder.value.isNot(false)).isOf("id1");
ids(holder.nullable.isNot(true)).notEmpty();
ids(holder.nullable.isNot(false)).not().has("id1");
}
private IterableChecker<List<String>, String> ids(BooleanHolderCriteria criteria) {
return CriteriaChecker.<TypeHolder.BooleanHolder>ofReader(repository.find(criteria)).toList(TypeHolder.BooleanHolder::id);
}
}