@simplix-boot/cli 0.0.0-stage → 1.3.0
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.md +146 -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,842 @@
|
|
|
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
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>ListDTO;
|
|
7
|
+
<% if (hasOrderField) { %>
|
|
8
|
+
import <%= packagePaths.dto %>.<%= classPrefix %>DTOs.<%= classPrefix %>OrderUpdateDTO;
|
|
9
|
+
<% } %>
|
|
10
|
+
import <%= entityPackage %>.<%= entityName %>;
|
|
11
|
+
<% if (hasEmbeddedId) { %>
|
|
12
|
+
import <%= entityPackage %>.<%= entityName %>.<%= embeddedIdType %>;
|
|
13
|
+
<% } %>
|
|
14
|
+
<%
|
|
15
|
+
// Parse import statements for reference entities (excluding self-reference)
|
|
16
|
+
// Only import entity classes for immutable reference field tests
|
|
17
|
+
const importedEntities = new Set();
|
|
18
|
+
|
|
19
|
+
if (ymlConfig && ymlConfig.fields) {
|
|
20
|
+
Object.entries(ymlConfig.fields).forEach(([fieldName, ymlField]) => {
|
|
21
|
+
if (ymlField.reference && ymlField.reference.entity && ymlField.reference.entity !== entityName) {
|
|
22
|
+
const refEntity = ymlField.reference.entity;
|
|
23
|
+
const refModulePath = ymlField.reference.modulePath;
|
|
24
|
+
|
|
25
|
+
// Import entity class for immutable reference fields (used in test assertions)
|
|
26
|
+
if (ymlField.immutable && !importedEntities.has(refEntity)) {
|
|
27
|
+
importedEntities.add(refEntity);
|
|
28
|
+
if (refModulePath) {
|
|
29
|
+
const entityPackagePath = basePackage.replace('.api', '') + '.domain.entity.' + refModulePath.replace(/\//g, '.');
|
|
30
|
+
%>
|
|
31
|
+
import <%= entityPackagePath %>.<%= refEntity %>;<%
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
} %>
|
|
37
|
+
|
|
38
|
+
import dev.simplecore.simplix.core.exception.ErrorCode;
|
|
39
|
+
import dev.simplecore.simplix.core.exception.SimpliXGeneralException;
|
|
40
|
+
import dev.simplecore.simplix.core.tree.repository.SimpliXTreeRepository;
|
|
41
|
+
import org.junit.jupiter.api.BeforeEach;
|
|
42
|
+
import org.junit.jupiter.api.Test;
|
|
43
|
+
import org.junit.jupiter.api.DisplayName;
|
|
44
|
+
import org.junit.jupiter.api.extension.ExtendWith;
|
|
45
|
+
import org.mockito.Mock;
|
|
46
|
+
import org.mockito.junit.jupiter.MockitoExtension;
|
|
47
|
+
import org.mockito.junit.jupiter.MockitoSettings;
|
|
48
|
+
import org.mockito.quality.Strictness;
|
|
49
|
+
import org.modelmapper.ModelMapper;
|
|
50
|
+
import org.springframework.context.MessageSource;
|
|
51
|
+
|
|
52
|
+
import java.time.Instant;
|
|
53
|
+
import java.util.*;
|
|
54
|
+
|
|
55
|
+
import static org.assertj.core.api.Assertions.assertThat;
|
|
56
|
+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
|
57
|
+
import static org.mockito.ArgumentMatchers.*;
|
|
58
|
+
import static org.mockito.Mockito.*;
|
|
59
|
+
|
|
60
|
+
<%
|
|
61
|
+
// Build excluded fields set from generator-simplix.json configuration (same as DTO template)
|
|
62
|
+
const excludedFieldsSet = new Set(dtoExcludeFields || []);
|
|
63
|
+
|
|
64
|
+
// Helper: Check if a field should be excluded from DTO generation
|
|
65
|
+
function isExcludedField(fieldName) {
|
|
66
|
+
return excludedFieldsSet.has(fieldName);
|
|
67
|
+
}
|
|
68
|
+
%>
|
|
69
|
+
/**
|
|
70
|
+
* Unit tests for <%= classPrefix %>TreeService
|
|
71
|
+
* <p>
|
|
72
|
+
* This test class verifies:
|
|
73
|
+
* <ul>
|
|
74
|
+
* <li>Tree node CRUD operations (createNode, updateNode, deleteNode)</li>
|
|
75
|
+
* <li>Tree structure operations (findTree, findSubtree)</li>
|
|
76
|
+
* <li>Children validation for delete operations</li>
|
|
77
|
+
* <li>Order update operations</li>
|
|
78
|
+
* <li>Exception handling</li>
|
|
79
|
+
* </ul>
|
|
80
|
+
*/
|
|
81
|
+
@ExtendWith(MockitoExtension.class)
|
|
82
|
+
@MockitoSettings(strictness = Strictness.LENIENT)
|
|
83
|
+
@DisplayName("<%= classPrefix %>TreeService Tests")
|
|
84
|
+
class <%= classPrefix %>TreeServiceTest {
|
|
85
|
+
|
|
86
|
+
@Mock
|
|
87
|
+
private SimpliXTreeRepository<<%= entityName %>, <%= idType %>> treeRepository;
|
|
88
|
+
|
|
89
|
+
@Mock
|
|
90
|
+
private MessageSource messageSource;
|
|
91
|
+
|
|
92
|
+
@Mock
|
|
93
|
+
private ModelMapper modelMapper;
|
|
94
|
+
|
|
95
|
+
private <%= classPrefix %>TreeService treeService;
|
|
96
|
+
|
|
97
|
+
private <%= entityName %> entity;
|
|
98
|
+
private <%= entityName %> childEntity;
|
|
99
|
+
private <%= classPrefix %>CreateDTO createDto;
|
|
100
|
+
private <%= classPrefix %>UpdateDTO updateDto;
|
|
101
|
+
<% if (hasEmbeddedId) { %>
|
|
102
|
+
private <%= embeddedIdType %> embeddedId;
|
|
103
|
+
<% } else { %>
|
|
104
|
+
private <%= idType %> testId;
|
|
105
|
+
private <%= idType %> childId;
|
|
106
|
+
<% } %>
|
|
107
|
+
|
|
108
|
+
@BeforeEach
|
|
109
|
+
void setUp() {
|
|
110
|
+
treeService = new <%= classPrefix %>TreeService(
|
|
111
|
+
treeRepository,
|
|
112
|
+
modelMapper,
|
|
113
|
+
messageSource
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
<% if (hasEmbeddedId) { %>
|
|
117
|
+
// Setup embedded ID
|
|
118
|
+
embeddedId = new <%= embeddedIdType %>();
|
|
119
|
+
<% embeddedIdFields.forEach(field => { %>
|
|
120
|
+
embeddedId.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(<%=
|
|
121
|
+
field.type === 'String' ? '"test-' + field.name + '"' :
|
|
122
|
+
field.type === 'Long' ? '1L' :
|
|
123
|
+
field.type === 'Integer' ? '1' :
|
|
124
|
+
field.type === 'UUID' ? 'UUID.randomUUID()' :
|
|
125
|
+
'"test"'
|
|
126
|
+
%>);
|
|
127
|
+
<% }); %>
|
|
128
|
+
<% } else { %>
|
|
129
|
+
testId = <%= idType === 'String' ? '"test-id-1"' :
|
|
130
|
+
idType === 'Long' ? '1L' :
|
|
131
|
+
idType === 'Integer' ? '1' :
|
|
132
|
+
idType === 'UUID' ? 'UUID.randomUUID()' : '1' %>;
|
|
133
|
+
childId = <%= idType === 'String' ? '"child-id-1"' :
|
|
134
|
+
idType === 'Long' ? '2L' :
|
|
135
|
+
idType === 'Integer' ? '2' :
|
|
136
|
+
idType === 'UUID' ? 'UUID.randomUUID()' : '2' %>;
|
|
137
|
+
<% } %>
|
|
138
|
+
|
|
139
|
+
// Setup test entity (root node)
|
|
140
|
+
entity = new <%= entityName %>();
|
|
141
|
+
<% if (!hasEmbeddedId) { %>entity.setId(testId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
142
|
+
<% if (hasEmbeddedId) { %>
|
|
143
|
+
entity.set<%= embeddedIdField.charAt(0).toUpperCase() + embeddedIdField.slice(1) %>(embeddedId);
|
|
144
|
+
<% } else { %>
|
|
145
|
+
entity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);
|
|
146
|
+
<% } %>
|
|
147
|
+
entity.setParentId(null); // Root node
|
|
148
|
+
<%
|
|
149
|
+
// Set fields that are in 'edit' view (same as CreateDTO)
|
|
150
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
151
|
+
fields.forEach(field => {
|
|
152
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
153
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && field.name !== 'parentId' && !field.isRelation && !isExcludedField(field.name)) {
|
|
154
|
+
if (field.type === 'String') { %>
|
|
155
|
+
entity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>("test-<%= field.name %>");
|
|
156
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
157
|
+
entity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(1);
|
|
158
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
159
|
+
entity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(1L);
|
|
160
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
161
|
+
entity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(true);
|
|
162
|
+
<% }
|
|
163
|
+
}
|
|
164
|
+
}); %>
|
|
165
|
+
entity.setCreatedAt(Instant.now());
|
|
166
|
+
entity.setUpdatedAt(Instant.now());
|
|
167
|
+
|
|
168
|
+
// Setup child entity
|
|
169
|
+
childEntity = new <%= entityName %>();
|
|
170
|
+
<% if (!hasEmbeddedId) { %>childEntity.setId(childId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
171
|
+
childEntity.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);
|
|
172
|
+
childEntity.setParentId(testId); // Child of test entity
|
|
173
|
+
<%
|
|
174
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
175
|
+
fields.forEach(field => {
|
|
176
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
177
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && field.name !== 'parentId' && !field.isRelation && !isExcludedField(field.name)) {
|
|
178
|
+
if (field.type === 'String') { %>
|
|
179
|
+
childEntity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>("child-<%= field.name %>");
|
|
180
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
181
|
+
childEntity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(2);
|
|
182
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
183
|
+
childEntity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(2L);
|
|
184
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
185
|
+
childEntity.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(true);
|
|
186
|
+
<% }
|
|
187
|
+
}
|
|
188
|
+
}); %>
|
|
189
|
+
childEntity.setCreatedAt(Instant.now());
|
|
190
|
+
childEntity.setUpdatedAt(Instant.now());
|
|
191
|
+
|
|
192
|
+
// Setup DTOs
|
|
193
|
+
createDto = new <%= classPrefix %>CreateDTO();
|
|
194
|
+
createDto.setParentId(null); // Root node
|
|
195
|
+
<%
|
|
196
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
197
|
+
fields.forEach(field => {
|
|
198
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
199
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && field.name !== 'parentId' && !field.isRelation && !isExcludedField(field.name)) {
|
|
200
|
+
if (field.type === 'String') { %>
|
|
201
|
+
createDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>("test-<%= field.name %>");
|
|
202
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
203
|
+
createDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(1);
|
|
204
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
205
|
+
createDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(1L);
|
|
206
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
207
|
+
createDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(true);
|
|
208
|
+
<% }
|
|
209
|
+
}
|
|
210
|
+
}); %>
|
|
211
|
+
|
|
212
|
+
updateDto = new <%= classPrefix %>UpdateDTO();
|
|
213
|
+
<% if (!hasEmbeddedId) { %>updateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
214
|
+
updateDto.setParentId(null); // Keep as root
|
|
215
|
+
<%
|
|
216
|
+
// Skip excluded fields (audit fields like createdBy, updatedBy, etc.)
|
|
217
|
+
fields.forEach(field => {
|
|
218
|
+
const ymlField = ymlConfig.fields?.[field.name];
|
|
219
|
+
if (ymlField?.views?.includes('edit') && field.name !== idFieldName && field.name !== 'parentId' && !field.isRelation && !isExcludedField(field.name)) {
|
|
220
|
+
if (field.type === 'String') { %>
|
|
221
|
+
updateDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>("updated-<%= field.name %>");
|
|
222
|
+
<% } else if (field.type === 'Integer' || field.type === 'int') { %>
|
|
223
|
+
updateDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(2);
|
|
224
|
+
<% } else if (field.type === 'Long' || field.type === 'long') { %>
|
|
225
|
+
updateDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(2L);
|
|
226
|
+
<% } else if (field.type === 'Boolean' || field.type === 'boolean') { %>
|
|
227
|
+
updateDto.set<%= field.name.charAt(0).toUpperCase() + field.name.slice(1) %>(false);
|
|
228
|
+
<% }
|
|
229
|
+
}
|
|
230
|
+
}); %>
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
//----------------------------------
|
|
234
|
+
// Tree Node CRUD Tests
|
|
235
|
+
//----------------------------------
|
|
236
|
+
|
|
237
|
+
@Test
|
|
238
|
+
@DisplayName("Should create root node successfully")
|
|
239
|
+
void testCreateRootNode() {
|
|
240
|
+
// Given
|
|
241
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
242
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
243
|
+
|
|
244
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
245
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
246
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
247
|
+
.thenReturn(detailDto);
|
|
248
|
+
|
|
249
|
+
// When
|
|
250
|
+
<%= classPrefix %>DetailDTO result = treeService.createNode(createDto);
|
|
251
|
+
|
|
252
|
+
// Then
|
|
253
|
+
assertThat(result).isNotNull();
|
|
254
|
+
<% if (!hasEmbeddedId) { %>
|
|
255
|
+
assertThat(result.get<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>()).isEqualTo(testId);
|
|
256
|
+
<% } %>
|
|
257
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
@Test
|
|
261
|
+
@DisplayName("Should create child node successfully")
|
|
262
|
+
void testCreateChildNode() {
|
|
263
|
+
// Given
|
|
264
|
+
createDto.setParentId(testId);
|
|
265
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
266
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
267
|
+
detailDto.setParentId(testId);
|
|
268
|
+
|
|
269
|
+
// Mock parent validation
|
|
270
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
271
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(childEntity);
|
|
272
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
273
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
274
|
+
.thenReturn(detailDto);
|
|
275
|
+
|
|
276
|
+
// When
|
|
277
|
+
<%= classPrefix %>DetailDTO result = treeService.createNode(createDto);
|
|
278
|
+
|
|
279
|
+
// Then
|
|
280
|
+
assertThat(result).isNotNull();
|
|
281
|
+
assertThat(result.getParentId()).isEqualTo(testId);
|
|
282
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
@Test
|
|
286
|
+
@DisplayName("Should handle empty parentId as null for root node")
|
|
287
|
+
void testCreateNodeWithEmptyParentId() {
|
|
288
|
+
// Given
|
|
289
|
+
createDto.setParentId(<%= idType === 'String' ? '""' : 'null' %>); // Empty string for root
|
|
290
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
291
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
292
|
+
detailDto.setParentId(null);
|
|
293
|
+
|
|
294
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
295
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
296
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
297
|
+
.thenReturn(detailDto);
|
|
298
|
+
|
|
299
|
+
// When
|
|
300
|
+
<%= classPrefix %>DetailDTO result = treeService.createNode(createDto);
|
|
301
|
+
|
|
302
|
+
// Then
|
|
303
|
+
assertThat(result).isNotNull();
|
|
304
|
+
assertThat(result.getParentId()).isNull();
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
@Test
|
|
308
|
+
@DisplayName("Should update node successfully")
|
|
309
|
+
void testUpdateNode() {
|
|
310
|
+
// Given
|
|
311
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
312
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
313
|
+
|
|
314
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
315
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
316
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
317
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
318
|
+
.thenReturn(detailDto);
|
|
319
|
+
|
|
320
|
+
// When
|
|
321
|
+
<%= classPrefix %>DetailDTO result = treeService.updateNode(entity, updateDto);
|
|
322
|
+
|
|
323
|
+
// Then
|
|
324
|
+
assertThat(result).isNotNull();
|
|
325
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
@Test
|
|
329
|
+
@DisplayName("Should throw exception when ID is changed during update")
|
|
330
|
+
void testUpdateNodeWithChangedId() {
|
|
331
|
+
// Given
|
|
332
|
+
<% if (!hasEmbeddedId) { %>
|
|
333
|
+
updateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId); // Different ID
|
|
334
|
+
<% } %>
|
|
335
|
+
// When/Then
|
|
336
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
337
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
338
|
+
.hasMessageContaining("error.id.cannot.change");
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
@Test
|
|
342
|
+
@DisplayName("Should delete node without children successfully")
|
|
343
|
+
void testDeleteNodeWithoutChildren() {
|
|
344
|
+
// Given
|
|
345
|
+
when(treeRepository.findDirectChildren(testId)).thenReturn(Collections.emptyList());
|
|
346
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
347
|
+
when(treeRepository.findItemWithAllDescendants(testId)).thenReturn(Arrays.asList(entity));
|
|
348
|
+
doNothing().when(treeRepository).deleteById(testId);
|
|
349
|
+
|
|
350
|
+
// When
|
|
351
|
+
treeService.deleteNode(testId);
|
|
352
|
+
|
|
353
|
+
// Then
|
|
354
|
+
verify(treeRepository, times(1)).findDirectChildren(testId);
|
|
355
|
+
verify(treeRepository, times(1)).deleteById(testId);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
@Test
|
|
359
|
+
@DisplayName("Should throw exception when deleting node with children")
|
|
360
|
+
void testDeleteNodeWithChildren() {
|
|
361
|
+
// Given
|
|
362
|
+
when(treeRepository.findDirectChildren(testId)).thenReturn(Arrays.asList(childEntity));
|
|
363
|
+
when(messageSource.getMessage(eq("error.tree.has.children"), any(), anyString(), any()))
|
|
364
|
+
.thenReturn("Cannot delete node with children");
|
|
365
|
+
|
|
366
|
+
// When/Then
|
|
367
|
+
assertThatThrownBy(() -> treeService.deleteNode(testId))
|
|
368
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
369
|
+
.hasMessageContaining("Cannot delete node with children");
|
|
370
|
+
|
|
371
|
+
verify(treeRepository, never()).deleteById(any());
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
@Test
|
|
375
|
+
@DisplayName("Should batch delete nodes without children")
|
|
376
|
+
void testBatchDeleteNodesWithoutChildren() {
|
|
377
|
+
// Given
|
|
378
|
+
List<<%= idType %>> ids = Arrays.asList(testId, childId);
|
|
379
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
380
|
+
when(treeRepository.findById(childId)).thenReturn(Optional.of(childEntity));
|
|
381
|
+
when(treeRepository.findDirectChildren(any())).thenReturn(Collections.emptyList());
|
|
382
|
+
when(treeRepository.findItemWithAllDescendants(any())).thenReturn(Collections.emptyList());
|
|
383
|
+
doNothing().when(treeRepository).deleteAllById(ids);
|
|
384
|
+
|
|
385
|
+
// When
|
|
386
|
+
treeService.batchDeleteNodes(ids);
|
|
387
|
+
|
|
388
|
+
// Then
|
|
389
|
+
// findDirectChildren is called twice per ID: once in batchDeleteNodes() validation, once in deleteBatch()
|
|
390
|
+
verify(treeRepository, atLeast(2)).findDirectChildren(any());
|
|
391
|
+
verify(treeRepository, times(1)).deleteAllById(ids);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
@Test
|
|
395
|
+
@DisplayName("Should throw exception when batch deleting node with children")
|
|
396
|
+
void testBatchDeleteNodesWithChildren() {
|
|
397
|
+
// Given
|
|
398
|
+
List<<%= idType %>> ids = Arrays.asList(testId);
|
|
399
|
+
when(treeRepository.findDirectChildren(testId)).thenReturn(Arrays.asList(childEntity));
|
|
400
|
+
when(messageSource.getMessage(eq("error.tree.has.children"), any(), anyString(), any()))
|
|
401
|
+
.thenReturn("Cannot delete node with children");
|
|
402
|
+
|
|
403
|
+
// When/Then
|
|
404
|
+
assertThatThrownBy(() -> treeService.batchDeleteNodes(ids))
|
|
405
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
406
|
+
.hasMessageContaining("Cannot delete node with children");
|
|
407
|
+
|
|
408
|
+
verify(treeRepository, never()).deleteAllById(any());
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
//----------------------------------
|
|
412
|
+
// Tree Structure Tests
|
|
413
|
+
//----------------------------------
|
|
414
|
+
|
|
415
|
+
@Test
|
|
416
|
+
@DisplayName("Should find root nodes only")
|
|
417
|
+
void testFindTreeRootsOnly() {
|
|
418
|
+
// Given
|
|
419
|
+
when(treeRepository.findRootItems()).thenReturn(Arrays.asList(entity));
|
|
420
|
+
<%= classPrefix %>ListDTO listDto = new <%= classPrefix %>ListDTO();
|
|
421
|
+
<% if (!hasEmbeddedId) { %>listDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
422
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>ListDTO.class)))
|
|
423
|
+
.thenReturn(listDto);
|
|
424
|
+
|
|
425
|
+
// When
|
|
426
|
+
List<<%= classPrefix %>ListDTO> result = treeService.findTree(false);
|
|
427
|
+
|
|
428
|
+
// Then
|
|
429
|
+
assertThat(result).isNotNull();
|
|
430
|
+
assertThat(result).hasSize(1);
|
|
431
|
+
verify(treeRepository, times(1)).findRootItems();
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
@Test
|
|
435
|
+
@DisplayName("Should find complete tree hierarchy")
|
|
436
|
+
void testFindTreeFullHierarchy() {
|
|
437
|
+
// Given
|
|
438
|
+
when(treeRepository.findCompleteHierarchy()).thenReturn(Arrays.asList(entity, childEntity));
|
|
439
|
+
<%= classPrefix %>ListDTO listDto1 = new <%= classPrefix %>ListDTO();
|
|
440
|
+
<% if (!hasEmbeddedId) { %>listDto1.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
441
|
+
<%= classPrefix %>ListDTO listDto2 = new <%= classPrefix %>ListDTO();
|
|
442
|
+
<% if (!hasEmbeddedId) { %>listDto2.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
443
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>ListDTO.class)))
|
|
444
|
+
.thenReturn(listDto1, listDto2);
|
|
445
|
+
|
|
446
|
+
// When
|
|
447
|
+
List<<%= classPrefix %>ListDTO> result = treeService.findTree(true);
|
|
448
|
+
|
|
449
|
+
// Then
|
|
450
|
+
assertThat(result).isNotNull();
|
|
451
|
+
assertThat(result).hasSize(2);
|
|
452
|
+
verify(treeRepository, times(1)).findCompleteHierarchy();
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
@Test
|
|
456
|
+
@DisplayName("Should find direct children of a node")
|
|
457
|
+
void testFindTreeDirectChildren() {
|
|
458
|
+
// Given
|
|
459
|
+
when(treeRepository.findDirectChildren(testId)).thenReturn(Arrays.asList(childEntity));
|
|
460
|
+
<%= classPrefix %>ListDTO listDto = new <%= classPrefix %>ListDTO();
|
|
461
|
+
<% if (!hasEmbeddedId) { %>listDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
462
|
+
listDto.setParentId(testId);
|
|
463
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>ListDTO.class)))
|
|
464
|
+
.thenReturn(listDto);
|
|
465
|
+
|
|
466
|
+
// When
|
|
467
|
+
List<<%= classPrefix %>ListDTO> result = treeService.findTree(testId, false);
|
|
468
|
+
|
|
469
|
+
// Then
|
|
470
|
+
assertThat(result).isNotNull();
|
|
471
|
+
assertThat(result).hasSize(1);
|
|
472
|
+
verify(treeRepository, times(1)).findDirectChildren(testId);
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
@Test
|
|
476
|
+
@DisplayName("Should find all descendants of a node")
|
|
477
|
+
void testFindTreeAllDescendants() {
|
|
478
|
+
// Given
|
|
479
|
+
when(treeRepository.findItemWithAllDescendants(testId)).thenReturn(Arrays.asList(childEntity));
|
|
480
|
+
<%= classPrefix %>ListDTO listDto = new <%= classPrefix %>ListDTO();
|
|
481
|
+
<% if (!hasEmbeddedId) { %>listDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
482
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>ListDTO.class)))
|
|
483
|
+
.thenReturn(listDto);
|
|
484
|
+
|
|
485
|
+
// When
|
|
486
|
+
List<<%= classPrefix %>ListDTO> result = treeService.findTree(testId, true);
|
|
487
|
+
|
|
488
|
+
// Then
|
|
489
|
+
assertThat(result).isNotNull();
|
|
490
|
+
assertThat(result).hasSize(1);
|
|
491
|
+
verify(treeRepository, times(1)).findItemWithAllDescendants(testId);
|
|
492
|
+
}
|
|
493
|
+
<% if (hasOrderField) {
|
|
494
|
+
const orderFieldName = orderField.name;
|
|
495
|
+
const upperOrderFieldName = orderFieldName.charAt(0).toUpperCase() + orderFieldName.slice(1);
|
|
496
|
+
const upperIdFieldName = ymlConfig.idField.charAt(0).toUpperCase() + ymlConfig.idField.slice(1);
|
|
497
|
+
%>
|
|
498
|
+
|
|
499
|
+
//----------------------------------
|
|
500
|
+
// Order Update Tests
|
|
501
|
+
//----------------------------------
|
|
502
|
+
|
|
503
|
+
@Test
|
|
504
|
+
@DisplayName("Should update sibling node orders successfully")
|
|
505
|
+
void testUpdateOrders() {
|
|
506
|
+
// Given
|
|
507
|
+
<%= classPrefix %>OrderUpdateDTO orderDto1 = new <%= classPrefix %>OrderUpdateDTO();
|
|
508
|
+
orderDto1.set<%= upperIdFieldName %>(testId);
|
|
509
|
+
orderDto1.set<%= upperOrderFieldName %>(2);
|
|
510
|
+
|
|
511
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
512
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
513
|
+
<%= classPrefix %>ListDTO listDto = new <%= classPrefix %>ListDTO();
|
|
514
|
+
<% if (!hasEmbeddedId) { %>listDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
515
|
+
listDto.set<%= upperOrderFieldName %>(2);
|
|
516
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>ListDTO.class)))
|
|
517
|
+
.thenReturn(listDto);
|
|
518
|
+
|
|
519
|
+
// When
|
|
520
|
+
List<<%= classPrefix %>ListDTO> result = treeService.updateOrders(null, Arrays.asList(orderDto1));
|
|
521
|
+
|
|
522
|
+
// Then
|
|
523
|
+
assertThat(result).isNotNull();
|
|
524
|
+
assertThat(result).hasSize(1);
|
|
525
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
@Test
|
|
529
|
+
@DisplayName("Should throw exception when updating order for node with wrong parent")
|
|
530
|
+
void testUpdateOrdersWithWrongParent() {
|
|
531
|
+
// Given
|
|
532
|
+
<%= classPrefix %>OrderUpdateDTO orderDto = new <%= classPrefix %>OrderUpdateDTO();
|
|
533
|
+
orderDto.set<%= upperIdFieldName %>(childId);
|
|
534
|
+
orderDto.set<%= upperOrderFieldName %>(1);
|
|
535
|
+
|
|
536
|
+
// childEntity has parentId = testId, but we're trying to update under null parent
|
|
537
|
+
when(treeRepository.findById(childId)).thenReturn(Optional.of(childEntity));
|
|
538
|
+
when(messageSource.getMessage(eq("error.tree.invalid.parent"), any(), anyString(), any()))
|
|
539
|
+
.thenReturn("Node does not belong to parent");
|
|
540
|
+
|
|
541
|
+
// When/Then
|
|
542
|
+
assertThatThrownBy(() -> treeService.updateOrders(null, Arrays.asList(orderDto)))
|
|
543
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
544
|
+
.hasMessageContaining("Node does not belong to parent");
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
@Test
|
|
548
|
+
@DisplayName("Should skip non-existent entities in order update")
|
|
549
|
+
void testUpdateOrdersSkipsNonExistent() {
|
|
550
|
+
// Given
|
|
551
|
+
<%= classPrefix %>OrderUpdateDTO orderDto = new <%= classPrefix %>OrderUpdateDTO();
|
|
552
|
+
orderDto.set<%= upperIdFieldName %>(<%= idType === 'String' ? '"non-existent-id"' : '999L' %>);
|
|
553
|
+
orderDto.set<%= upperOrderFieldName %>(1);
|
|
554
|
+
|
|
555
|
+
when(treeRepository.findById(any())).thenReturn(Optional.empty());
|
|
556
|
+
|
|
557
|
+
// When
|
|
558
|
+
List<<%= classPrefix %>ListDTO> result = treeService.updateOrders(null, Arrays.asList(orderDto));
|
|
559
|
+
|
|
560
|
+
// Then
|
|
561
|
+
assertThat(result).isEmpty();
|
|
562
|
+
verify(treeRepository, never()).save(any());
|
|
563
|
+
}
|
|
564
|
+
<% } %>
|
|
565
|
+
|
|
566
|
+
//----------------------------------
|
|
567
|
+
// Parent Change Validation Tests
|
|
568
|
+
//----------------------------------
|
|
569
|
+
|
|
570
|
+
@Test
|
|
571
|
+
@DisplayName("Should update node successfully when parentId changes to valid parent")
|
|
572
|
+
void testUpdateNodeWithValidParentChange() {
|
|
573
|
+
// Given
|
|
574
|
+
<%= idType %> newParentId = <%= idType === 'String' ? '"new-parent-id"' :
|
|
575
|
+
idType === 'Long' ? '100L' :
|
|
576
|
+
idType === 'Integer' ? '100' : '100' %>;
|
|
577
|
+
<%= entityName %> newParent = new <%= entityName %>();
|
|
578
|
+
<% if (!hasEmbeddedId) { %>newParent.setId(newParentId<% if (idType !== 'String') { %>.toString()<% } %>);<% } %>
|
|
579
|
+
newParent.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(newParentId);
|
|
580
|
+
|
|
581
|
+
entity.setParentId(null); // Currently a root node
|
|
582
|
+
updateDto.setParentId(newParentId); // Move to new parent
|
|
583
|
+
|
|
584
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
585
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
586
|
+
|
|
587
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
588
|
+
when(treeRepository.findById(newParentId)).thenReturn(Optional.of(newParent));
|
|
589
|
+
when(treeRepository.findItemWithAllDescendants(testId)).thenReturn(Collections.emptyList());
|
|
590
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
591
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
592
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
593
|
+
.thenReturn(detailDto);
|
|
594
|
+
|
|
595
|
+
// When
|
|
596
|
+
<%= classPrefix %>DetailDTO result = treeService.updateNode(entity, updateDto);
|
|
597
|
+
|
|
598
|
+
// Then
|
|
599
|
+
assertThat(result).isNotNull();
|
|
600
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
@Test
|
|
604
|
+
@DisplayName("Should throw exception when moving to non-existent parent")
|
|
605
|
+
void testUpdateNodeWithNonExistentParent() {
|
|
606
|
+
// Given
|
|
607
|
+
<%= idType %> nonExistentParentId = <%= idType === 'String' ? '"non-existent-parent"' :
|
|
608
|
+
idType === 'Long' ? '999L' :
|
|
609
|
+
idType === 'Integer' ? '999' : '999' %>;
|
|
610
|
+
entity.setParentId(null);
|
|
611
|
+
updateDto.setParentId(nonExistentParentId);
|
|
612
|
+
|
|
613
|
+
when(treeRepository.findById(nonExistentParentId)).thenReturn(Optional.empty());
|
|
614
|
+
when(messageSource.getMessage(eq("error.entity.not.found"), any(), anyString(), any()))
|
|
615
|
+
.thenReturn("Parent not found");
|
|
616
|
+
|
|
617
|
+
// When/Then
|
|
618
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
619
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
620
|
+
.hasMessageContaining("Parent not found");
|
|
621
|
+
|
|
622
|
+
verify(treeRepository, never()).saveAndFlush(any());
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
@Test
|
|
626
|
+
@DisplayName("Should throw exception when moving node to its own descendant (circular reference)")
|
|
627
|
+
void testUpdateNodeWithCircularReference() {
|
|
628
|
+
// Given
|
|
629
|
+
// entity (testId) is parent of childEntity (childId)
|
|
630
|
+
// Trying to move entity under childEntity would create circular reference
|
|
631
|
+
entity.setParentId(null);
|
|
632
|
+
updateDto.setParentId(childId); // Try to set child as parent
|
|
633
|
+
|
|
634
|
+
// Mock for new parent lookup
|
|
635
|
+
when(treeRepository.findById(childId)).thenReturn(Optional.of(childEntity));
|
|
636
|
+
// Mock for ancestor lookup: childEntity -> entity (testId) -> null
|
|
637
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
638
|
+
// When/Then
|
|
639
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
640
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
641
|
+
.hasMessageContaining("error.tree.circular.reference");
|
|
642
|
+
|
|
643
|
+
verify(treeRepository, never()).saveAndFlush(any());
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
@Test
|
|
647
|
+
@DisplayName("Should update node successfully when moving to root (parentId null)")
|
|
648
|
+
void testUpdateNodeMoveToRoot() {
|
|
649
|
+
// Given
|
|
650
|
+
childEntity.setParentId(testId); // Currently has parent
|
|
651
|
+
<%= classPrefix %>UpdateDTO childUpdateDto = new <%= classPrefix %>UpdateDTO();
|
|
652
|
+
childUpdateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);
|
|
653
|
+
childUpdateDto.setParentId(null); // Move to root
|
|
654
|
+
|
|
655
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
656
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
657
|
+
|
|
658
|
+
when(treeRepository.findById(childId)).thenReturn(Optional.of(childEntity));
|
|
659
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(childEntity);
|
|
660
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
661
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
662
|
+
.thenReturn(detailDto);
|
|
663
|
+
|
|
664
|
+
// When
|
|
665
|
+
<%= classPrefix %>DetailDTO result = treeService.updateNode(childEntity, childUpdateDto);
|
|
666
|
+
|
|
667
|
+
// Then
|
|
668
|
+
assertThat(result).isNotNull();
|
|
669
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
670
|
+
}
|
|
671
|
+
<% if (idType === 'String') { %>
|
|
672
|
+
|
|
673
|
+
@Test
|
|
674
|
+
@DisplayName("Should handle empty string parentId as null (move to root)")
|
|
675
|
+
void testUpdateNodeWithEmptyParentId() {
|
|
676
|
+
// Given
|
|
677
|
+
childEntity.setParentId(testId); // Currently has parent
|
|
678
|
+
<%= classPrefix %>UpdateDTO childUpdateDto = new <%= classPrefix %>UpdateDTO();
|
|
679
|
+
childUpdateDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);
|
|
680
|
+
childUpdateDto.setParentId(""); // Empty string should be treated as null
|
|
681
|
+
|
|
682
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
683
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(childId);<% } %>
|
|
684
|
+
|
|
685
|
+
when(treeRepository.findById(childId)).thenReturn(Optional.of(childEntity));
|
|
686
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenAnswer(invocation -> {
|
|
687
|
+
<%= entityName %> savedEntity = invocation.getArgument(0);
|
|
688
|
+
// Verify parentId is set to null (not empty string)
|
|
689
|
+
assertThat(savedEntity.getParentId()).isNull();
|
|
690
|
+
return savedEntity;
|
|
691
|
+
});
|
|
692
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
693
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
694
|
+
.thenReturn(detailDto);
|
|
695
|
+
|
|
696
|
+
// When
|
|
697
|
+
<%= classPrefix %>DetailDTO result = treeService.updateNode(childEntity, childUpdateDto);
|
|
698
|
+
|
|
699
|
+
// Then
|
|
700
|
+
assertThat(result).isNotNull();
|
|
701
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
702
|
+
}
|
|
703
|
+
<% } %>
|
|
704
|
+
<%
|
|
705
|
+
// Generate Immutable Reference Field Tests
|
|
706
|
+
const immutableFields = [];
|
|
707
|
+
if (ymlConfig && ymlConfig.fields) {
|
|
708
|
+
Object.entries(ymlConfig.fields).forEach(([fieldName, field]) => {
|
|
709
|
+
if (field.reference && !field.reference.multiple && field.reference.entity !== entityName && field.immutable) {
|
|
710
|
+
immutableFields.push({ fieldName, field });
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
if (immutableFields.length > 0) { %>
|
|
716
|
+
|
|
717
|
+
//----------------------------------
|
|
718
|
+
// Immutable Reference Field Tests
|
|
719
|
+
//----------------------------------
|
|
720
|
+
<%
|
|
721
|
+
immutableFields.forEach(({ fieldName, field }) => {
|
|
722
|
+
const upperFieldName = fieldName.charAt(0).toUpperCase() + fieldName.slice(1);
|
|
723
|
+
const idFieldNameRef = field.reference.idField;
|
|
724
|
+
const upperIdFieldNameRef = idFieldNameRef.charAt(0).toUpperCase() + idFieldNameRef.slice(1);
|
|
725
|
+
const refEntity = field.reference.entity;
|
|
726
|
+
const refIdType = field.reference.idType || 'String';
|
|
727
|
+
%>
|
|
728
|
+
|
|
729
|
+
@Test
|
|
730
|
+
@DisplayName("Should throw exception when <%= fieldName %>Id is changed during update")
|
|
731
|
+
void testUpdateNodeWith<%= upperFieldName %>Changed() {
|
|
732
|
+
// Given
|
|
733
|
+
<%= refIdType %> original<%= upperFieldName %>Id = <%= refIdType === 'String' ? '"' + fieldName + '-1"' : '1L' %>;
|
|
734
|
+
<%= refIdType %> new<%= upperFieldName %>Id = <%= refIdType === 'String' ? '"' + fieldName + '-2"' : '2L' %>;
|
|
735
|
+
|
|
736
|
+
<%= refEntity %> <%= _.camelCase(fieldName) %> = new <%= refEntity %>();
|
|
737
|
+
<%= _.camelCase(fieldName) %>.set<%= upperIdFieldNameRef %>(original<%= upperFieldName %>Id);
|
|
738
|
+
entity.set<%= upperFieldName %>(<%= _.camelCase(fieldName) %>);
|
|
739
|
+
updateDto.set<%= upperIdFieldNameRef %>(new<%= upperFieldName %>Id);
|
|
740
|
+
|
|
741
|
+
// When/Then
|
|
742
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
743
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
744
|
+
.hasMessageContaining("error.<%= _.snakeCase(fieldName) %>.cannot.change");
|
|
745
|
+
|
|
746
|
+
verify(treeRepository, never()).saveAndFlush(any());
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
@Test
|
|
750
|
+
@DisplayName("Should throw exception when <%= fieldName %>Id is changed from null to value")
|
|
751
|
+
void testUpdateNodeWith<%= upperFieldName %>FromNullToValue() {
|
|
752
|
+
// Given
|
|
753
|
+
entity.set<%= upperFieldName %>(null);
|
|
754
|
+
updateDto.set<%= upperIdFieldNameRef %>(<%= refIdType === 'String' ? '"' + fieldName + '-1"' : '1L' %>);
|
|
755
|
+
|
|
756
|
+
// When/Then
|
|
757
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
758
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
759
|
+
.hasMessageContaining("error.<%= _.snakeCase(fieldName) %>.cannot.change");
|
|
760
|
+
|
|
761
|
+
verify(treeRepository, never()).saveAndFlush(any());
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
@Test
|
|
765
|
+
@DisplayName("Should throw exception when <%= fieldName %>Id is changed from value to null")
|
|
766
|
+
void testUpdateNodeWith<%= upperFieldName %>FromValueToNull() {
|
|
767
|
+
// Given
|
|
768
|
+
<%= refEntity %> <%= _.camelCase(fieldName) %> = new <%= refEntity %>();
|
|
769
|
+
<%= _.camelCase(fieldName) %>.set<%= upperIdFieldNameRef %>(<%= refIdType === 'String' ? '"' + fieldName + '-1"' : '1L' %>);
|
|
770
|
+
entity.set<%= upperFieldName %>(<%= _.camelCase(fieldName) %>);
|
|
771
|
+
updateDto.set<%= upperIdFieldNameRef %>(null);
|
|
772
|
+
|
|
773
|
+
// When/Then
|
|
774
|
+
assertThatThrownBy(() -> treeService.updateNode(entity, updateDto))
|
|
775
|
+
.isInstanceOf(SimpliXGeneralException.class)
|
|
776
|
+
.hasMessageContaining("error.<%= _.snakeCase(fieldName) %>.cannot.change");
|
|
777
|
+
|
|
778
|
+
verify(treeRepository, never()).saveAndFlush(any());
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
@Test
|
|
782
|
+
@DisplayName("Should update node successfully when <%= fieldName %>Id is same")
|
|
783
|
+
void testUpdateNodeWith<%= upperFieldName %>Same() {
|
|
784
|
+
// Given
|
|
785
|
+
<%= refIdType %> <%= _.camelCase(fieldName) %>Id = <%= refIdType === 'String' ? '"' + fieldName + '-1"' : '1L' %>;
|
|
786
|
+
<%= refEntity %> <%= _.camelCase(fieldName) %> = new <%= refEntity %>();
|
|
787
|
+
<%= _.camelCase(fieldName) %>.set<%= upperIdFieldNameRef %>(<%= _.camelCase(fieldName) %>Id);
|
|
788
|
+
entity.set<%= upperFieldName %>(<%= _.camelCase(fieldName) %>);
|
|
789
|
+
updateDto.set<%= upperIdFieldNameRef %>(<%= _.camelCase(fieldName) %>Id);
|
|
790
|
+
|
|
791
|
+
<%= classPrefix %>DetailDTO detailDto = new <%= classPrefix %>DetailDTO();
|
|
792
|
+
<% if (!hasEmbeddedId) { %>detailDto.set<%= idFieldName.charAt(0).toUpperCase() + idFieldName.slice(1) %>(testId);<% } %>
|
|
793
|
+
|
|
794
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
795
|
+
when(treeRepository.saveAndFlush(any(<%= entityName %>.class))).thenReturn(entity);
|
|
796
|
+
doAnswer(invocation -> null).when(modelMapper).map(any(), any(<%= entityName %>.class));
|
|
797
|
+
when(modelMapper.map(any(<%= entityName %>.class), eq(<%= classPrefix %>DetailDTO.class)))
|
|
798
|
+
.thenReturn(detailDto);
|
|
799
|
+
|
|
800
|
+
// When
|
|
801
|
+
<%= classPrefix %>DetailDTO result = treeService.updateNode(entity, updateDto);
|
|
802
|
+
|
|
803
|
+
// Then
|
|
804
|
+
assertThat(result).isNotNull();
|
|
805
|
+
verify(treeRepository, times(1)).saveAndFlush(any(<%= entityName %>.class));
|
|
806
|
+
}
|
|
807
|
+
<%
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
%>
|
|
811
|
+
|
|
812
|
+
//----------------------------------
|
|
813
|
+
// Helper Method Tests
|
|
814
|
+
//----------------------------------
|
|
815
|
+
|
|
816
|
+
@Test
|
|
817
|
+
@DisplayName("Should find entity by ID")
|
|
818
|
+
void testFindById() {
|
|
819
|
+
// Given
|
|
820
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.of(entity));
|
|
821
|
+
|
|
822
|
+
// When
|
|
823
|
+
Optional<<%= entityName %>> result = treeService.findById(testId);
|
|
824
|
+
|
|
825
|
+
// Then
|
|
826
|
+
assertThat(result).isPresent();
|
|
827
|
+
assertThat(result.get()).isEqualTo(entity);
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
@Test
|
|
831
|
+
@DisplayName("Should return empty when entity not found")
|
|
832
|
+
void testFindByIdNotFound() {
|
|
833
|
+
// Given
|
|
834
|
+
when(treeRepository.findById(testId)).thenReturn(Optional.empty());
|
|
835
|
+
|
|
836
|
+
// When
|
|
837
|
+
Optional<<%= entityName %>> result = treeService.findById(testId);
|
|
838
|
+
|
|
839
|
+
// Then
|
|
840
|
+
assertThat(result).isEmpty();
|
|
841
|
+
}
|
|
842
|
+
}
|