Search in sources :

Example 6 with MockValidationContext

use of org.apache.nifi.util.MockValidationContext in project nifi by apache.

the class SSLContextServiceTest method testValidationResultsCacheShouldExpire.

@Test
public void testValidationResultsCacheShouldExpire() throws InitializationException, IOException {
    // Arrange
    // Copy the keystore and truststore to a tmp directory so the originals are not modified
    File originalKeystore = new File("src/test/resources/localhost-ks.jks");
    File originalTruststore = new File("src/test/resources/localhost-ts.jks");
    File tmpKeystore = tmp.newFile("keystore-tmp.jks");
    File tmpTruststore = tmp.newFile("truststore-tmp.jks");
    Files.copy(originalKeystore.toPath(), tmpKeystore.toPath(), StandardCopyOption.REPLACE_EXISTING);
    Files.copy(originalTruststore.toPath(), tmpTruststore.toPath(), StandardCopyOption.REPLACE_EXISTING);
    final TestRunner runner = TestRunners.newTestRunner(TestProcessor.class);
    SSLContextService service = new StandardSSLContextService();
    final String serviceIdentifier = "test-should-expire";
    runner.addControllerService(serviceIdentifier, service);
    runner.setProperty(service, StandardSSLContextService.KEYSTORE.getName(), tmpKeystore.getAbsolutePath());
    runner.setProperty(service, StandardSSLContextService.KEYSTORE_PASSWORD.getName(), "localtest");
    runner.setProperty(service, StandardSSLContextService.KEYSTORE_TYPE.getName(), "JKS");
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE.getName(), tmpTruststore.getAbsolutePath());
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_PASSWORD.getName(), "localtest");
    runner.setProperty(service, StandardSSLContextService.TRUSTSTORE_TYPE.getName(), "JKS");
    runner.enableControllerService(service);
    runner.setProperty("SSL Context Svc ID", serviceIdentifier);
    runner.assertValid(service);
    final StandardSSLContextService sslContextService = (StandardSSLContextService) service;
    // Act
    boolean isDeleted = tmpKeystore.delete();
    assert isDeleted;
    assert !tmpKeystore.exists();
    logger.info("Deleted keystore file");
    // Manually validate the service (expecting cached result to be returned)
    final MockProcessContext processContext = (MockProcessContext) runner.getProcessContext();
    // This service does not use the state manager or variable registry
    final ValidationContext validationContext = new MockValidationContext(processContext, null, null);
    // Even though the keystore file is no longer present, because no property changed, the cached result is still valid
    Collection<ValidationResult> validationResults = sslContextService.customValidate(validationContext);
    assertTrue("validation results is not empty", validationResults.isEmpty());
    logger.info("(1) StandardSSLContextService#customValidate() returned true even though the keystore file is no longer available");
    // Have to exhaust the cached result by checking n-1 more times
    for (int i = 2; i < sslContextService.getValidationCacheExpiration(); i++) {
        validationResults = sslContextService.customValidate(validationContext);
        assertTrue("validation results is not empty", validationResults.isEmpty());
        logger.info("(" + i + ") StandardSSLContextService#customValidate() returned true even though the keystore file is no longer available");
    }
    validationResults = sslContextService.customValidate(validationContext);
    assertFalse("validation results is empty", validationResults.isEmpty());
    logger.info("(" + sslContextService.getValidationCacheExpiration() + ") StandardSSLContextService#customValidate() returned false because the cache expired");
}
Also used : TestRunner(org.apache.nifi.util.TestRunner) MockValidationContext(org.apache.nifi.util.MockValidationContext) ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) MockProcessContext(org.apache.nifi.util.MockProcessContext) ValidationContext(org.apache.nifi.components.ValidationContext) MockValidationContext(org.apache.nifi.util.MockValidationContext) Test(org.junit.Test)

Aggregations

MockProcessContext (org.apache.nifi.util.MockProcessContext)6 MockValidationContext (org.apache.nifi.util.MockValidationContext)6 Test (org.junit.Test)6 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)2 ValidationResult (org.apache.nifi.components.ValidationResult)2 Relationship (org.apache.nifi.processor.Relationship)2 File (java.io.File)1 Collection (java.util.Collection)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 ATLAS_NIFI_URL (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_NIFI_URL)1 ATLAS_PASSWORD (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_PASSWORD)1 ATLAS_URLS (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_URLS)1 ATLAS_USER (org.apache.nifi.atlas.reporting.ReportLineageToAtlas.ATLAS_USER)1 ValidationContext (org.apache.nifi.components.ValidationContext)1 TestRunner (org.apache.nifi.util.TestRunner)1 Assert.assertTrue (org.junit.Assert.assertTrue)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1