1
1
package org .folio .service .orders ;
2
2
3
3
import static io .vertx .core .Future .succeededFuture ;
4
- import static org .folio .TestUtils .getLocationsForTenants ;
5
4
import static org .folio .rest .core .exceptions .ErrorCodes .*;
6
5
import static org .folio .rest .jaxrs .model .CompositePoLine .OrderFormat .OTHER ;
7
6
import static org .folio .rest .jaxrs .model .CompositePoLine .OrderFormat .P_E_MIX ;
12
11
import static org .junit .jupiter .api .Assertions .assertInstanceOf ;
13
12
import static org .junit .jupiter .api .Assertions .assertTrue ;
14
13
import static org .junit .jupiter .api .Assertions .fail ;
15
- import static org .mockito .ArgumentMatchers .any ;
16
14
import static org .mockito .ArgumentMatchers .anyString ;
17
15
import static org .mockito .ArgumentMatchers .eq ;
18
16
import static org .mockito .Mockito .doReturn ;
17
+ import static org .mockito .Mockito .when ;
19
18
20
- import java .lang .reflect .InvocationTargetException ;
21
19
import java .util .List ;
22
20
import java .util .Optional ;
23
21
import java .util .Set ;
24
22
import java .util .UUID ;
25
23
import java .util .stream .Collectors ;
26
24
27
25
import io .vertx .core .Future ;
26
+ import io .vertx .junit5 .VertxExtension ;
27
+ import io .vertx .junit5 .VertxTestContext ;
28
28
import org .folio .models .consortium .ConsortiumConfiguration ;
29
29
import org .folio .rest .core .exceptions .ErrorCodes ;
30
30
import org .folio .rest .core .exceptions .HttpException ;
35
35
import org .folio .rest .jaxrs .model .Error ;
36
36
import org .folio .rest .jaxrs .model .Location ;
37
37
import org .folio .rest .jaxrs .model .Physical ;
38
- import org .folio .rest .jaxrs .model .PoLine ;
39
38
import org .folio .service .consortium .ConsortiumConfigurationService ;
40
39
import org .folio .service .consortium .ConsortiumUserTenantsRetriever ;
41
40
import org .folio .service .finance .expenceclass .ExpenseClassValidationService ;
42
41
import org .junit .jupiter .api .AfterEach ;
43
42
import org .junit .jupiter .api .BeforeEach ;
44
43
import org .junit .jupiter .api .DisplayName ;
45
44
import org .junit .jupiter .api .Test ;
45
+ import org .junit .jupiter .api .extension .ExtendWith ;
46
46
import org .mockito .InjectMocks ;
47
47
import org .mockito .Mock ;
48
48
import org .mockito .MockitoAnnotations ;
49
49
50
+ @ ExtendWith (VertxExtension .class )
50
51
public class CompositePoLineValidationServiceTest {
52
+
53
+ @ Mock private ExpenseClassValidationService expenseClassValidationService ;
54
+ @ Mock private ConsortiumConfigurationService consortiumConfigurationService ;
55
+ @ Mock private ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever ;
56
+ @ Mock private RequestContext requestContext ;
57
+ @ InjectMocks private CompositePoLineValidationService compositePoLineValidationService ;
58
+
51
59
private AutoCloseable mockitoMocks ;
52
- @ Mock
53
- private ExpenseClassValidationService expenseClassValidationService ;
54
- @ Mock
55
- private ConsortiumConfigurationService consortiumConfigurationService ;
56
- @ Mock
57
- private ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever ;
58
- @ InjectMocks
59
- private CompositePoLineValidationService compositePoLineValidationService ;
60
- @ Mock
61
- private RequestContext requestContext ;
62
60
63
61
@ BeforeEach
64
- public void initMocks (){
62
+ public void initMocks () {
65
63
mockitoMocks = MockitoAnnotations .openMocks (this );
66
64
}
67
65
@@ -169,7 +167,6 @@ void shouldReturnErrorIfIncorrectOrderFormatWhenBindaryActive() {
169
167
assertEquals (ORDER_FORMAT_INCORRECT_FOR_BINDARY_ACTIVE .getCode (), errors .get (0 ).getCode ());
170
168
}
171
169
172
-
173
170
@ Test
174
171
void shouldReturnErrorIfIncorrectCreateInventoryWhenBindaryActive () {
175
172
var compositePoLine = new CompositePoLine ()
@@ -382,7 +379,6 @@ void testValidatePoLineWithZeroQuantitiesWithoutLocations() {
382
379
assertThat (errors , hasSize (0 ));
383
380
}
384
381
385
-
386
382
private Set <String > errorsToCodes (List <Error > errors ) {
387
383
return errors
388
384
.stream ()
@@ -391,38 +387,91 @@ private Set<String> errorsToCodes(List<Error> errors) {
391
387
}
392
388
393
389
@ Test
394
- void testValidateUserUnaffiliatedLocationUpdates () throws NoSuchMethodException , InvocationTargetException , IllegalAccessException {
395
- var locationsStored = getLocationsForTenants (List .of ("tenant1" , "tenant2" , "tenant3" ));
396
- var locationsUpdated = getLocationsForTenants (List .of ("tenant1" , "tenant3" ));
397
- var storagePoLine = new PoLine ().withLocations (locationsStored );
398
- var updatedPoLine = new CompositePoLine ().withLocations (locationsUpdated );
390
+ void testValidateUserUnaffiliatedLocationUpdates (VertxTestContext testContext ) {
391
+ var locationsUpdated = List .of (
392
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant1" ),
393
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ));
394
+ var updatedPoLineId = UUID .randomUUID ().toString ();
395
+
396
+ when (consortiumConfigurationService .getConsortiumConfiguration (eq (requestContext )))
397
+ .thenReturn (Future .succeededFuture (Optional .empty ()));
398
+ when (consortiumUserTenantsRetriever .getUserTenants (eq ("consortiumId" ), anyString (), eq (requestContext )))
399
+ .thenReturn (Future .succeededFuture (List .of ("tenant1" )));
400
+
401
+ compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLineId , locationsUpdated , requestContext )
402
+ .onComplete (testContext .succeeding (result -> testContext .verify (testContext ::completeNow )));
403
+ }
399
404
400
- doReturn (succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))))
401
- .when (consortiumConfigurationService ).getConsortiumConfiguration (any (RequestContext .class ));
402
- doReturn (succeededFuture (List .of ("tenant1" , "tenant2" )))
403
- .when (consortiumUserTenantsRetriever ).getUserTenants (eq ("consortiumId" ), eq ("centralTenantId" ), any (RequestContext .class ));
405
+ @ Test
406
+ void testValidateUserUnaffiliatedLocationUpdatesAllValidLocations (VertxTestContext testContext ) {
407
+ var locationsUpdated = List .of (
408
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant1" ),
409
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ));
410
+ var updatedPoLineId = UUID .randomUUID ().toString ();
411
+
412
+ when (consortiumConfigurationService .getConsortiumConfiguration (eq (requestContext )))
413
+ .thenReturn (Future .succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))));
414
+ when (consortiumUserTenantsRetriever .getUserTenants (eq ("consortiumId" ), anyString (), eq (requestContext )))
415
+ .thenReturn (Future .succeededFuture (List .of ("tenant1" , "tenant2" )));
416
+
417
+ compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLineId , locationsUpdated , requestContext )
418
+ .onComplete (testContext .succeeding (result -> testContext .verify (testContext ::completeNow )));
419
+ }
404
420
405
- var future = compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLine .getId (), updatedPoLine .getLocations (), storagePoLine .getLocations (), requestContext );
421
+ @ Test
422
+ void testValidateUserUnaffiliatedLocationUpdatesAllValidDuplicateTenantLocations (VertxTestContext testContext ) {
423
+ var locationsUpdated = List .of (
424
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant1" ),
425
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ),
426
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ));
427
+ var updatedPoLineId = UUID .randomUUID ().toString ();
428
+
429
+ when (consortiumConfigurationService .getConsortiumConfiguration (eq (requestContext )))
430
+ .thenReturn (Future .succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))));
431
+ when (consortiumUserTenantsRetriever .getUserTenants (eq ("consortiumId" ), anyString (), eq (requestContext )))
432
+ .thenReturn (Future .succeededFuture (List .of ("tenant1" , "tenant2" )));
433
+
434
+ compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLineId , locationsUpdated , requestContext )
435
+ .onComplete (testContext .succeeding (result -> testContext .verify (testContext ::completeNow )));
436
+ }
406
437
407
- assertTrue (future .succeeded ());
438
+ @ Test
439
+ void testValidateUserUnaffiliatedLocationUpdatesOneValidAndOneInvalidLocations (VertxTestContext testContext ) {
440
+ var locationsUpdated = List .of (
441
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant1" ),
442
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ));
443
+ var updatedPoLineId = UUID .randomUUID ().toString ();
444
+
445
+ when (consortiumConfigurationService .getConsortiumConfiguration (eq (requestContext )))
446
+ .thenReturn (Future .succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))));
447
+ when (consortiumUserTenantsRetriever .getUserTenants (eq ("consortiumId" ), anyString (), eq (requestContext )))
448
+ .thenReturn (Future .succeededFuture (List .of ("tenant1" )));
449
+
450
+ compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLineId , locationsUpdated , requestContext )
451
+ .onComplete (testContext .failing (cause -> testContext .verify (() -> {
452
+ assertInstanceOf (HttpException .class , cause );
453
+ assertTrue (cause .getMessage ().contains (ErrorCodes .LOCATION_UPDATE_WITHOUT_AFFILIATION .getDescription ()));
454
+ testContext .completeNow ();
455
+ })));
408
456
}
409
457
410
458
@ Test
411
- void testValidateUserUnaffiliatedLocationUpdatesInvalid () {
412
- var locationsStored = getLocationsForTenants (List .of ("tenant1" , "tenant2" , "tenant3" ));
413
- var locationsUpdated = getLocationsForTenants (List .of ("tenant1" , "tenant3" ));
414
- locationsUpdated .get (1 ).withQuantityPhysical (10 );
415
- var storagePoLine = new PoLine ().withLocations (locationsStored );
416
- var updatedPoLine = new CompositePoLine ().withLocations (locationsUpdated );
417
-
418
- doReturn (succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))))
419
- .when (consortiumConfigurationService ).getConsortiumConfiguration (any (RequestContext .class ));
420
- doReturn (succeededFuture (List .of ("tenant1" )))
421
- .when (consortiumUserTenantsRetriever ).getUserTenants (eq ("consortiumId" ), anyString (), any (RequestContext .class ));
422
-
423
- var future = compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLine .getId (), updatedPoLine .getLocations (), storagePoLine .getLocations (), requestContext );
424
-
425
- assertTrue (future .failed ());
426
- assertInstanceOf (HttpException .class , future .cause ());
459
+ void testValidateUserUnaffiliatedLocationUpdatesTwoInvalidLocations (VertxTestContext testContext ) {
460
+ var locationsUpdated = List .of (
461
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant1" ),
462
+ new Location ().withLocationId (UUID .randomUUID ().toString ()).withTenantId ("tenant2" ));
463
+ var updatedPoLineId = UUID .randomUUID ().toString ();
464
+
465
+ when (consortiumConfigurationService .getConsortiumConfiguration (eq (requestContext )))
466
+ .thenReturn (Future .succeededFuture (Optional .of (new ConsortiumConfiguration ("tenant1" , "consortiumId" ))));
467
+ when (consortiumUserTenantsRetriever .getUserTenants (eq ("consortiumId" ), anyString (), eq (requestContext )))
468
+ .thenReturn (Future .succeededFuture (List .of ("tenant3" )));
469
+
470
+ compositePoLineValidationService .validateUserUnaffiliatedLocationUpdates (updatedPoLineId , locationsUpdated , requestContext )
471
+ .onComplete (testContext .failing (cause -> testContext .verify (() -> {
472
+ assertInstanceOf (HttpException .class , cause );
473
+ assertTrue (cause .getMessage ().contains (ErrorCodes .LOCATION_UPDATE_WITHOUT_AFFILIATION .getDescription ()));
474
+ testContext .completeNow ();
475
+ })));
427
476
}
428
477
}
0 commit comments