|
|
1
|
+package fr.ird.observe.entities;
|
|
|
2
|
+
|
|
|
3
|
+/*-
|
|
|
4
|
+ * #%L
|
|
|
5
|
+ * ObServe Models :: Persistence :: Java
|
|
|
6
|
+ * %%
|
|
|
7
|
+ * Copyright (C) 2008 - 2021 IRD, Code Lutin, Ultreia.io
|
|
|
8
|
+ * %%
|
|
|
9
|
+ * This program is free software: you can redistribute it and/or modify
|
|
|
10
|
+ * it under the terms of the GNU General Public License as
|
|
|
11
|
+ * published by the Free Software Foundation, either version 3 of the
|
|
|
12
|
+ * License, or (at your option) any later version.
|
|
|
13
|
+ *
|
|
|
14
|
+ * This program is distributed in the hope that it will be useful,
|
|
|
15
|
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
16
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
17
|
+ * GNU General Public License for more details.
|
|
|
18
|
+ *
|
|
|
19
|
+ * You should have received a copy of the GNU General Public
|
|
|
20
|
+ * License along with this program. If not, see
|
|
|
21
|
+ * <http://www.gnu.org/licenses/gpl-3.0.html>.
|
|
|
22
|
+ * #L%
|
|
|
23
|
+ */
|
|
|
24
|
+
|
|
|
25
|
+import fr.ird.observe.dto.IdDto;
|
|
|
26
|
+import fr.ird.observe.dto.ToolkitId;
|
|
|
27
|
+import fr.ird.observe.dto.ToolkitIdMap;
|
|
|
28
|
+import fr.ird.observe.dto.data.EditableDto;
|
|
|
29
|
+import fr.ird.observe.dto.data.OpenableDto;
|
|
|
30
|
+import fr.ird.observe.dto.reference.DtoReference;
|
|
|
31
|
+import fr.ird.observe.spi.PersistenceBusinessProject;
|
|
|
32
|
+import fr.ird.observe.spi.context.DtoEntityContext;
|
|
|
33
|
+import fr.ird.observe.spi.module.ObserveBusinessProject;
|
|
|
34
|
+import fr.ird.observe.test.ObserveFixtures;
|
|
|
35
|
+import org.junit.Assert;
|
|
|
36
|
+import org.junit.Test;
|
|
|
37
|
+import org.nuiton.topia.persistence.TopiaEntity;
|
|
|
38
|
+
|
|
|
39
|
+import java.lang.reflect.Modifier;
|
|
|
40
|
+import java.util.Collections;
|
|
|
41
|
+import java.util.List;
|
|
|
42
|
+import java.util.Set;
|
|
|
43
|
+import java.util.stream.Stream;
|
|
|
44
|
+
|
|
|
45
|
+/**
|
|
|
46
|
+ * Created on 28/04/2021.
|
|
|
47
|
+ *
|
|
|
48
|
+ * @author Tony Chemit - dev@tchemit.fr
|
|
|
49
|
+ * @since 9.0.0
|
|
|
50
|
+ */
|
|
|
51
|
+public class ObserveTopiaPersistenceContextTest extends PersistenceTestSupport {
|
|
|
52
|
+
|
|
|
53
|
+ @Test
|
|
|
54
|
+ public void testMapProjection() {
|
|
|
55
|
+ try (ObserveTopiaPersistenceContext persistenceContext = localTestMethodResource.newPersistenceContext()) {
|
|
|
56
|
+ for (ObserveEntityEnum value : ObserveEntityEnum.values()) {
|
|
|
57
|
+ if (Modifier.isAbstract(value.getImplementation().getModifiers())) {
|
|
|
58
|
+ continue;
|
|
|
59
|
+ }
|
|
|
60
|
+ Class<? extends TopiaEntity> contract = value.getContract();
|
|
|
61
|
+ List<String> allIds = persistenceContext.getDao(contract).findAllIds();
|
|
|
62
|
+ if (allIds.isEmpty()) {
|
|
|
63
|
+ continue;
|
|
|
64
|
+ }
|
|
|
65
|
+ if (allIds.size() < 100) {
|
|
|
66
|
+ Stream<ToolkitIdMap> queryAll = persistenceContext.executeQueryMapAll(contract);
|
|
|
67
|
+ Assert.assertNotNull(queryAll);
|
|
|
68
|
+ Assert.assertEquals(allIds.size(), queryAll.count());
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ String id = allIds.get(0);
|
|
|
72
|
+ ToolkitIdMap queryOne = persistenceContext.executeQueryMapOne(contract, id);
|
|
|
73
|
+ Assert.assertNotNull(queryOne);
|
|
|
74
|
+ Assert.assertEquals(id, queryOne.getTopiaId());
|
|
|
75
|
+
|
|
|
76
|
+ Stream<ToolkitIdMap> querySome = persistenceContext.executeQueryMapSome(contract, Collections.singleton(id));
|
|
|
77
|
+ Assert.assertNotNull(querySome);
|
|
|
78
|
+ Assert.assertEquals(1, querySome.count());
|
|
|
79
|
+
|
|
|
80
|
+ }
|
|
|
81
|
+ }
|
|
|
82
|
+ }
|
|
|
83
|
+
|
|
|
84
|
+ @Test
|
|
|
85
|
+ public void testMapProjectionFromDao() {
|
|
|
86
|
+ try (ObserveTopiaPersistenceContext persistenceContext = localTestMethodResource.newPersistenceContext()) {
|
|
|
87
|
+ for (ObserveEntityEnum value : ObserveEntityEnum.values()) {
|
|
|
88
|
+ if (Modifier.isAbstract(value.getImplementation().getModifiers())) {
|
|
|
89
|
+ continue;
|
|
|
90
|
+ }
|
|
|
91
|
+ Class<? extends TopiaEntity> contract = value.getContract();
|
|
|
92
|
+ ObserveDaoQuerySupport dao = (ObserveDaoQuerySupport) persistenceContext.getDao(contract);
|
|
|
93
|
+ List<String> allIds = dao.findAllIds();
|
|
|
94
|
+ if (allIds.isEmpty()) {
|
|
|
95
|
+ continue;
|
|
|
96
|
+ }
|
|
|
97
|
+ if (allIds.size() < 100) {
|
|
|
98
|
+ Stream<ToolkitIdMap> queryAll = dao.executeQueryMapAll();
|
|
|
99
|
+ Assert.assertNotNull(queryAll);
|
|
|
100
|
+ Assert.assertEquals(allIds.size(), queryAll.count());
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ String id = allIds.get(0);
|
|
|
104
|
+ ToolkitIdMap queryOne = dao.executeQueryMapOne(id);
|
|
|
105
|
+ Assert.assertNotNull(queryOne);
|
|
|
106
|
+ Assert.assertEquals(id, queryOne.getTopiaId());
|
|
|
107
|
+
|
|
|
108
|
+ Stream<ToolkitIdMap> querySome = dao.executeQueryMapSome(Collections.singleton(id));
|
|
|
109
|
+ Assert.assertNotNull(querySome);
|
|
|
110
|
+ Assert.assertEquals(1, querySome.count());
|
|
|
111
|
+
|
|
|
112
|
+ }
|
|
|
113
|
+ }
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ @Test
|
|
|
117
|
+ public void testGetParentId() {
|
|
|
118
|
+
|
|
|
119
|
+ Set<Class<? extends OpenableDto>> openableDataTypes = ObserveBusinessProject.get().getOpenableDataTypes();
|
|
|
120
|
+ Set<Class<? extends EditableDto>> editableDataTypes = ObserveBusinessProject.get().getEditableDataTypes();
|
|
|
121
|
+
|
|
|
122
|
+ try (ObserveTopiaPersistenceContext persistenceContext = localTestMethodResource.newPersistenceContext()) {
|
|
|
123
|
+ for (ObserveEntityEnum value : ObserveEntityEnum.values()) {
|
|
|
124
|
+ if (Modifier.isAbstract(value.getImplementation().getModifiers())) {
|
|
|
125
|
+ continue;
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ @SuppressWarnings("unchecked") Class<? extends Entity> contract = (Class<? extends Entity>) value.getContract();
|
|
|
129
|
+ DtoEntityContext<IdDto, DtoReference, ? extends Entity, ?> spi = PersistenceBusinessProject.fromEntity(contract);
|
|
|
130
|
+ if (spi == null) {
|
|
|
131
|
+ continue;
|
|
|
132
|
+ }
|
|
|
133
|
+ Class<IdDto> dtoType = spi.toDtoType();
|
|
|
134
|
+
|
|
|
135
|
+ boolean isOpen = openableDataTypes.contains(dtoType);
|
|
|
136
|
+ String id = null;
|
|
|
137
|
+ if (isOpen) {
|
|
|
138
|
+ List<String> allIds = persistenceContext.getDao(contract).findAllIds();
|
|
|
139
|
+ if (allIds.isEmpty()) {
|
|
|
140
|
+ continue;
|
|
|
141
|
+ }
|
|
|
142
|
+ id = allIds.get(0);
|
|
|
143
|
+ }
|
|
|
144
|
+ if (id == null) {
|
|
|
145
|
+ continue;
|
|
|
146
|
+ }
|
|
|
147
|
+ String query = persistenceContext.executeQueryGetParentId(contract, id);
|
|
|
148
|
+ if (query == null) {
|
|
|
149
|
+ query = persistenceContext.executeQueryGetParentId2(contract, id);
|
|
|
150
|
+ Assert.assertNotNull(query);
|
|
|
151
|
+ }
|
|
|
152
|
+ }
|
|
|
153
|
+ }
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ @Test
|
|
|
157
|
+ public void testIdProjection() {
|
|
|
158
|
+ try (ObserveTopiaPersistenceContext persistenceContext = localTestMethodResource.newPersistenceContext()) {
|
|
|
159
|
+ for (ObserveEntityEnum value : ObserveEntityEnum.values()) {
|
|
|
160
|
+ if (Modifier.isAbstract(value.getImplementation().getModifiers())) {
|
|
|
161
|
+ continue;
|
|
|
162
|
+ }
|
|
|
163
|
+ Class<? extends TopiaEntity> contract = value.getContract();
|
|
|
164
|
+ List<String> allIds = persistenceContext.getDao(contract).findAllIds();
|
|
|
165
|
+ if (allIds.isEmpty()) {
|
|
|
166
|
+ continue;
|
|
|
167
|
+ }
|
|
|
168
|
+ if (allIds.size() < 100) {
|
|
|
169
|
+ Stream<ToolkitId> queryAll = persistenceContext.executeQueryIdAll(contract);
|
|
|
170
|
+ Assert.assertNotNull(queryAll);
|
|
|
171
|
+ Assert.assertEquals(allIds.size(), queryAll.count());
|
|
|
172
|
+ }
|
|
|
173
|
+ String id = allIds.get(0);
|
|
|
174
|
+ ToolkitId query = persistenceContext.executeQueryIdEquals(contract, id);
|
|
|
175
|
+ Assert.assertNotNull(query);
|
|
|
176
|
+ Assert.assertEquals(id, query.getTopiaId());
|
|
|
177
|
+ Assert.assertEquals(id, query.getId());
|
|
|
178
|
+ Assert.assertNotNull(query.getLastUpdateDate());
|
|
|
179
|
+
|
|
|
180
|
+ Stream<ToolkitId> querySome = persistenceContext.executeQueryIdIn(contract, Collections.singleton(id));
|
|
|
181
|
+ Assert.assertNotNull(querySome);
|
|
|
182
|
+ Assert.assertEquals(1, querySome.count());
|
|
|
183
|
+
|
|
|
184
|
+ Stream<ToolkitId> queryBefore = persistenceContext.executeQueryIdBefore(contract, ObserveFixtures.DATE);
|
|
|
185
|
+ Assert.assertNotNull(queryBefore);
|
|
|
186
|
+
|
|
|
187
|
+ Stream<ToolkitId> queryAfter = persistenceContext.executeQueryIdAfter(contract, ObserveFixtures.DATE);
|
|
|
188
|
+ Assert.assertNotNull(queryAfter);
|
|
|
189
|
+ }
|
|
|
190
|
+ }
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+
|
|
|
194
|
+ @Test
|
|
|
195
|
+ public void testIdProjectionFromDao() {
|
|
|
196
|
+ try (ObserveTopiaPersistenceContext persistenceContext = localTestMethodResource.newPersistenceContext()) {
|
|
|
197
|
+ for (ObserveEntityEnum value : ObserveEntityEnum.values()) {
|
|
|
198
|
+ if (Modifier.isAbstract(value.getImplementation().getModifiers())) {
|
|
|
199
|
+ continue;
|
|
|
200
|
+ }
|
|
|
201
|
+ Class<? extends TopiaEntity> contract = value.getContract();
|
|
|
202
|
+ ObserveDaoQuerySupport dao = (ObserveDaoQuerySupport) persistenceContext.getDao(contract);
|
|
|
203
|
+ List<String> allIds = dao.findAllIds();
|
|
|
204
|
+ if (allIds.isEmpty()) {
|
|
|
205
|
+ continue;
|
|
|
206
|
+ }
|
|
|
207
|
+ if (allIds.size() < 100) {
|
|
|
208
|
+ Stream<ToolkitId> queryAll = dao.executeQueryIdAll();
|
|
|
209
|
+ Assert.assertNotNull(queryAll);
|
|
|
210
|
+ Assert.assertEquals(allIds.size(), queryAll.count());
|
|
|
211
|
+ }
|
|
|
212
|
+ String id = allIds.get(0);
|
|
|
213
|
+ ToolkitId query = dao.executeQueryIdEquals(id);
|
|
|
214
|
+ Assert.assertNotNull(query);
|
|
|
215
|
+ Assert.assertEquals(id, query.getTopiaId());
|
|
|
216
|
+ Assert.assertEquals(id, query.getId());
|
|
|
217
|
+ Assert.assertNotNull(query.getLastUpdateDate());
|
|
|
218
|
+
|
|
|
219
|
+ Stream<ToolkitId> querySome = dao.executeQueryIdIn(Collections.singleton(id));
|
|
|
220
|
+ Assert.assertNotNull(querySome);
|
|
|
221
|
+ Assert.assertEquals(1, querySome.count());
|
|
|
222
|
+
|
|
|
223
|
+ Stream<ToolkitId> queryBefore = dao.executeQueryIdBefore(ObserveFixtures.DATE);
|
|
|
224
|
+ Assert.assertNotNull(queryBefore);
|
|
|
225
|
+
|
|
|
226
|
+ Stream<ToolkitId> queryAfter = dao.executeQueryIdAfter(ObserveFixtures.DATE);
|
|
|
227
|
+ Assert.assertNotNull(queryAfter);
|
|
|
228
|
+ }
|
|
|
229
|
+ }
|
|
|
230
|
+ }
|
|
|
231
|
+}
|