FreeResourcesLimitValidator { public void check(FreeResourcesLimitDto freeResourcesLimit) throws BadRequestException { if (freeResourcesLimit == null) { throw new BadRequestException("Missed free resources limit description."); } if (freeResourcesLimit.getAccountId() == null) { throw new BadRequestException("Missed account id."); } Set<String> resourcesToSet = new HashSet<>(); for (ResourceDto resource : freeResourcesLimit.getResources()) { if (!resourcesToSet.add(resource.getType())) { throw new BadRequestException( format( "Free resources limit should contain only one resources with type '%s'.", resource.getType())); } resourceValidator.validate(resource); } } @Inject  FreeResourcesLimitValidator(ResourceValidator resourceValidator); void check(FreeResourcesLimitDto freeResourcesLimit);  }
@Test( expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "Missed free resources limit description.") public void shouldThrowBadRequestExceptionWhenFreeResourcesIsNull() throws Exception { validator.check(null); }
@Test( expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "Missed account id.") public void shouldThrowBadRequestExceptionWhenAccountIdIsMissed() throws Exception { validator.check( DtoFactory.newDto(FreeResourcesLimitDto.class) .withResources( singletonList( DtoFactory.newDto(ResourceDto.class) .withType("test") .withUnit("mb") .withAmount(1230)))); }
@Test( expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "invalid resource") public void shouldRethrowBadRequestExceptionWhenThereIsAnyInvalidResource() throws Exception { Mockito.doNothing() .doThrow(new BadRequestException("invalid resource")) .when(resourceValidator) .validate(any()); validator.check( DtoFactory.newDto(FreeResourcesLimitDto.class) .withAccountId("account123") .withResources( Arrays.asList( DtoFactory.newDto(ResourceDto.class) .withType("test") .withUnit("mb") .withAmount(1230), DtoFactory.newDto(ResourceDto.class) .withType("test2") .withUnit("mb") .withAmount(3214)))); }
@Test( expectedExceptions = BadRequestException.class, expectedExceptionsMessageRegExp = "Free resources limit should contain only one resources with type 'test'.") public void shouldThrowBadRequestExceptionWhenAccountResourcesLimitContainTwoResourcesWithTheSameType() throws Exception { validator.check( DtoFactory.newDto(FreeResourcesLimitDto.class) .withAccountId("account123") .withResources( Arrays.asList( DtoFactory.newDto(ResourceDto.class) .withType("test") .withUnit("mb") .withAmount(1230), DtoFactory.newDto(ResourceDto.class) .withType("test") .withUnit("mb") .withAmount(3)))); }
@Test public void shouldNotThrowAnyExceptionWhenAccountResourcesLimitIsValid() throws Exception { validator.check( DtoFactory.newDto(FreeResourcesLimitDto.class) .withAccountId("account123") .withResources( singletonList( DtoFactory.newDto(ResourceDto.class) .withType("test") .withUnit("mb") .withAmount(1230)))); }