@simplix-boot/cli 0.0.0-stage → 1.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +25 -0
- package/LICENSE_KO +28 -0
- package/README.ko.md +82 -0
- package/README.md +81 -2
- package/bin/simplix-boot.js +6 -0
- package/package.json +45 -4
- package/src/cli.js +103 -0
- package/src/commands/config.js +129 -0
- package/src/commands/generate.js +291 -0
- package/src/commands/generated.js +125 -0
- package/src/commands/init.js +41 -0
- package/src/commands/promote.js +172 -0
- package/src/commands/templates.js +100 -0
- package/src/commands/version.js +134 -0
- package/src/lib/analysis/fields.js +217 -0
- package/src/lib/analysis/imports.js +76 -0
- package/src/lib/analysis/structure.js +193 -0
- package/src/lib/analysis/types.js +94 -0
- package/src/lib/args.js +69 -0
- package/src/lib/comment-extractor.js +141 -0
- package/src/lib/config/fields.js +321 -0
- package/src/lib/config/parents.js +108 -0
- package/src/lib/config/paths.js +156 -0
- package/src/lib/config/yml.js +61 -0
- package/src/lib/console.js +83 -0
- package/src/lib/controller-name.js +33 -0
- package/src/lib/entity-analyzer.js +49 -0
- package/src/lib/entity-type-resolver.js +80 -0
- package/src/lib/entity-type.js +35 -0
- package/src/lib/errors.js +17 -0
- package/src/lib/export-config.js +74 -0
- package/src/lib/file-utils.js +77 -0
- package/src/lib/generate/file-plan.js +155 -0
- package/src/lib/generate/search-fields.js +111 -0
- package/src/lib/generate/settings.js +111 -0
- package/src/lib/generate/support-classes.js +47 -0
- package/src/lib/generate/template-vars.js +183 -0
- package/src/lib/git-utils.js +18 -0
- package/src/lib/name-field.js +42 -0
- package/src/lib/project.js +121 -0
- package/src/lib/promote-files.js +132 -0
- package/src/lib/rendered-output.js +13 -0
- package/src/lib/search-operators.js +87 -0
- package/src/lib/templates.js +50 -0
- package/src/lib/tracked-render.js +200 -0
- package/src/lib/version-check.js +195 -0
- package/src/lib/version-utils.js +97 -0
- package/templates/controller/rest/EntityRestController.java.template +517 -0
- package/templates/controller/web/EntityWebController.java.template +211 -0
- package/templates/dto/EntityDTOs.java.template +981 -0
- package/templates/repository/EntityRepository.java.template +78 -0
- package/templates/service/EntityService.java.template +790 -0
- package/templates/service/EntityTreeService.java.template +352 -0
- package/templates/test/EntityServiceTest.java.template +937 -0
- package/templates/test/EntityTreeServiceTest.java.template +842 -0
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
package <%= packagePaths.service %>;
|
|
2
|
+
|
|
3
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>CreateDTO;
|
|
4
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>UpdateDTO;
|
|
5
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>DetailDTO;
|
|
6
|
+
<%
|
|
7
|
+
// Check if batch update is supported for import
|
|
8
|
+
const hasBatchUpdateFieldsForImport = fields.some(field => {
|
|
9
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
10
|
+
return ymlField?.views?.includes('batchUpdate');
|
|
11
|
+
});
|
|
12
|
+
if (hasBatchUpdateFieldsForImport) { %>
|
|
13
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>BatchUpdateDTO;
|
|
14
|
+
<% } %>
|
|
15
|
+
import <%= entityPackage %>.<%= entityName %>;
|
|
16
|
+
<% if (hasEmbeddedId) { %>
|
|
17
|
+
import <%= entityPackage %>.<%= entityName %>.<%= embeddedIdType %>;
|
|
18
|
+
<% } %>
|
|
19
|
+
import <%= packagePaths.repository %>.<%= entityName %>Repository;
|
|
20
|
+
<%
|
|
21
|
+
// Parse import statements for reference entities (excluding self-reference)
|
|
22
|
+
// Only import services for ManyToMany references (multiple: true) since ManyToOne uses FK ID directly
|
|
23
|
+
const importedServices = new Set();
|
|
24
|
+
|
|
25
|
+
// Find ManyToMany reference types from yml configuration and generate imports if different package
|
|
26
|
+
if (ymlConfig && ymlConfig.fields) {
|
|
27
|
+
Object.entries(ymlConfig.fields).forEach(([fieldName, ymlField]) => {
|
|
28
|
+
// Only generate service imports for ManyToMany references (multiple: true)
|
|
29
|
+
if (ymlField.reference && ymlField.reference.entity && ymlField.reference.entity !== entityName && ymlField.reference.multiple) {
|
|
30
|
+
const refEntity = ymlField.reference.entity;
|
|
31
|
+
const refModulePath = ymlField.reference.modulePath;
|
|
32
|
+
|
|
33
|
+
if (!importedServices.has(refEntity)) {
|
|
34
|
+
importedServices.add(refEntity);
|
|
35
|
+
if (refModulePath && refModulePath !== modulePath) {
|
|
36
|
+
// Different package - generate import
|
|
37
|
+
const servicePackage = basePackage + '.web.' + refModulePath.replace(/\//g, '.') + '.service';
|
|
38
|
+
%>
|
|
39
|
+
import <%= servicePackage %>.<%= refEntity %>Service;<%
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
} %>
|
|
45
|
+
|
|
46
|
+
import dev.simplecore.simplix.core.exception.SimpliXGeneralException;
|
|
47
|
+
import jakarta.persistence.EntityManager;
|
|
48
|
+
import org.junit.jupiter.api.BeforeEach;
|
|
49
|
+
import org.junit.jupiter.api.Test;
|
|
50
|
+
import org.junit.jupiter.api.DisplayName;
|
|
51
|
+
import org.junit.jupiter.api.extension.ExtendWith;
|
|
52
|
+
import org.mockito.Mock;
|
|
53
|
+
<% const statusCountFields = ymlConfig.statusCounts || []; const aggregateItems = ymlConfig.aggregates || []; const hasFigures = statusCountFields.length > 0 || aggregateItems.length > 0; const figureScope = ymlConfig.figureScope && ymlConfig.figureScope.method; const scopeMayMatchNothing = !!(ymlConfig.figureScope && ymlConfig.figureScope.mayMatchNothing); const scopedValue = scopeMayMatchNothing ? 'Optional.of(new HashMap<String, String>())' : 'new HashMap<String, String>()'; %>
|
|
54
|
+
<% if (ymlConfig.timeAxisField) { %>
|
|
55
|
+
import dev.simplecore.searchable.core.service.bucket.TimeBucketCounter;
|
|
56
|
+
<% } %>
|
|
57
|
+
<% if (hasFigures) { %>
|
|
58
|
+
import dev.simplecore.searchable.core.service.aggregate.SearchAggregator;
|
|
59
|
+
import dev.simplecore.searchable.core.service.aggregate.SearchAggregator.AggregateValue;
|
|
60
|
+
import dev.simplecore.searchable.core.service.aggregate.SearchAggregator.ValueCount;
|
|
61
|
+
import dev.simplecore.simplix.core.exception.SimpliXGeneralException;
|
|
62
|
+
<% } %>
|
|
63
|
+
import org.mockito.junit.jupiter.MockitoExtension;
|
|
64
|
+
import org.modelmapper.ModelMapper;
|
|
65
|
+
import org.springframework.beans.factory.ObjectProvider;
|
|
66
|
+
import org.springframework.context.MessageSource;
|
|
67
|
+
import org.springframework.test.util.ReflectionTestUtils;
|
|
68
|
+
import org.springframework.data.domain.*;
|
|
69
|
+
import org.springframework.data.jpa.domain.Specification;
|
|
70
|
+
|
|
71
|
+
import java.time.Instant;
|
|
72
|
+
import java.util.*;
|
|
73
|
+
import java.util.stream.Collectors;
|
|
74
|
+
|
|
75
|
+
import static org.assertj.core.api.Assertions.assertThat;
|
|
76
|
+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
77
|
+
import static org.mockito.ArgumentMatchers.*;
|
|
78
|
+
import static org.mockito.Mockito.*;
|
|
79
|
+
|
|
80
|
+
<%
|
|
81
|
+
// Build excluded fields set from generator-simplix.json configuration (same as DTO template)
|
|
82
|
+
const excludedFieldsSet = new Set(dtoExcludeFields || []);
|
|
83
|
+
|
|
84
|
+
// Helper: Check if a field should be excluded from DTO generation
|
|
85
|
+
function isExcludedField(fieldName) {
|
|
86
|
+
return excludedFieldsSet.has(fieldName);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Helper: Check if field name already ends with 'Id' (to avoid 'IdId' pattern)
|
|
90
|
+
function fieldNameEndsWithId(fieldName) {
|
|
91
|
+
return fieldName.endsWith('Id') && fieldName !== 'Id';
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// Helper: whether a referencing field holds the key itself (a scalar column such as grantedBy)
|
|
95
|
+
// rather than an association whose key sits one step further; the field's own type decides.
|
|
96
|
+
function holdsReferenceKey(fieldName) {
|
|
97
|
+
if (fieldNameEndsWithId(fieldName)) return true;
|
|
98
|
+
const entityField = fields.find(f => f.name === fieldName);
|
|
99
|
+
return Boolean(entityField) && entityField.typeKind !== 'entity' && !entityField.isCollection;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Helper: Get DTO setter name for reference field (avoids 'IdId' pattern)
|
|
103
|
+
function getDtoSetterName(fieldName, hasReference, isMultiple) {
|
|
104
|
+
if (!hasReference) {
|
|
105
|
+
return fieldName;
|
|
106
|
+
}
|
|
107
|
+
if (isMultiple) {
|
|
108
|
+
return fieldNameEndsWithId(fieldName) ? `${fieldName}s` : `${fieldName}Ids`;
|
|
109
|
+
} else {
|
|
110
|
+
return holdsReferenceKey(fieldName) ? fieldName : `${fieldName}Id`;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
// Helper: For primitive boolean fields with 'is' prefix, Lombok strips 'is' in the setter
|
|
114
|
+
// e.g., primitive `boolean isOnline` -> setter: `setOnline(boolean)` (not `setIsOnline`)
|
|
115
|
+
// But boxed `Boolean isOnline` -> setter: `setIsOnline(Boolean)` (keeps 'is' prefix)
|
|
116
|
+
// This helper is ONLY for entity setters, not DTO setters (DTOs use boxed Boolean)
|
|
117
|
+
function getEntitySetterSuffix(field) {
|
|
118
|
+
if (field.type === 'boolean' && field.name.startsWith('is') && field.name.length > 2) {
|
|
119
|
+
return field.name.charAt(2).toUpperCase() + field.name.slice(3);
|
|
120
|
+
}
|
|
121
|
+
return field.name.charAt(0).toUpperCase() + field.name.slice(1);
|
|
122
|
+
}
|
|
123
|
+
%>
|
|
124
|
+
/**
|
|
125
|
+
* Unit tests for <%= classPrefix %>Service
|
|
126
|
+
*
|
|
127
|
+
* This test class verifies:
|
|
128
|
+
* - CRUD operations (Create, Read, Update, Delete)
|
|
129
|
+
* - Search and pagination functionality
|
|
130
|
+
* - Batch update operations
|
|
131
|
+
* - Exception handling
|
|
132
|
+
* - Data validation
|
|
133
|
+
*/
|
|
134
|
+
@ExtendWith(MockitoExtension.class)
|
|
135
|
+
@DisplayName("<%= classPrefix %>Service Tests")
|
|
136
|
+
class <%= classPrefix %>ServiceTest {
|
|
137
|
+
|
|
138
|
+
@Mock
|
|
139
|
+
private <%= entityName %>Repository repository;
|
|
140
|
+
|
|
141
|
+
@Mock
|
|
142
|
+
private EntityManager entityManager;
|
|
143
|
+
|
|
144
|
+
@Mock
|
|
145
|
+
private MessageSource messageSource;
|
|
146
|
+
|
|
147
|
+
@Mock
|
|
148
|
+
private ModelMapper modelMapper;
|
|
149
|
+
<% if (ymlConfig.timeAxisField) { %>
|
|
150
|
+
@Mock
|
|
151
|
+
private TimeBucketCounter timeBucketCounter;
|
|
152
|
+
<% } %>
|
|
153
|
+
<% if (hasFigures) { %>
|
|
154
|
+
@Mock
|
|
155
|
+
private SearchAggregator searchAggregator;
|
|
156
|
+
<% } %>
|
|
157
|
+
<%
|
|
158
|
+
// Add mocks for reference services (only for ManyToMany - ManyToOne uses FK ID directly)
|
|
159
|
+
const addedServices = new Set();
|
|
160
|
+
fields.forEach(field => {
|
|
161
|
+
// Only add mocks for ManyToMany relationships (collection references)
|
|
162
|
+
if (field.relationType === 'ManyToMany' || (field.isCollection && field.typeKind === 'entity')) {
|
|
163
|
+
const entityType = field.actualType || field.type;
|
|
164
|
+
if (!addedServices.has(entityType) && entityType !== entityName) {
|
|
165
|
+
addedServices.add(entityType); %>
|
|
166
|
+
@Mock
|
|
167
|
+
private ObjectProvider<<%= entityType %>Service> <%= _.camelCase(entityType) %>ServiceProvider;
|
|
168
|
+
<% }
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
// Also check yml configuration for ManyToMany references
|
|
172
|
+
if (ymlConfig && ymlConfig.fields) {
|
|
173
|
+
Object.values(ymlConfig.fields).forEach(field => {
|
|
174
|
+
if (field.reference && field.reference.entity && field.reference.multiple && !addedServices.has(field.reference.entity) && field.reference.entity !== entityName) {
|
|
175
|
+
addedServices.add(field.reference.entity); %>
|
|
176
|
+
@Mock
|
|
177
|
+
private ObjectProvider<<%= field.reference.entity %>Service> <%= _.camelCase(field.reference.entity) %>ServiceProvider;
|
|
178
|
+
<% }
|
|
179
|
+
});
|
|
180
|
+
} %>
|
|
181
|
+
|
|
182
|
+
private <%= classPrefix %>Service service;
|
|
183
|
+
|
|
184
|
+
private <%= entityName %> entity;
|
|
185
|
+
private <%= classPrefix %>CreateDTO createDto;
|
|
186
|
+
private <%= classPrefix %>UpdateDTO updateDto;
|
|
187
|
+
<% if (hasEmbeddedId) { %>
|
|
188
|
+
private <%= embeddedIdType %> embeddedId;
|
|
189
|
+
<% } else { %>
|
|
190
|
+
private <%= idType %> testId;
|
|
191
|
+
<% } %>
|
|
192
|
+
|
|
193
|
+
@BeforeEach
|
|
194
|
+
void setUp() {
|
|
195
|
+
service = new <%= classPrefix %>Service(
|
|
196
|
+
repository,
|
|
197
|
+
entityManager<%
|
|
198
|
+
Array.from(addedServices).forEach(entityType => { %>,
|
|
199
|
+
<%= _.camelCase(entityType) %>ServiceProvider<%
|
|
200
|
+
}); %>,
|
|
201
|
+
messageSource<% if (ymlConfig.timeAxisField) { %>,
|
|
202
|
+
timeBucketCounter<% } %><% if (hasFigures) { %>,
|
|
203
|
+
searchAggregator<% } %>
|
|
204
|
+
);
|
|
205
|
+
|
|
206
|
+
// Inject ModelMapper using reflection since it's not in the constructor
|
|
207
|
+
ReflectionTestUtils.setField(service, "modelMapper", modelMapper);
|
|
208
|
+
|
|
209
|
+
<% if (hasEmbeddedId) { %>
|
|
210
|
+
// Setup embedded ID
|
|
211
|
+
embeddedId = new <%= embeddedIdType %>();
|
|
212
|
+
<% embeddedIdFields.forEach(field => { %>
|
|
213
|
+
embeddedId.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(<%=
|
|
214
|
+
field.type === 'String' ? '"test-' + field.name + '"' :
|
|
215
|
+
field.type === 'Long' ? '1L' :
|
|
216
|
+
field.type === 'Integer' ? '1' :
|
|
217
|
+
field.type === 'UUID' ? 'UUID.randomUUID()' :
|
|
218
|
+
'"test"'
|
|
219
|
+
%>);
|
|
220
|
+
<% }); %>
|
|
221
|
+
<% } else { %>
|
|
222
|
+
testId = <%= idType === 'String' ? 'UUID.randomUUID().toString()' :
|
|
223
|
+
idType === 'Long' ? '1L' :
|
|
224
|
+
idType === 'Integer' ? '1' :
|
|
225
|
+
idType === 'UUID' ? 'UUID.randomUUID()' : '1' %>;
|
|
226
|
+
<% } %>
|
|
227
|
+
|
|
228
|
+
// Setup test entity
|
|
229
|
+
entity = new <%= entityName %>();
|
|
230
|
+
<% if (!hasEmbeddedId) { %>entity.setId(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
231
|
+
<% if (hasEmbeddedId) { %>
|
|
232
|
+
entity.set<%= embeddedIdField.charAt(0).toUpperCase() + embeddedIdField.slice(1) %>(embeddedId);
|
|
233
|
+
<% } else { %>
|
|
234
|
+
entity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
235
|
+
<% } %>
|
|
236
|
+
<%
|
|
237
|
+
// Set fields that are in 'edit' view (same as CreateDTO)
|
|
238
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
239
|
+
fields.forEach(field => {
|
|
240
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
241
|
+
const setterSuffix = getEntitySetterSuffix(field);
|
|
242
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && !field.isRelation && !isExcludedField(field.name)) {
|
|
243
|
+
if (field.type === 'String') { %>
|
|
244
|
+
entity.set<%= setterSuffix %>("test-<%= field.name %>");
|
|
245
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
246
|
+
entity.set<%= setterSuffix %>(1);
|
|
247
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
248
|
+
entity.set<%= setterSuffix %>(1L);
|
|
249
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
250
|
+
entity.set<%= setterSuffix %>(true);
|
|
251
|
+
<% }
|
|
252
|
+
}
|
|
253
|
+
}); %><% if (ymlConfig.fields?.createdAt) { %>
|
|
254
|
+
entity.setCreatedAt(Instant.now());<% } %><% if (ymlConfig.fields?.updatedAt) { %>
|
|
255
|
+
entity.setUpdatedAt(Instant.now());<% } %>
|
|
256
|
+
|
|
257
|
+
// Setup DTOs
|
|
258
|
+
createDto = new <%= classPrefix %>CreateDTO();
|
|
259
|
+
<%
|
|
260
|
+
// Set DTO fields
|
|
261
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
262
|
+
fields.forEach(field => {
|
|
263
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
264
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && !field.isRelation && !isExcludedField(field.name)) {
|
|
265
|
+
// For reference fields, use getDtoSetterName to avoid 'IdId' pattern
|
|
266
|
+
const hasReference = ymlField?.reference && !ymlField.reference.multiple;
|
|
267
|
+
const setterName = getDtoSetterName(field.name, hasReference, false);
|
|
268
|
+
const upperSetterName = setterName.charAt(0).toUpperCase() + setterName.slice(1);
|
|
269
|
+
if (field.type === 'String') { %>
|
|
270
|
+
createDto.set<%= upperSetterName %>("test-<%= field.name %>");
|
|
271
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
272
|
+
createDto.set<%= upperSetterName %>(1);
|
|
273
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
274
|
+
createDto.set<%= upperSetterName %>(1L);
|
|
275
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
276
|
+
createDto.set<%= upperSetterName %>(true);
|
|
277
|
+
<% }
|
|
278
|
+
}
|
|
279
|
+
}); %>
|
|
280
|
+
|
|
281
|
+
updateDto = new <%= classPrefix %>UpdateDTO();
|
|
282
|
+
<% if (!hasEmbeddedId) { %>updateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
283
|
+
<%
|
|
284
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
285
|
+
fields.forEach(field => {
|
|
286
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
287
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && !field.isRelation && !isExcludedField(field.name)) {
|
|
288
|
+
// For reference fields, use getDtoSetterName to avoid 'IdId' pattern
|
|
289
|
+
const hasReference = ymlField?.reference && !ymlField.reference.multiple;
|
|
290
|
+
const setterName = getDtoSetterName(field.name, hasReference, false);
|
|
291
|
+
const upperSetterName = setterName.charAt(0).toUpperCase() + setterName.slice(1);
|
|
292
|
+
if (field.type === 'String') { %>
|
|
293
|
+
updateDto.set<%= upperSetterName %>("updated-<%= field.name %>");
|
|
294
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
295
|
+
updateDto.set<%= upperSetterName %>(2);
|
|
296
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
297
|
+
updateDto.set<%= upperSetterName %>(2L);
|
|
298
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
299
|
+
updateDto.set<%= upperSetterName %>(false);
|
|
300
|
+
<% }
|
|
301
|
+
}
|
|
302
|
+
}); %>
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
@Test
|
|
306
|
+
@DisplayName("Should create entity successfully")
|
|
307
|
+
void testCreate() {
|
|
308
|
+
// Given
|
|
309
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
310
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
311
|
+
|
|
312
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
313
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
314
|
+
doAnswer(invocation -> {
|
|
315
|
+
// Simulate mapping from DTO to entity
|
|
316
|
+
return null;
|
|
317
|
+
}).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
318
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
319
|
+
.thenReturn(detailDto);
|
|
320
|
+
|
|
321
|
+
// When
|
|
322
|
+
<%= classPrefix %>DetailDTO result = service.create(createDto);
|
|
323
|
+
|
|
324
|
+
// Then
|
|
325
|
+
assertThat(result).isNotNull();
|
|
326
|
+
<% if (hasEmbeddedId) { %>
|
|
327
|
+
assertThat(result.get<%= embeddedIdField.charAt(0).toUpperCase() + embeddedIdField.slice(1) %>()).isEqualTo(embeddedId);
|
|
328
|
+
<% } else { %>
|
|
329
|
+
assertThat(result.get<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>()).isEqualTo(testId<% if (idType !== 'String') { %>.toString()<% } %>);
|
|
330
|
+
<% } %>
|
|
331
|
+
verify(repository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
@Test
|
|
335
|
+
@DisplayName("Should find entity by ID")
|
|
336
|
+
void testFindById() {
|
|
337
|
+
// Given
|
|
338
|
+
when(repository.findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>))
|
|
339
|
+
.thenReturn(Optional.of(entity));
|
|
340
|
+
|
|
341
|
+
// When
|
|
342
|
+
Optional<<%= entityName %>> result = service.findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>);
|
|
343
|
+
|
|
344
|
+
// Then
|
|
345
|
+
assertThat(result).isPresent();
|
|
346
|
+
assertThat(result.get()).isEqualTo(entity);
|
|
347
|
+
verify(repository, times(1)).findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@Test
|
|
351
|
+
@DisplayName("Should return empty when entity not found")
|
|
352
|
+
void testFindByIdNotFound() {
|
|
353
|
+
// Given
|
|
354
|
+
when(repository.findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>))
|
|
355
|
+
.thenReturn(Optional.empty());
|
|
356
|
+
|
|
357
|
+
// When
|
|
358
|
+
Optional<<%= entityName %>> result = service.findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>);
|
|
359
|
+
|
|
360
|
+
// Then
|
|
361
|
+
assertThat(result).isEmpty();
|
|
362
|
+
verify(repository, times(1)).findById(<% if (hasEmbeddedId) { %>embeddedId<% } else { %>testId<% } %>);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
@Test
|
|
366
|
+
@DisplayName("Should update entity successfully")
|
|
367
|
+
void testUpdate() {
|
|
368
|
+
// Given
|
|
369
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
370
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
371
|
+
|
|
372
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
373
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
374
|
+
doAnswer(invocation -> {
|
|
375
|
+
// Simulate mapping from DTO to entity
|
|
376
|
+
return null;
|
|
377
|
+
}).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
378
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
379
|
+
.thenReturn(detailDto);
|
|
380
|
+
|
|
381
|
+
// When
|
|
382
|
+
<%= classPrefix %>DetailDTO result = service.update(entity, updateDto);
|
|
383
|
+
|
|
384
|
+
// Then
|
|
385
|
+
assertThat(result).isNotNull();
|
|
386
|
+
verify(repository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
@Test
|
|
390
|
+
@DisplayName("Should handle entity update with save")
|
|
391
|
+
void testUpdateWithSave() {
|
|
392
|
+
// Given
|
|
393
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
394
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
395
|
+
|
|
396
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
397
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
398
|
+
doAnswer(invocation -> {
|
|
399
|
+
return null;
|
|
400
|
+
}).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
401
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
402
|
+
.thenReturn(detailDto);
|
|
403
|
+
|
|
404
|
+
// When
|
|
405
|
+
<%= classPrefix %>DetailDTO result = service.update(entity, updateDto);
|
|
406
|
+
|
|
407
|
+
// Then
|
|
408
|
+
assertThat(result).isNotNull();
|
|
409
|
+
verify(repository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
<% if (!hasEmbeddedId) { %>
|
|
413
|
+
@Test
|
|
414
|
+
@DisplayName("Should refuse an update that changes the ID")
|
|
415
|
+
void testUpdateRefusesChangedId() {
|
|
416
|
+
// Given
|
|
417
|
+
updateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(<%= idType === 'String' ? 'UUID.randomUUID().toString()' :
|
|
418
|
+
idType === 'Long' ? '2L' :
|
|
419
|
+
idType === 'Integer' ? '2' :
|
|
420
|
+
idType === 'UUID' ? 'UUID.randomUUID()' : '2' %>);
|
|
421
|
+
|
|
422
|
+
// When/Then: the message is the key a placeholder names, resolved only at the HTTP layer
|
|
423
|
+
assertThatThrownBy(() -> service.update(entity, updateDto))
|
|
424
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
425
|
+
.hasMessageContaining("error.id.cannot.change");
|
|
426
|
+
|
|
427
|
+
verify(repository, never()).saveAndFlush(any(<%= entityName %>.class));
|
|
428
|
+
}
|
|
429
|
+
<% } %>
|
|
430
|
+
|
|
431
|
+
@Test
|
|
432
|
+
@DisplayName("Should delete entity successfully")
|
|
433
|
+
void testDelete() {
|
|
434
|
+
// Given
|
|
435
|
+
<% if (idType === 'String') { %>
|
|
436
|
+
String deleteId = testId;
|
|
437
|
+
<% } else { %>
|
|
438
|
+
String deleteId = testId.toString();
|
|
439
|
+
<% } %>
|
|
440
|
+
doNothing().when(repository).deleteById(deleteId);
|
|
441
|
+
|
|
442
|
+
// When
|
|
443
|
+
service.delete(deleteId);
|
|
444
|
+
|
|
445
|
+
// Then
|
|
446
|
+
verify(repository, times(1)).deleteById(deleteId);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
@Test
|
|
450
|
+
@DisplayName("Should handle batch delete")
|
|
451
|
+
void testBatchDelete() {
|
|
452
|
+
// Given
|
|
453
|
+
<% if (idType === 'String') { %>
|
|
454
|
+
List<String> deleteIds = Arrays.asList(testId, UUID.randomUUID().toString());
|
|
455
|
+
<% } else { %>
|
|
456
|
+
List<String> deleteIds = Arrays.asList(testId.toString(), String.valueOf(<%= idType === 'Integer' ? 'new Random().nextInt(1000) + 1' : idType === 'Long' ? 'new Random().nextLong()' : '999L' %>));
|
|
457
|
+
<% } %>
|
|
458
|
+
doNothing().when(repository).deleteById(any());
|
|
459
|
+
|
|
460
|
+
// When
|
|
461
|
+
service.batchDelete(deleteIds);
|
|
462
|
+
|
|
463
|
+
// Then
|
|
464
|
+
// SimpliXBaseService calls deleteById for each ID
|
|
465
|
+
verify(repository, times(deleteIds.size())).deleteById(any());
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
@Test
|
|
469
|
+
@DisplayName("Should find all entities with pagination")
|
|
470
|
+
void testFindAllWithPagination() {
|
|
471
|
+
// Given
|
|
472
|
+
List<<%= entityName %>> entities = Arrays.asList(entity);
|
|
473
|
+
Page<<%= entityName %>> page = new PageImpl<>(entities, PageRequest.of(0, 10), 1);
|
|
474
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
475
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
476
|
+
|
|
477
|
+
when(repository.findAll(any(Pageable.class))).thenReturn(page);
|
|
478
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
479
|
+
.thenReturn(detailDto);
|
|
480
|
+
|
|
481
|
+
// When
|
|
482
|
+
Page<<%= classPrefix %>DetailDTO> result = service.findAll(PageRequest.of(0, 10), <%= classPrefix %>DetailDTO.class);
|
|
483
|
+
|
|
484
|
+
// Then
|
|
485
|
+
assertThat(result).isNotNull();
|
|
486
|
+
assertThat(result.getTotalElements()).isEqualTo(1);
|
|
487
|
+
assertThat(result.getContent()).hasSize(1);
|
|
488
|
+
verify(repository, times(1)).findAll(any(Pageable.class));
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
@Test
|
|
492
|
+
@DisplayName("Should find all entities")
|
|
493
|
+
void testFindAll() {
|
|
494
|
+
// Given
|
|
495
|
+
List<<%= entityName %>> entities = Arrays.asList(entity);
|
|
496
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
497
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
498
|
+
|
|
499
|
+
when(repository.findAll()).thenReturn(entities);
|
|
500
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
501
|
+
.thenReturn(detailDto);
|
|
502
|
+
|
|
503
|
+
// When
|
|
504
|
+
List<<%= classPrefix %>DetailDTO> result = service.findAll(<%= classPrefix %>DetailDTO.class);
|
|
505
|
+
|
|
506
|
+
// Then
|
|
507
|
+
assertThat(result).isNotNull();
|
|
508
|
+
assertThat(result).hasSize(1);
|
|
509
|
+
verify(repository, times(1)).findAll();
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Note: SimpliXBaseService doesn't have a count() method
|
|
513
|
+
// Count functionality is not available in the base service
|
|
514
|
+
|
|
515
|
+
@Test
|
|
516
|
+
@DisplayName("Should check if entity exists by ID")
|
|
517
|
+
void testExistsById() {
|
|
518
|
+
// Given
|
|
519
|
+
when(repository.existsById(<% if (idType === 'String') { %>testId<% } else { %>testId.toString()<% } %>)).thenReturn(true);
|
|
520
|
+
|
|
521
|
+
// When
|
|
522
|
+
boolean result = service.existsById(<% if (idType === 'String') { %>testId<% } else { %>testId.toString()<% } %>);
|
|
523
|
+
|
|
524
|
+
// Then
|
|
525
|
+
assertThat(result).isTrue();
|
|
526
|
+
verify(repository, times(1)).existsById(<% if (idType === 'String') { %>testId<% } else { %>testId.toString()<% } %>);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
<%
|
|
530
|
+
// Check if batch update is supported
|
|
531
|
+
const hasBatchUpdateFields = fields.some(field => {
|
|
532
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
533
|
+
return ymlField?.views?.includes('batchUpdate');
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
if (hasBatchUpdateFields) { %>
|
|
537
|
+
@Test
|
|
538
|
+
@DisplayName("Should perform batch update successfully")
|
|
539
|
+
void testBatchUpdate() {
|
|
540
|
+
// Given
|
|
541
|
+
Set<String> ids = new HashSet<>(Arrays.asList(<% if (idType === 'String') { %>testId<% } else { %>testId.toString()<% } %>));
|
|
542
|
+
List<<%= entityName %>> entities = Arrays.asList(entity);
|
|
543
|
+
when(repository.findAllById(ids)).thenReturn(entities);
|
|
544
|
+
when(repository.saveAll(anyList())).thenReturn(entities);
|
|
545
|
+
|
|
546
|
+
// When
|
|
547
|
+
<%= classPrefix %>BatchUpdateDTO batchDto = new <%= classPrefix %>BatchUpdateDTO();
|
|
548
|
+
batchDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>s(ids);
|
|
549
|
+
<%
|
|
550
|
+
// Set batch update fields
|
|
551
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
552
|
+
fields.forEach(field => {
|
|
553
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
554
|
+
if (ymlField?.views?.includes('batchUpdate') && !field.isRelation && !isExcludedField(field.name)) {
|
|
555
|
+
if (field.type === 'String') { %>
|
|
556
|
+
batchDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>("batch-<%= field.name %>");
|
|
557
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
558
|
+
batchDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(99);
|
|
559
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
560
|
+
batchDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(99L);
|
|
561
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
562
|
+
batchDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(true);
|
|
563
|
+
<% }
|
|
564
|
+
}
|
|
565
|
+
}); %>
|
|
566
|
+
|
|
567
|
+
service.batchUpdate(batchDto);
|
|
568
|
+
|
|
569
|
+
// Then
|
|
570
|
+
verify(repository, times(1)).findAllById(ids);
|
|
571
|
+
verify(repository, times(1)).saveAll(anyList());
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
@Test
|
|
575
|
+
@DisplayName("Should handle empty batch update")
|
|
576
|
+
void testBatchUpdateWithEmptyIds() {
|
|
577
|
+
// Given
|
|
578
|
+
Set<String> emptyIds = Collections.emptySet();
|
|
579
|
+
<%= classPrefix %>BatchUpdateDTO batchDto = new <%= classPrefix %>BatchUpdateDTO();
|
|
580
|
+
batchDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>s(emptyIds);
|
|
581
|
+
|
|
582
|
+
// When
|
|
583
|
+
service.batchUpdate(batchDto);
|
|
584
|
+
|
|
585
|
+
// Then
|
|
586
|
+
verify(repository, never()).findAllById(anyList());
|
|
587
|
+
verify(repository, never()).saveAll(anyList());
|
|
588
|
+
}
|
|
589
|
+
<% } %>
|
|
590
|
+
<%
|
|
591
|
+
// Generate auto-increment tests if sortOrderConfig is present
|
|
592
|
+
if (ymlConfig && ymlConfig.sortOrderConfig && ymlConfig.sortOrderConfig.autoIncrement) {
|
|
593
|
+
const field = ymlConfig.sortOrderConfig.field;
|
|
594
|
+
const upperField = field.charAt(0).toUpperCase() + field.slice(1);
|
|
595
|
+
const scopeFields = ymlConfig.sortOrderConfig.scopeFields || [];
|
|
596
|
+
const gap = ymlConfig.sortOrderConfig.gap || 100;
|
|
597
|
+
|
|
598
|
+
if (scopeFields.length === 0) {
|
|
599
|
+
// Global scope tests
|
|
600
|
+
%>
|
|
601
|
+
|
|
602
|
+
// =====================================
|
|
603
|
+
// Auto-increment <%= field %> Tests
|
|
604
|
+
// =====================================
|
|
605
|
+
|
|
606
|
+
@Test
|
|
607
|
+
@DisplayName("Should auto-increment <%= field %> when null")
|
|
608
|
+
void testCreate_AutoIncrements<%= upperField %>WhenNull() {
|
|
609
|
+
// Given
|
|
610
|
+
int currentMax = 500;
|
|
611
|
+
when(repository.findMax<%= upperField %>()).thenReturn(currentMax);
|
|
612
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
613
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
614
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(currentMax + <%= gap %>);
|
|
615
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
616
|
+
return savedEntity;
|
|
617
|
+
});
|
|
618
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
619
|
+
doAnswer(invocation -> {
|
|
620
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
621
|
+
target.set<%= upperField %>(null);
|
|
622
|
+
return null;
|
|
623
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
624
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
625
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
626
|
+
|
|
627
|
+
createDto.set<%= upperField %>(null);
|
|
628
|
+
|
|
629
|
+
// When
|
|
630
|
+
service.create(createDto);
|
|
631
|
+
|
|
632
|
+
// Then
|
|
633
|
+
verify(repository, times(1)).findMax<%= upperField %>();
|
|
634
|
+
verify(repository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
@Test
|
|
638
|
+
@DisplayName("Should auto-increment <%= field %> when zero")
|
|
639
|
+
void testCreate_AutoIncrements<%= upperField %>WhenZero() {
|
|
640
|
+
// Given
|
|
641
|
+
int currentMax = 300;
|
|
642
|
+
when(repository.findMax<%= upperField %>()).thenReturn(currentMax);
|
|
643
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
644
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
645
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(currentMax + <%= gap %>);
|
|
646
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
647
|
+
return savedEntity;
|
|
648
|
+
});
|
|
649
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
650
|
+
doAnswer(invocation -> {
|
|
651
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
652
|
+
target.set<%= upperField %>(0);
|
|
653
|
+
return null;
|
|
654
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
655
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
656
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
657
|
+
|
|
658
|
+
createDto.set<%= upperField %>(0);
|
|
659
|
+
|
|
660
|
+
// When
|
|
661
|
+
service.create(createDto);
|
|
662
|
+
|
|
663
|
+
// Then
|
|
664
|
+
verify(repository, times(1)).findMax<%= upperField %>();
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
@Test
|
|
668
|
+
@DisplayName("Should preserve explicit <%= field %> value")
|
|
669
|
+
void testCreate_PreservesExplicit<%= upperField %>() {
|
|
670
|
+
// Given
|
|
671
|
+
int explicitValue = 999;
|
|
672
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
673
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
674
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(explicitValue);
|
|
675
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
676
|
+
return savedEntity;
|
|
677
|
+
});
|
|
678
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
679
|
+
doAnswer(invocation -> {
|
|
680
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
681
|
+
target.set<%= upperField %>(explicitValue);
|
|
682
|
+
return null;
|
|
683
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
684
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
685
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
686
|
+
|
|
687
|
+
createDto.set<%= upperField %>(explicitValue);
|
|
688
|
+
|
|
689
|
+
// When
|
|
690
|
+
service.create(createDto);
|
|
691
|
+
|
|
692
|
+
// Then
|
|
693
|
+
verify(repository, never()).findMax<%= upperField %>();
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
@Test
|
|
697
|
+
@DisplayName("Should set <%= field %> to <%= gap %> for first entity")
|
|
698
|
+
void testCreate_FirstEntityGets<%= gap %>() {
|
|
699
|
+
// Given
|
|
700
|
+
when(repository.findMax<%= upperField %>()).thenReturn(0);
|
|
701
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
702
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
703
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(<%= gap %>);
|
|
704
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
705
|
+
return savedEntity;
|
|
706
|
+
});
|
|
707
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
708
|
+
doAnswer(invocation -> {
|
|
709
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
710
|
+
target.set<%= upperField %>(null);
|
|
711
|
+
return null;
|
|
712
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
713
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
714
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
715
|
+
|
|
716
|
+
createDto.set<%= upperField %>(null);
|
|
717
|
+
|
|
718
|
+
// When
|
|
719
|
+
service.create(createDto);
|
|
720
|
+
|
|
721
|
+
// Then
|
|
722
|
+
verify(repository, times(1)).findMax<%= upperField %>();
|
|
723
|
+
}
|
|
724
|
+
<% } else {
|
|
725
|
+
// Scoped tests
|
|
726
|
+
const firstScopeField = scopeFields[0];
|
|
727
|
+
const upperFirstScopeField = firstScopeField.charAt(0).toUpperCase() + firstScopeField.slice(1);
|
|
728
|
+
const methodSuffix = scopeFields.map(f => f.charAt(0).toUpperCase() + f.slice(1)).join('And');
|
|
729
|
+
%>
|
|
730
|
+
|
|
731
|
+
// =====================================
|
|
732
|
+
// Auto-increment <%= field %> Tests (Scoped by <%= scopeFields.join(', ') %>)
|
|
733
|
+
// =====================================
|
|
734
|
+
|
|
735
|
+
@Test
|
|
736
|
+
@DisplayName("Should auto-increment <%= field %> when null (scoped)")
|
|
737
|
+
void testCreate_AutoIncrements<%= upperField %>WhenNull_Scoped() {
|
|
738
|
+
// Given
|
|
739
|
+
int currentMax = 500;
|
|
740
|
+
String scopeId = UUID.randomUUID().toString();
|
|
741
|
+
when(repository.findMax<%= upperField %>By<%= methodSuffix %>(scopeId)).thenReturn(currentMax);
|
|
742
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
743
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
744
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(currentMax + <%= gap %>);
|
|
745
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
746
|
+
return savedEntity;
|
|
747
|
+
});
|
|
748
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
749
|
+
doAnswer(invocation -> {
|
|
750
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
751
|
+
target.set<%= upperField %>(null);
|
|
752
|
+
return null;
|
|
753
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
754
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
755
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
756
|
+
|
|
757
|
+
createDto.set<%= upperField %>(null);
|
|
758
|
+
createDto.set<%= upperFirstScopeField %>(scopeId);
|
|
759
|
+
|
|
760
|
+
// When
|
|
761
|
+
service.create(createDto);
|
|
762
|
+
|
|
763
|
+
// Then
|
|
764
|
+
verify(repository, times(1)).findMax<%= upperField %>By<%= methodSuffix %>(scopeId);
|
|
765
|
+
verify(repository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
@Test
|
|
769
|
+
@DisplayName("Should auto-increment <%= field %> when zero (scoped)")
|
|
770
|
+
void testCreate_AutoIncrements<%= upperField %>WhenZero_Scoped() {
|
|
771
|
+
// Given
|
|
772
|
+
int currentMax = 300;
|
|
773
|
+
String scopeId = UUID.randomUUID().toString();
|
|
774
|
+
when(repository.findMax<%= upperField %>By<%= methodSuffix %>(scopeId)).thenReturn(currentMax);
|
|
775
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
776
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
777
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(currentMax + <%= gap %>);
|
|
778
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
779
|
+
return savedEntity;
|
|
780
|
+
});
|
|
781
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
782
|
+
doAnswer(invocation -> {
|
|
783
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
784
|
+
target.set<%= upperField %>(0);
|
|
785
|
+
return null;
|
|
786
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
787
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
788
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
789
|
+
|
|
790
|
+
createDto.set<%= upperField %>(0);
|
|
791
|
+
createDto.set<%= upperFirstScopeField %>(scopeId);
|
|
792
|
+
|
|
793
|
+
// When
|
|
794
|
+
service.create(createDto);
|
|
795
|
+
|
|
796
|
+
// Then
|
|
797
|
+
verify(repository, times(1)).findMax<%= upperField %>By<%= methodSuffix %>(scopeId);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
@Test
|
|
801
|
+
@DisplayName("Should preserve explicit <%= field %> value (scoped)")
|
|
802
|
+
void testCreate_PreservesExplicit<%= upperField %>_Scoped() {
|
|
803
|
+
// Given
|
|
804
|
+
int explicitValue = 999;
|
|
805
|
+
String scopeId = UUID.randomUUID().toString();
|
|
806
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
807
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
808
|
+
assertThat(savedEntity.get<%= upperField %>()).isEqualTo(explicitValue);
|
|
809
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
810
|
+
return savedEntity;
|
|
811
|
+
});
|
|
812
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
813
|
+
doAnswer(invocation -> {
|
|
814
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
815
|
+
target.set<%= upperField %>(explicitValue);
|
|
816
|
+
return null;
|
|
817
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
818
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
819
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
820
|
+
|
|
821
|
+
createDto.set<%= upperField %>(explicitValue);
|
|
822
|
+
createDto.set<%= upperFirstScopeField %>(scopeId);
|
|
823
|
+
|
|
824
|
+
// When
|
|
825
|
+
service.create(createDto);
|
|
826
|
+
|
|
827
|
+
// Then
|
|
828
|
+
verify(repository, never()).findMax<%= upperField %>By<%= methodSuffix %>(anyString());
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
@Test
|
|
832
|
+
@DisplayName("Should skip auto-increment when scope field is null")
|
|
833
|
+
void testCreate_SkipsAutoIncrementWhenScopeNull() {
|
|
834
|
+
// Given
|
|
835
|
+
when(repository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
836
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
837
|
+
savedEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
838
|
+
return savedEntity;
|
|
839
|
+
});
|
|
840
|
+
when(repository.findById(any())).thenReturn(Optional.of(entity));
|
|
841
|
+
doAnswer(invocation -> {
|
|
842
|
+
<%= entityName %> target = invocation.getArgument(1);
|
|
843
|
+
target.set<%= upperField %>(null);
|
|
844
|
+
return null;
|
|
845
|
+
}).when(modelMapper).map(any(<%= classPrefix %>CreateDTO.class), any(<%= entityName %>.class));
|
|
846
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
847
|
+
.thenReturn(new <%= classPrefix %>DetailDTO());
|
|
848
|
+
|
|
849
|
+
createDto.set<%= upperField %>(null);
|
|
850
|
+
createDto.set<%= upperFirstScopeField %>(null);
|
|
851
|
+
|
|
852
|
+
// When
|
|
853
|
+
service.create(createDto);
|
|
854
|
+
|
|
855
|
+
// Then
|
|
856
|
+
verify(repository, never()).findMax<%= upperField %>By<%= methodSuffix %>(anyString());
|
|
857
|
+
}
|
|
858
|
+
<% }
|
|
859
|
+
} %>
|
|
860
|
+
<% if (statusCountFields.length > 0) { %>
|
|
861
|
+
@Test
|
|
862
|
+
@DisplayName("statusCounts - refuses a field the list does not count by")
|
|
863
|
+
void statusCounts_unknownField_refused() {
|
|
864
|
+
assertThatThrownBy(() -> service.statusCounts("notAListedField", Map.of()))
|
|
865
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
866
|
+
.hasMessageContaining("error.search.countFieldUnknown");
|
|
867
|
+
verifyNoInteractions(searchAggregator);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
@Test
|
|
871
|
+
@DisplayName("statusCounts - names an enum value by its constant")
|
|
872
|
+
void statusCounts_listedField_namesEachValue() {
|
|
873
|
+
<% if (figureScope) { -%>
|
|
874
|
+
service = spy(service);
|
|
875
|
+
doReturn(<%- scopedValue %>).when(service).<%= figureScope %>(any());
|
|
876
|
+
<% } -%>
|
|
877
|
+
when(searchAggregator.countByValue(eq(<%= entityName %>.class), eq(repository), any(), eq("<%= statusCountFields[0] %>")))
|
|
878
|
+
.thenReturn(List.of(new ValueCount(Thread.State.NEW, 3L)));
|
|
879
|
+
|
|
880
|
+
assertThat(service.statusCounts("<%= statusCountFields[0] %>", Map.of("field", "<%= statusCountFields[0] %>")))
|
|
881
|
+
.singleElement()
|
|
882
|
+
.satisfies(count -> {
|
|
883
|
+
assertThat(count.value()).isEqualTo("NEW");
|
|
884
|
+
assertThat(count.count()).isEqualTo(3L);
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
<% } %>
|
|
888
|
+
<% if (aggregateItems.length > 0) { %>
|
|
889
|
+
@Test
|
|
890
|
+
@DisplayName("aggregates - asks for every listed figure in order")
|
|
891
|
+
void aggregates_asksForEveryListedFigure() {
|
|
892
|
+
<% if (figureScope) { -%>
|
|
893
|
+
service = spy(service);
|
|
894
|
+
doReturn(<%- scopedValue %>).when(service).<%= figureScope %>(any());
|
|
895
|
+
<% } -%>
|
|
896
|
+
when(searchAggregator.aggregate(eq(<%= entityName %>.class), eq(repository), any(), anyList()))
|
|
897
|
+
.thenAnswer(call -> ((List<SearchAggregator.Aggregate>) call.getArgument(3)).stream()
|
|
898
|
+
.map(a -> new AggregateValue(a.field(), a.op().wireName(), 0L)).toList());
|
|
899
|
+
|
|
900
|
+
assertThat(service.aggregates(Map.of()))
|
|
901
|
+
.extracting(figure -> figure.field() + ":" + figure.op())
|
|
902
|
+
.containsExactly(<%- aggregateItems.map(a => `"${a.field}:${a.op}"`).join(', ') %>);
|
|
903
|
+
}
|
|
904
|
+
<% } %>
|
|
905
|
+
<% if (figureScope) { %>
|
|
906
|
+
@Test
|
|
907
|
+
@DisplayName("figures - are read under the scope the list is forced to")
|
|
908
|
+
void figures_readUnderTheForcedScope() {
|
|
909
|
+
service = spy(service);
|
|
910
|
+
doReturn(<%- scopedValue %>).when(service).<%= figureScope %>(any());
|
|
911
|
+
<% if (statusCountFields.length > 0) { -%>
|
|
912
|
+
service.statusCounts("<%= statusCountFields[0] %>", Map.of("field", "<%= statusCountFields[0] %>"));
|
|
913
|
+
<% } -%>
|
|
914
|
+
<% if (aggregateItems.length > 0) { -%>
|
|
915
|
+
service.aggregates(Map.of());
|
|
916
|
+
<% } -%>
|
|
917
|
+
verify(service, times(<%= (statusCountFields.length > 0 ? 1 : 0) + (aggregateItems.length > 0 ? 1 : 0) %>)).<%= figureScope %>(any());
|
|
918
|
+
}
|
|
919
|
+
<% if (scopeMayMatchNothing) { %>
|
|
920
|
+
@Test
|
|
921
|
+
@DisplayName("figures - a caller who reaches no row reads empty figures without a query")
|
|
922
|
+
void figures_emptyScopeReadsNothing() {
|
|
923
|
+
service = spy(service);
|
|
924
|
+
doReturn(Optional.empty()).when(service).<%= figureScope %>(any());
|
|
925
|
+
<% if (statusCountFields.length > 0) { -%>
|
|
926
|
+
assertThat(service.statusCounts("<%= statusCountFields[0] %>", Map.of())).isEmpty();
|
|
927
|
+
<% } -%>
|
|
928
|
+
<% if (aggregateItems.length > 0) { -%>
|
|
929
|
+
assertThat(service.aggregates(Map.of()))
|
|
930
|
+
.extracting(figure -> figure.field() + ":" + figure.op())
|
|
931
|
+
.containsExactly(<%- aggregateItems.map(a => `"${a.field}:${a.op}"`).join(', ') %>);
|
|
932
|
+
<% } -%>
|
|
933
|
+
verifyNoInteractions(searchAggregator);
|
|
934
|
+
}
|
|
935
|
+
<% } %>
|
|
936
|
+
<% } %>
|
|
937
|
+
}
|