### Question:
SelectiveIdentificationAuthorizationService implements IdentificationAuthorizationService { @Override public IdentifyingToken validate(IdentifyingToken token, @Nullable ChargingStationId chargingStationId) { if (chargingStationId == null) { LOG.warn("No charging station id passed to validation request for token {}", token.getToken()); return token; } ChargingStation chargingStation = repository.findOne(chargingStationId.getId()); if (chargingStation == null) { LOG.warn("Charging station not found in repository: {}", chargingStationId); return token; } List<String> chargingStationAuthorizationProviders = chargingStation.getAuthorizationProvidersAsList(); if (chargingStationAuthorizationProviders.isEmpty()) { LOG.warn("No authorization providers configured for charging station: {}", chargingStationId); return token; } for (String authorizationProvider:chargingStationAuthorizationProviders) { AuthorizationProvider provider = providers.get(authorizationProvider); if (provider != null) { IdentifyingToken validatedToken = provider.validate(token, chargingStationId); if (validatedToken.isValid()) { return validatedToken; } } } return token; }  void setProviders(Map<String, AuthorizationProvider> providers); void setRepository(ChargingStationRepository repository); @Override IdentifyingToken validate(IdentifyingToken token, @Nullable ChargingStationId chargingStationId); void setDefaultAuthorizationProviders(String defaultAuthorizationProviders);  }### Answer:
@Test public void oneAuthProviderIsChecked() { when(localAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(chargingStation.getAuthorizationProvidersAsList()).thenReturn( Arrays.asList("local") ); assertTrue(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); when(localAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_INVALID); assertFalse(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); }
@Test public void secondAuthProviderIsCheckedIfFirstDoesNotValidate() { when(localAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_INVALID); when(ocpiAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(chargingStation.getAuthorizationProvidersAsList()).thenReturn( Arrays.asList("local", "ocpi") ); assertTrue(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); }
@Test public void chargingStationHasNoAuthProvidersThenAuthShouldNotBeValid() { when(localAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(ocpiAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(chargingStation.getAuthorizationProvidersAsList()).thenReturn( new ArrayList<String>() ); assertFalse(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); }
@Test public void unknownAuthProviderShouldNotThrowException() { when(localAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(ocpiAuthProvider.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID)).thenReturn(IDENTIFYING_TOKEN_ACCEPTED); when(chargingStation.getAuthorizationProvidersAsList()).thenReturn( Arrays.asList("unknown", "auth", "providers") ); assertFalse(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); }
@Test public void unknownChargingStationShouldNotThrowException() { when(chargingStationRepository.findOne(CHARGING_STATION_ID.getId())).thenReturn(null); assertFalse(service.validate(IDENTIFYING_TOKEN, CHARGING_STATION_ID).isValid()); }
@Test public void nullChargingStationIdShouldNotThrowException() { assertFalse(service.validate(IDENTIFYING_TOKEN, null).isValid()); }