ControlPointConfigValidator implements Validator<ControlPointConfig> { @Override public ValidationResult<String> validate(final ControlPointConfig newConfig) { final ControlPointConfig config = newConfig.merge(configService.getConfig().getControlPoint()); final ControlPointService controlPointService = new ControlPointService(httpClient, config); try { controlPointService.getPolicies(); } catch (final ControlPointApiException e) { if (e.getStatusCode() == HttpStatus.SC_UNAUTHORIZED || e.getStatusCode() == HttpStatus.SC_FORBIDDEN || e.getErrorId() == ControlPointApiException.ErrorId.INVALID_GRANT ) { return new ValidationResult<>(false, "INVALID_CREDENTIALS"); } else { return new ValidationResult<>(false, "UNKNOWN_ERROR"); } } catch (final ControlPointServiceException e) { return new ValidationResult<>(false, "CONNECTION_ERROR"); } return new ValidationResult<>(true); } @Autowired  ControlPointConfigValidator(
        final ConfigService<IdolFindConfig> configService,
        final HttpClient httpClient
    ); @Override ValidationResult<String> validate(final ControlPointConfig newConfig); @Override Class<ControlPointConfig> getSupportedClass();  }
@Test public void testValidate_valid() { final ValidationResult<String> res = validator.validate( new ControlPointConfig(true, ControlPointServerConfigTest.validConfig)); Assert.assertTrue("should report valid", res.isValid()); }
@Test public void testValidate_invalidCredentials() throws IOException { final String resJson = "{" + "\"error\": \"Invalid Grant\"," + "\"error_description\":\"Wrong credentials\"}"; Mockito.when(httpClient.execute(Mockito.any())) .thenReturn(ControlPointApiClientTest.buildResponse(401, "Unauthorized", resJson)); final ValidationResult<String> res = validator.validate( new ControlPointConfig(true, ControlPointServerConfigTest.validConfig)); Assert.assertFalse("should report invalid", res.isValid()); Assert.assertEquals("should have correct error ID", "INVALID_CREDENTIALS", res.getData()); }
@Test public void testValidate_apiError() throws IOException { final String resJson = "{" + "\"error\": \"Unknown\"," + "\"error_description\":\"Bad things\"}"; Mockito.when(httpClient.execute(Mockito.any())) .thenReturn(ControlPointApiClientTest.buildResponse(400, "Bad Request", resJson)); final ValidationResult<String> res = validator.validate( new ControlPointConfig(true, ControlPointServerConfigTest.validConfig)); Assert.assertFalse("should report invalid", res.isValid()); Assert.assertEquals("should have correct error ID", "UNKNOWN_ERROR", res.getData()); }
@Test public void testValidate_connectionError() throws IOException { Mockito.when(httpClient.execute(Mockito.any())).thenThrow(new IOException()); final ValidationResult<String> res = validator.validate( new ControlPointConfig(true, ControlPointServerConfigTest.validConfig)); Assert.assertFalse("should report invalid", res.isValid()); Assert.assertEquals("should have correct error ID", "CONNECTION_ERROR", res.getData()); }