Search in sources :

Example 1 with MockPropertyValue

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

the class TestAmbariReportingTask method testOnTrigger.

@Test
public void testOnTrigger() throws InitializationException, IOException {
    final String metricsUrl = "http://myambari:6188/ws/v1/timeline/metrics";
    final String applicationId = "NIFI";
    final String hostName = "localhost";
    // create the jersey client mocks for handling the post
    final Client client = Mockito.mock(Client.class);
    final WebTarget target = Mockito.mock(WebTarget.class);
    final Invocation.Builder builder = Mockito.mock(Invocation.Builder.class);
    final Response response = Mockito.mock(Response.class);
    Mockito.when(response.getStatus()).thenReturn(200);
    Mockito.when(client.target(metricsUrl)).thenReturn(target);
    Mockito.when(target.request()).thenReturn(builder);
    Mockito.when(builder.post(Matchers.any(Entity.class))).thenReturn(response);
    // mock the ReportingInitializationContext for initialize(...)
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class);
    Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(initContext.getLogger()).thenReturn(logger);
    // mock the ConfigurationContext for setup(...)
    final ConfigurationContext configurationContext = Mockito.mock(ConfigurationContext.class);
    // mock the ReportingContext for onTrigger(...)
    final ReportingContext context = Mockito.mock(ReportingContext.class);
    Mockito.when(context.getProperty(AmbariReportingTask.METRICS_COLLECTOR_URL)).thenReturn(new MockPropertyValue(metricsUrl));
    Mockito.when(context.getProperty(AmbariReportingTask.APPLICATION_ID)).thenReturn(new MockPropertyValue(applicationId));
    Mockito.when(context.getProperty(AmbariReportingTask.HOSTNAME)).thenReturn(new MockPropertyValue(hostName));
    Mockito.when(context.getProperty(AmbariReportingTask.PROCESS_GROUP_ID)).thenReturn(new MockPropertyValue("1234"));
    final EventAccess eventAccess = Mockito.mock(EventAccess.class);
    Mockito.when(context.getEventAccess()).thenReturn(eventAccess);
    Mockito.when(eventAccess.getControllerStatus()).thenReturn(status);
    // create a testable instance of the reporting task
    final AmbariReportingTask task = new TestableAmbariReportingTask(client);
    task.initialize(initContext);
    task.setup(configurationContext);
    task.onTrigger(context);
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) Entity(javax.ws.rs.client.Entity) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) Invocation(javax.ws.rs.client.Invocation) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) ReportingContext(org.apache.nifi.reporting.ReportingContext) Response(javax.ws.rs.core.Response) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 2 with MockPropertyValue

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

the class TestSiteToSiteBulletinReportingTask method testWhenProvenanceMaxIdEqualToLastEventIdInStateManager.

@Test
public void testWhenProvenanceMaxIdEqualToLastEventIdInStateManager() throws IOException, InitializationException {
    // creating the list of bulletins
    final List<Bulletin> bulletins = new ArrayList<Bulletin>();
    bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
    bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
    bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
    bulletins.add(BulletinFactory.createBulletin("category", "severity", "message"));
    // mock the access to the list of bulletins
    final ReportingContext context = Mockito.mock(ReportingContext.class);
    final BulletinRepository repository = Mockito.mock(BulletinRepository.class);
    Mockito.when(context.getBulletinRepository()).thenReturn(repository);
    Mockito.when(repository.findBulletins(Mockito.any(BulletinQuery.class))).thenReturn(bulletins);
    final long maxEventId = getMaxBulletinId(bulletins);
    ;
    // create the mock reporting task and mock state manager
    final MockSiteToSiteBulletinReportingTask task = new MockSiteToSiteBulletinReportingTask();
    final MockStateManager stateManager = new MockStateManager(task);
    // settings properties and mocking access to properties
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    for (final PropertyDescriptor descriptor : task.getSupportedPropertyDescriptors()) {
        properties.put(descriptor, descriptor.getDefaultValue());
    }
    properties.put(SiteToSiteBulletinReportingTask.BATCH_SIZE, "1000");
    properties.put(SiteToSiteBulletinReportingTask.PLATFORM, "nifi");
    properties.put(SiteToSiteBulletinReportingTask.TRANSPORT_PROTOCOL, SiteToSiteTransportProtocol.HTTP.name());
    properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_HOSTNAME, "localhost");
    properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_PORT, "80");
    properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_USERNAME, "username");
    properties.put(SiteToSiteBulletinReportingTask.HTTP_PROXY_PASSWORD, "password");
    Mockito.doAnswer(new Answer<PropertyValue>() {

        @Override
        public PropertyValue answer(final InvocationOnMock invocation) throws Throwable {
            final PropertyDescriptor descriptor = invocation.getArgumentAt(0, PropertyDescriptor.class);
            return new MockPropertyValue(properties.get(descriptor));
        }
    }).when(context).getProperty(Mockito.any(PropertyDescriptor.class));
    // create the state map and set the last id to the same value as maxEventId
    final Map<String, String> state = new HashMap<>();
    state.put(SiteToSiteProvenanceReportingTask.LAST_EVENT_ID_KEY, String.valueOf(maxEventId));
    stateManager.setState(state, Scope.LOCAL);
    // setup the mock reporting context to return the mock state manager
    Mockito.when(context.getStateManager()).thenReturn(stateManager);
    // setup the mock initialization context
    final ComponentLog logger = Mockito.mock(ComponentLog.class);
    final ReportingInitializationContext initContext = Mockito.mock(ReportingInitializationContext.class);
    Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
    Mockito.when(initContext.getLogger()).thenReturn(logger);
    task.initialize(initContext);
    // execute the reporting task and should not produce any data b/c max id same as previous id
    task.onTrigger(context);
    assertEquals(0, task.dataSent.size());
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) StandardPropertyValue(org.apache.nifi.attribute.expression.language.StandardPropertyValue) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ComponentLog(org.apache.nifi.logging.ComponentLog) MockStateManager(org.apache.nifi.state.MockStateManager) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Test(org.junit.Test)

Example 3 with MockPropertyValue

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

the class TestRangerNiFiAuthorizer method testKerberosEnabledWithoutKeytabOrPrincipal.

@Test
public void testKerberosEnabledWithoutKeytabOrPrincipal() {
    when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_KERBEROS_ENABLED_PROP))).thenReturn(new MockPropertyValue("true"));
    nifiProperties = Mockito.mock(NiFiProperties.class);
    when(nifiProperties.getKerberosServiceKeytabLocation()).thenReturn("");
    when(nifiProperties.getKerberosServicePrincipal()).thenReturn("");
    authorizer = new MockRangerNiFiAuthorizer(rangerBasePlugin);
    authorizer.setNiFiProperties(nifiProperties);
    try {
        authorizer.onConfigured(configurationContext);
        Assert.fail("Should have thrown exception");
    } catch (AuthorizerCreationException e) {
        // want to make sure this exception is from our authorizer code
        verifyOnlyAuthorizeCreationExceptions(e);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) Test(org.junit.Test)

Example 4 with MockPropertyValue

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

the class TestRangerNiFiAuthorizer method testKerberosEnabledWithoutKeytab.

@Test
public void testKerberosEnabledWithoutKeytab() {
    when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_KERBEROS_ENABLED_PROP))).thenReturn(new MockPropertyValue("true"));
    nifiProperties = Mockito.mock(NiFiProperties.class);
    when(nifiProperties.getKerberosServicePrincipal()).thenReturn("");
    authorizer = new MockRangerNiFiAuthorizer(rangerBasePlugin);
    authorizer.setNiFiProperties(nifiProperties);
    try {
        authorizer.onConfigured(configurationContext);
        Assert.fail("Should have thrown exception");
    } catch (AuthorizerCreationException e) {
        // want to make sure this exception is from our authorizer code
        verifyOnlyAuthorizeCreationExceptions(e);
    }
}
Also used : NiFiProperties(org.apache.nifi.util.NiFiProperties) AuthorizerCreationException(org.apache.nifi.authorization.exception.AuthorizerCreationException) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) Test(org.junit.Test)

Example 5 with MockPropertyValue

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

the class TestRangerNiFiAuthorizer method runRangerAdminTest.

private void runRangerAdminTest(final String resourceIdentifier, final AuthorizationResult.Result expectedResult) {
    configurationContext = createMockConfigContext();
    final String rangerAdminIdentity = "ranger-admin";
    when(configurationContext.getProperty(eq(RangerNiFiAuthorizer.RANGER_ADMIN_IDENTITY_PROP))).thenReturn(new MockPropertyValue(rangerAdminIdentity));
    rangerBasePlugin = Mockito.mock(RangerBasePluginWithPolicies.class);
    authorizer = new MockRangerNiFiAuthorizer(rangerBasePlugin);
    authorizer.onConfigured(configurationContext);
    final RequestAction action = RequestAction.WRITE;
    // the incoming NiFi request to test
    final AuthorizationRequest request = new AuthorizationRequest.Builder().resource(new MockResource(resourceIdentifier, resourceIdentifier)).action(action).identity(rangerAdminIdentity).resourceContext(new HashMap<>()).accessAttempt(true).anonymous(false).build();
    // the expected Ranger resource and request that are created
    final RangerAccessResourceImpl resource = new RangerAccessResourceImpl();
    resource.setValue(RangerNiFiAuthorizer.RANGER_NIFI_RESOURCE_NAME, resourceIdentifier);
    final RangerAccessRequestImpl expectedRangerRequest = new RangerAccessRequestImpl();
    expectedRangerRequest.setResource(resource);
    expectedRangerRequest.setAction(request.getAction().name());
    expectedRangerRequest.setAccessType(request.getAction().name());
    expectedRangerRequest.setUser(request.getIdentity());
    // return true when checking if a policy exists for the resource
    when(rangerBasePlugin.doesPolicyExist(resourceIdentifier, action)).thenReturn(true);
    // a non-null result processor should be used for direct access
    when(rangerBasePlugin.isAccessAllowed(argThat(new RangerAccessRequestMatcher(expectedRangerRequest)))).thenReturn(notAllowedResult);
    final AuthorizationResult result = authorizer.authorize(request);
    assertEquals(expectedResult, result.getResult());
}
Also used : RangerAccessRequestImpl(org.apache.ranger.plugin.policyengine.RangerAccessRequestImpl) AuthorizationRequest(org.apache.nifi.authorization.AuthorizationRequest) RequestAction(org.apache.nifi.authorization.RequestAction) HashMap(java.util.HashMap) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) AuthorizationResult(org.apache.nifi.authorization.AuthorizationResult) RangerAccessResourceImpl(org.apache.ranger.plugin.policyengine.RangerAccessResourceImpl)

Aggregations

MockPropertyValue (org.apache.nifi.util.MockPropertyValue)23 Test (org.junit.Test)14 PropertyValue (org.apache.nifi.components.PropertyValue)10 HashMap (java.util.HashMap)5 ComponentLog (org.apache.nifi.logging.ComponentLog)5 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)5 MockStateManager (org.apache.nifi.state.MockStateManager)5 AuthorizerCreationException (org.apache.nifi.authorization.exception.AuthorizerCreationException)4 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)4 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)4 NiFiProperties (org.apache.nifi.util.NiFiProperties)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ArrayList (java.util.ArrayList)3 AuthorizerConfigurationContext (org.apache.nifi.authorization.AuthorizerConfigurationContext)3 ConfigurationContext (org.apache.nifi.controller.ConfigurationContext)3 EventAccess (org.apache.nifi.reporting.EventAccess)3 ReportingContext (org.apache.nifi.reporting.ReportingContext)3 ReportingInitializationContext (org.apache.nifi.reporting.ReportingInitializationContext)3 StandardPropertyValue (org.apache.nifi.attribute.expression.language.StandardPropertyValue)2 AuthorizationRequest (org.apache.nifi.authorization.AuthorizationRequest)2