Search in sources :

Example 36 with BiFunction

use of java.util.function.BiFunction in project riposte by Nike-Inc.

the class AsyncNettyHelperTest method biFunctionWithTracingAndMdc_separate_args_works_as_expected.

@Test
public void biFunctionWithTracingAndMdc_separate_args_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = generateTracingAndMdcInfo();
    // when
    BiFunction result = AsyncNettyHelper.biFunctionWithTracingAndMdc(biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
    // then
    verifyBiFunctionWithTracingAndMdcSupport(result, biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
}
Also used : BiFunction(java.util.function.BiFunction) Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 37 with BiFunction

use of java.util.function.BiFunction in project riposte by Nike-Inc.

the class AsyncNettyHelperTest method biFunctionWithTracingAndMdc_ctx_works_as_expected.

@Test
public void biFunctionWithTracingAndMdc_ctx_works_as_expected() {
    // given
    Pair<Deque<Span>, Map<String, String>> setupInfo = setupStateWithTracingAndMdcInfo();
    // when
    BiFunction result = AsyncNettyHelper.biFunctionWithTracingAndMdc(biFunctionMock, ctxMock);
    // then
    verifyBiFunctionWithTracingAndMdcSupport(result, biFunctionMock, setupInfo.getLeft(), setupInfo.getRight());
}
Also used : BiFunction(java.util.function.BiFunction) Deque(java.util.Deque) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 38 with BiFunction

use of java.util.function.BiFunction in project camel by apache.

the class SpringLdapProducer method process.

/**
     * Performs the LDAP operation defined in SpringLdapEndpoint that created
     * this producer. The in-message in the exchange must be a map, containing
     * the following entries:
     * 
     * <pre>
     * key: "dn" - base DN for the LDAP operation
     * key: "filter" - necessary for the search operation only; LDAP filter for the search operation,
     * see <a http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol>http://en.wikipedia.org/wiki/Lightweight_Directory_Access_Protocol</a>
     * key: "attributes" - necessary for the bind operation only; an instance of javax.naming.directory.Attributes,
     * containing the information necessary to create an LDAP node.
     * key: "password" - necessary for the authentication operation only;
     * key: "modificationItems" - necessary for the modify_attributes operation only;
     * key: "function" - necessary for the function_driven operation only; provides a flexible hook into the {@link LdapTemplate} to call any method
     * key: "request" - necessary for the function_driven operation only; passed into the "function" to enable the client to bind parameters that need to be passed into the {@link LdapTemplate}
     * </pre>
     * 
     * The keys are defined as final fields above.
     */
@Override
public void process(Exchange exchange) throws Exception {
    @SuppressWarnings("unchecked") Map<String, Object> body = exchange.getIn().getBody(Map.class);
    LdapOperation operation = endpoint.getOperation();
    if (null == operation) {
        throw new UnsupportedOperationException("LDAP operation must not be empty, but you provided an empty operation");
    }
    String dn = (String) body.get(DN);
    if (operation != LdapOperation.FUNCTION_DRIVEN && (StringUtils.isBlank(dn))) {
        throw new UnsupportedOperationException("DN must not be empty, but you provided an empty DN");
    }
    LdapOperations ldapTemplate = endpoint.getLdapTemplate();
    switch(operation) {
        case SEARCH:
            String filter = (String) body.get(FILTER);
            exchange.getIn().setBody(ldapTemplate.search(dn, filter, endpoint.scopeValue(), mapper));
            break;
        case BIND:
            Attributes attributes = (Attributes) body.get(ATTRIBUTES);
            ldapTemplate.bind(dn, null, attributes);
            break;
        case UNBIND:
            ldapTemplate.unbind(dn);
            break;
        case AUTHENTICATE:
            ldapTemplate.authenticate(LdapQueryBuilder.query().base(dn).filter((String) body.get(FILTER)), (String) body.get(PASSWORD));
            break;
        case MODIFY_ATTRIBUTES:
            ModificationItem[] modificationItems = (ModificationItem[]) body.get(MODIFICATION_ITEMS);
            ldapTemplate.modifyAttributes(dn, modificationItems);
            break;
        case FUNCTION_DRIVEN:
            BiFunction<LdapOperations, Object, ?> ldapOperationFunction = (BiFunction<LdapOperations, Object, ?>) body.get(FUNCTION);
            Object ldapOperationRequest = body.get(REQUEST);
            exchange.getIn().setBody(ldapOperationFunction.apply(ldapTemplate, ldapOperationRequest));
            break;
        default:
            throw new UnsupportedOperationException("Bug in the Spring-LDAP component. Despite of all assertions, you managed to call an unsupported operation '" + operation + "'");
    }
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) BiFunction(java.util.function.BiFunction) LdapOperations(org.springframework.ldap.core.LdapOperations) Attributes(javax.naming.directory.Attributes)

Example 39 with BiFunction

use of java.util.function.BiFunction in project camel by apache.

the class SpringLdapProducerTest method testFunctionDriven.

@Test
public void testFunctionDriven() throws Exception {
    String dn = "cn=dn";
    Exchange exchange = new DefaultExchange(context);
    Message in = new DefaultMessage();
    Map<String, Object> body = new HashMap<String, Object>();
    body.put(SpringLdapProducer.DN, dn);
    body.put(SpringLdapProducer.REQUEST, dn);
    body.put(SpringLdapProducer.FUNCTION, (BiFunction<LdapOperations, String, Void>) (l, q) -> {
        l.lookup(q);
        return null;
    });
    when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
    processBody(exchange, in, body);
    verify(ldapTemplate).lookup(eq(dn));
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) DefaultMessage(org.apache.camel.impl.DefaultMessage) ModificationItem(javax.naming.directory.ModificationItem) Message(org.apache.camel.Message) Matchers(org.mockito.Matchers) BiFunction(java.util.function.BiFunction) HashMap(java.util.HashMap) Exchange(org.apache.camel.Exchange) LdapTemplate(org.springframework.ldap.core.LdapTemplate) SearchControls(javax.naming.directory.SearchControls) BasicAttribute(javax.naming.directory.BasicAttribute) LdapOperations(org.springframework.ldap.core.LdapOperations) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) Before(org.junit.Before) LdapQuery(org.springframework.ldap.query.LdapQuery) DefaultExchange(org.apache.camel.impl.DefaultExchange) Matchers.isNull(org.mockito.Matchers.isNull) DirContext(javax.naming.directory.DirContext) BasicAttributes(javax.naming.directory.BasicAttributes) DefaultMessage(org.apache.camel.impl.DefaultMessage) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) AttributesMapper(org.springframework.ldap.core.AttributesMapper) CamelTestSupport(org.apache.camel.test.junit4.CamelTestSupport) Message(org.apache.camel.Message) DefaultMessage(org.apache.camel.impl.DefaultMessage) HashMap(java.util.HashMap) LdapOperations(org.springframework.ldap.core.LdapOperations) Test(org.junit.Test)

Example 40 with BiFunction

use of java.util.function.BiFunction in project samza by apache.

the class TestStreamGraphImpl method testGetInputStream.

@Test
public void testGetInputStream() {
    ApplicationRunner mockRunner = mock(ApplicationRunner.class);
    Config mockConfig = mock(Config.class);
    StreamSpec testStreamSpec = new StreamSpec("test-stream-1", "physical-stream-1", "test-system");
    when(mockRunner.getStreamSpec("test-stream-1")).thenReturn(testStreamSpec);
    StreamGraphImpl graph = new StreamGraphImpl(mockRunner, mockConfig);
    BiFunction<String, MessageType, TestInputMessageEnvelope> xMsgBuilder = (k, v) -> new TestInputMessageEnvelope(k, v.getValue(), v.getEventTime(), "input-id-1");
    MessageStream<TestMessageEnvelope> mInputStream = graph.getInputStream("test-stream-1", xMsgBuilder);
    assertEquals(graph.getInputStreams().get(testStreamSpec), mInputStream);
    assertTrue(mInputStream instanceof InputStreamInternalImpl);
    assertEquals(((InputStreamInternalImpl) mInputStream).getMsgBuilder(), xMsgBuilder);
    String key = "test-input-key";
    MessageType msgBody = new MessageType("test-msg-value", 333333L);
    TestMessageEnvelope xInputMsg = ((InputStreamInternalImpl<String, MessageType, TestMessageEnvelope>) mInputStream).getMsgBuilder().apply(key, msgBody);
    assertEquals(xInputMsg.getKey(), key);
    assertEquals(xInputMsg.getMessage().getValue(), msgBody.getValue());
    assertEquals(xInputMsg.getMessage().getEventTime(), msgBody.getEventTime());
    assertEquals(((TestInputMessageEnvelope) xInputMsg).getInputId(), "input-id-1");
}
Also used : TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) MessageType(org.apache.samza.operators.data.MessageType) BiFunction(java.util.function.BiFunction) JobConfig(org.apache.samza.config.JobConfig) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) Assert.assertTrue(org.junit.Assert.assertTrue) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Function(java.util.function.Function) OutputStreamInternalImpl(org.apache.samza.operators.stream.OutputStreamInternalImpl) Config(org.apache.samza.config.Config) IntermediateStreamInternalImpl(org.apache.samza.operators.stream.IntermediateStreamInternalImpl) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.mock(org.mockito.Mockito.mock) StreamSpec(org.apache.samza.system.StreamSpec) TestMessageEnvelope(org.apache.samza.operators.data.TestMessageEnvelope) ApplicationRunner(org.apache.samza.runtime.ApplicationRunner) JobConfig(org.apache.samza.config.JobConfig) Config(org.apache.samza.config.Config) TestInputMessageEnvelope(org.apache.samza.operators.data.TestInputMessageEnvelope) InputStreamInternalImpl(org.apache.samza.operators.stream.InputStreamInternalImpl) MessageType(org.apache.samza.operators.data.MessageType) Test(org.junit.Test)

Aggregations

BiFunction (java.util.function.BiFunction)51 List (java.util.List)25 HashMap (java.util.HashMap)24 Map (java.util.Map)24 ArrayList (java.util.ArrayList)21 Test (org.junit.Test)21 Collectors (java.util.stream.Collectors)18 Collections (java.util.Collections)15 Arrays (java.util.Arrays)14 Set (java.util.Set)14 Function (java.util.function.Function)14 Collection (java.util.Collection)13 IOException (java.io.IOException)12 IntStream (java.util.stream.IntStream)12 Mockito.mock (org.mockito.Mockito.mock)12 Before (org.junit.Before)11 Stream (java.util.stream.Stream)10 HashSet (java.util.HashSet)8 TimeUnit (java.util.concurrent.TimeUnit)8 Logger (org.apache.logging.log4j.Logger)8