-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionManagerServiceImplTest-1.java
More file actions
325 lines (278 loc) · 14.7 KB
/
TransactionManagerServiceImplTest-1.java
File metadata and controls
325 lines (278 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package com.cl.apps.mssepamd.services.impl;
import com.cl.apps.mssepamd.exceptions.TransactionException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.assertThatNoException;
import static org.mockito.Mockito.*;
/**
* Tests unitaires pour {@link TransactionManagerServiceImpl}.
*
* <p>Stratégie de test :
* <ul>
* <li>verifierErreur : couverture exhaustive des 4 branches (codeSysteme, codeApplicatif,
* codeSortieAppli, aucune erreur)</li>
* <li>callTransaction : vérification de l'orchestration des appels CICS</li>
* </ul>
*/
@ExtendWith(MockitoExtension.class)
@DisplayName("TransactionManagerServiceImpl")
class TransactionManagerServiceImplTest {
@Mock
private CICSService cics;
@Mock
private CICSProperties cicsProperties;
@InjectMocks
private TransactionManagerServiceImpl service;
// -------------------------------------------------------------------------
// Helpers – construction d'un RetourCICS stub
// -------------------------------------------------------------------------
/**
* Crée un {@link RetourCICS} totalement vide (aucune erreur).
*/
private RetourCICS retourSansErreur() {
RetourCICS retour = mock(RetourCICS.class);
lenient().when(retour.getCodeSysteme()).thenReturn(null);
lenient().when(retour.getCodeApplicatif()).thenReturn(null);
lenient().when(retour.getCodeSortieAppli()).thenReturn(null);
lenient().when(retour.getLibelleSortieAppli()).thenReturn(null);
return retour;
}
// =========================================================================
// verifierErreur
// =========================================================================
@Nested
@DisplayName("verifierErreur()")
class VerifierErreurTests {
// -----------------------------------------------------------------
// Branche 1 : codeSysteme présent et != "0000"
// -----------------------------------------------------------------
@Test
@DisplayName("doit lever TransactionException quand codeSysteme est non-blank et != '0000'")
void verifierErreur_codeSystemeNonBlankEtDifferentDe0000_leveException() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn("SYS1");
when(erreur.getLibelleSysteme()).thenReturn("Erreur système grave");
when(erreur.getMessageDetaille()).thenReturn("Détail erreur système");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex -> {
TransactionException txEx = (TransactionException) ex;
assertThat(txEx.getCode()).isEqualTo("Erreur système grave");
assertThat(txEx.getMessage()).isEqualTo("Détail erreur système");
assertThat(txEx.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
});
}
@Test
@DisplayName("ne doit PAS lever d'exception quand codeSysteme vaut '0000' (valeur nominale)")
void verifierErreur_codeSystemeEgalA0000_pasException() {
// GIVEN – "0000" == ERROR_CODE_SYSTEM_OR_APPLICATION → branche ignorée
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn("0000");
when(erreur.getCodeApplicatif()).thenReturn(null);
when(erreur.getCodeSortieAppli()).thenReturn(null);
when(erreur.getLibelleSortieAppli()).thenReturn(null);
// WHEN / THEN
assertThatNoException().isThrownBy(() -> service.verifierErreur(erreur));
}
@Test
@DisplayName("ne doit PAS lever d'exception quand codeSysteme est blank")
void verifierErreur_codeSystemeBlank_pasException() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn(" ");
when(erreur.getCodeApplicatif()).thenReturn(null);
when(erreur.getCodeSortieAppli()).thenReturn(null);
when(erreur.getLibelleSortieAppli()).thenReturn(null);
// WHEN / THEN
assertThatNoException().isThrownBy(() -> service.verifierErreur(erreur));
}
// -----------------------------------------------------------------
// Branche 2 : codeApplicatif présent et != "0000"
// -----------------------------------------------------------------
@Test
@DisplayName("doit lever TransactionException quand codeApplicatif est non-blank et != '0000'")
void verifierErreur_codeApplicatifNonBlankEtDifferentDe0000_leveException() {
// GIVEN – codeSysteme ignoré (null), codeApplicatif déclenche l'erreur
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn(null);
when(erreur.getCodeApplicatif()).thenReturn("APP9");
when(erreur.getLibelleApplicatif()).thenReturn("Erreur applicative");
when(erreur.getMessageDetaille()).thenReturn("Détail erreur applicative");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex -> {
TransactionException txEx = (TransactionException) ex;
assertThat(txEx.getCode()).isEqualTo("Erreur applicative");
assertThat(txEx.getMessage()).isEqualTo("Détail erreur applicative");
assertThat(txEx.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
});
}
@Test
@DisplayName("ne doit PAS lever d'exception quand codeApplicatif vaut '0000'")
void verifierErreur_codeApplicatifEgalA0000_pasException() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn(null);
when(erreur.getCodeApplicatif()).thenReturn("0000");
when(erreur.getCodeSortieAppli()).thenReturn(null);
when(erreur.getLibelleSortieAppli()).thenReturn(null);
// WHEN / THEN
assertThatNoException().isThrownBy(() -> service.verifierErreur(erreur));
}
// -----------------------------------------------------------------
// Branche 3 : codeSortieAppli OU libelleSortieAppli présent
// -----------------------------------------------------------------
@Test
@DisplayName("doit lever TransactionException quand codeSortieAppli est non-blank")
void verifierErreur_codeSortieAppliNonBlank_leveException() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn(null);
when(erreur.getCodeApplicatif()).thenReturn(null);
when(erreur.getCodeSortieAppli()).thenReturn("SORT1");
when(erreur.getLibelleSortieAppli()).thenReturn("Libellé sortie appli");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex -> {
TransactionException txEx = (TransactionException) ex;
assertThat(txEx.getCode()).isEqualTo("SORT1");
assertThat(txEx.getMessage()).isEqualTo("Libellé sortie appli");
assertThat(txEx.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
});
}
@Test
@DisplayName("doit lever TransactionException quand libelleSortieAppli est non-blank (codeSortieAppli blank)")
void verifierErreur_libelleSortieAppliNonBlankSansCode_leveException() {
// GIVEN – condition || : codeSortie blank mais libelle présent suffit
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn(null);
when(erreur.getCodeApplicatif()).thenReturn(null);
when(erreur.getCodeSortieAppli()).thenReturn("");
when(erreur.getLibelleSortieAppli()).thenReturn("Libellé sans code");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex -> {
TransactionException txEx = (TransactionException) ex;
assertThat(txEx.getCode()).isEqualTo(""); // codeSortieAppli vide
assertThat(txEx.getMessage()).isEqualTo("Libellé sans code");
assertThat(txEx.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
});
}
// -----------------------------------------------------------------
// Branche 4 : aucune erreur → return silencieux
// -----------------------------------------------------------------
@Test
@DisplayName("ne doit PAS lever d'exception quand aucun champ d'erreur n'est renseigné")
void verifierErreur_aucuneErreur_pasException() {
// GIVEN
RetourCICS erreur = retourSansErreur();
// WHEN / THEN
assertThatNoException().isThrownBy(() -> service.verifierErreur(erreur));
}
@Test
@DisplayName("ne doit PAS lever d'exception quand tous les champs d'erreur sont blank")
void verifierErreur_tousChampsBlanks_pasException() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn("");
when(erreur.getCodeApplicatif()).thenReturn("");
when(erreur.getCodeSortieAppli()).thenReturn("");
when(erreur.getLibelleSortieAppli()).thenReturn("");
// WHEN / THEN
assertThatNoException().isThrownBy(() -> service.verifierErreur(erreur));
}
// -----------------------------------------------------------------
// Cas limites / priorité des branches
// -----------------------------------------------------------------
@Test
@DisplayName("la branche codeSysteme doit être prioritaire sur codeApplicatif")
void verifierErreur_codeSystemeEtApplicatifPresents_prioriteAuCodeSysteme() {
// GIVEN – les deux codes sont en erreur : c'est codeSysteme qui gagne
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn("SYS_PRIO");
when(erreur.getLibelleSysteme()).thenReturn("Libellé système prioritaire");
when(erreur.getMessageDetaille()).thenReturn("Détail système");
// codeApplicatif ne doit pas être atteint
lenient().when(erreur.getCodeApplicatif()).thenReturn("APP_IGNORE");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex -> {
TransactionException txEx = (TransactionException) ex;
assertThat(txEx.getCode()).isEqualTo("Libellé système prioritaire");
});
// Vérification que getCodeApplicatif n'a jamais été appelé
verify(erreur, never()).getCodeApplicatif();
}
@Test
@DisplayName("le statut HTTP de l'exception doit toujours être INTERNAL_SERVER_ERROR")
void verifierErreur_statusHttpEstToujoursInternalServerError() {
// GIVEN
RetourCICS erreur = mock(RetourCICS.class);
when(erreur.getCodeSysteme()).thenReturn("ERR");
when(erreur.getLibelleSysteme()).thenReturn("code");
when(erreur.getMessageDetaille()).thenReturn("msg");
// WHEN / THEN
assertThatThrownBy(() -> service.verifierErreur(erreur))
.isInstanceOf(TransactionException.class)
.satisfies(ex ->
assertThat(((TransactionException) ex).getStatus())
.isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR)
);
}
}
// =========================================================================
// callTransaction
// =========================================================================
@Nested
@DisplayName("callTransaction()")
class CallTransactionTests {
@Test
@DisplayName("doit déléguer à CICS et retourner le CommareaOutput")
void callTransaction_nominal_delegueACicsEtRetourneOutput() throws Exception {
// GIVEN
String transactionName = "KX01";
Object inputObj = new Object();
CICSTransaction transaction = mock(CICSTransaction.class);
CICSInput input = mock(CICSInput.class);
CommareaOutput expectedOutput = mock(CommareaOutput.class);
when(cics.getTransaction(transactionName)).thenReturn(transaction);
when(transaction.createInput()).thenReturn(input);
when(transaction.execute(any())).thenReturn(expectedOutput);
// WHEN
CommareaOutput result = service.callTransaction(inputObj, transactionName);
// THEN
assertThat(result).isEqualTo(expectedOutput);
verify(cics).getTransaction(transactionName);
verify(transaction).createInput();
verify(input).set(inputObj);
verify(transaction).execute(input);
}
@Test
@DisplayName("doit propager l'exception CICS telle quelle")
void callTransaction_cicsLeveException_propageLException() {
// GIVEN
String transactionName = "KX01";
when(cics.getTransaction(transactionName))
.thenThrow(new RuntimeException("Connexion CICS indisponible"));
// WHEN / THEN
assertThatThrownBy(() -> service.callTransaction(new Object(), transactionName))
.isInstanceOf(RuntimeException.class)
.hasMessageContaining("Connexion CICS indisponible");
}
}
}