You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2016/11/18 07:59:04 UTC
[1/2] camel git commit: Check-style related changes
Repository: camel
Updated Branches:
refs/heads/master 77f03faed -> 8fbddbd7f
Check-style related changes
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8fbddbd7
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8fbddbd7
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8fbddbd7
Branch: refs/heads/master
Commit: 8fbddbd7fa682e45e6f4a0d93ed37c92bf6313f0
Parents: 1195aca
Author: THAKKBH <TH...@TGA0045ALKHTASC.uswin.ad.vzwcorp.com>
Authored: Wed Nov 16 16:23:17 2016 -0500
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Fri Nov 18 08:53:26 2016 +0100
----------------------------------------------------------------------
.../component/springldap/LdapOperation.java | 59 +--
.../springldap/LdapOperationsFunction.java | 63 ++-
.../springldap/SpringLdapProducer.java | 254 +++++----
.../springldap/SpringLdapProducerTest.java | 510 +++++++++----------
4 files changed, 450 insertions(+), 436 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/8fbddbd7/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
index 68e219e..c88e00f 100644
--- a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
@@ -1,29 +1,30 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.springldap;
-
-import org.springframework.ldap.core.LdapOperations;
-
-/**
- * The list of supported LDAP operations. Currently supported operations are search, bind, and unbind, authenticate and modify_attributes.
- *
- * The function_driven operation expects a request {@link Object} along with an instance of {@link LdapOperationsFunction} that can be used to invoke any method on the
- * {@link LdapOperations} instance
- */
-public enum LdapOperation {
- SEARCH, BIND, UNBIND, AUTHENTICATE, MODIFY_ATTRIBUTES, FUNCTION_DRIVEN
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springldap;
+
+import org.springframework.ldap.core.LdapOperations;
+
+/**
+ * The list of supported LDAP operations. Currently supported operations are
+ * search, bind, and unbind, authenticate and modify_attributes. The
+ * function_driven operation expects a request {@link Object} along with an
+ * instance of {@link LdapOperationsFunction} that can be used to invoke any
+ * method on the {@link LdapOperations} instance
+ */
+public enum LdapOperation {
+ SEARCH, BIND, UNBIND, AUTHENTICATE, MODIFY_ATTRIBUTES, FUNCTION_DRIVEN
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/8fbddbd7/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
index 403a80c..ae3cf21 100644
--- a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
@@ -1,24 +1,39 @@
-package org.apache.camel.component.springldap;
-
-import org.springframework.ldap.core.LdapOperations;
-
-/**
- * Provides a way to invoke any method on {@link LdapOperations} when an operation is not provided out of the box by this component.
- *
- * @param <Q>
- * - The set of request parameters as expected by the method being invoked
- * @param <S>
- * - The response to be returned by the method being invoked
- */
-public interface LdapOperationsFunction<Q, S> {
-
- /**
- * @param ldapOperations
- * - An instance of {@link LdapOperations}
- * @param request
- * - Any object needed by the {@link LdapOperations} method being invoked
- * @return - result of the {@link LdapOperations} method being invoked
- */
- S apply(LdapOperations ldapOperations, Q request);
-
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springldap;
+
+import org.springframework.ldap.core.LdapOperations;
+
+/**
+ * Provides a way to invoke any method on {@link LdapOperations} when an
+ * operation is not provided out of the box by this component.
+ *
+ * @param <Q> - The set of request parameters as expected by the method being
+ * invoked
+ * @param <S> - The response to be returned by the method being invoked
+ */
+public interface LdapOperationsFunction<Q, S> {
+
+ /**
+ * @param ldapOperations - An instance of {@link LdapOperations}
+ * @param request - Any object needed by the {@link LdapOperations} method
+ * being invoked
+ * @return - result of the {@link LdapOperations} method being invoked
+ */
+ S apply(LdapOperations ldapOperations, Q request);
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/8fbddbd7/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
index 3a06d75..2c84bb3 100644
--- a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
@@ -1,128 +1,126 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.springldap;
-
-import static org.apache.camel.component.springldap.LdapOperation.FUNCTION_DRIVEN;
-
-import java.util.Map;
-
-import javax.naming.NamingException;
-import javax.naming.directory.Attributes;
-import javax.naming.directory.ModificationItem;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.impl.DefaultProducer;
-import org.apache.commons.lang.StringUtils;
-import org.springframework.ldap.core.AttributesMapper;
-import org.springframework.ldap.core.LdapOperations;
-import org.springframework.ldap.core.LdapTemplate;
-import org.springframework.ldap.query.LdapQueryBuilder;
-
-public class SpringLdapProducer extends DefaultProducer {
-
- public static final String DN = "dn";
- public static final String FILTER = "filter";
- public static final String ATTRIBUTES = "attributes";
- public static final String PASSWORD = "password";
- public static final String MODIFICATION_ITEMS = "modificationItems";
-
- public static final String FUNCTION = "function";
- public static final String REQUEST = "request";
-
- SpringLdapEndpoint endpoint;
-
- private AttributesMapper<Object> mapper = new AttributesMapper<Object>() {
-
- @Override
- public Object mapFromAttributes(Attributes attributes) throws NamingException {
- return attributes;
- }
- };
-
- /**
- * Initializes the SpringLdapProducer with the given endpoint
- */
- public SpringLdapProducer(SpringLdapEndpoint endpoint) {
- super(endpoint);
- this.endpoint = endpoint;
- }
-
- /**
- * 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 != 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:
- LdapOperationsFunction<Object, ?> ldapOperationFunction = (LdapOperationsFunction<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 + "'");
- }
- }
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springldap;
+
+import java.util.Map;
+
+import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.DefaultProducer;
+import org.apache.commons.lang.StringUtils;
+import org.springframework.ldap.core.AttributesMapper;
+import org.springframework.ldap.core.LdapOperations;
+import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.query.LdapQueryBuilder;
+
+public class SpringLdapProducer extends DefaultProducer {
+
+ public static final String DN = "dn";
+ public static final String FILTER = "filter";
+ public static final String ATTRIBUTES = "attributes";
+ public static final String PASSWORD = "password";
+ public static final String MODIFICATION_ITEMS = "modificationItems";
+
+ public static final String FUNCTION = "function";
+ public static final String REQUEST = "request";
+
+ SpringLdapEndpoint endpoint;
+
+ private AttributesMapper<Object> mapper = new AttributesMapper<Object>() {
+
+ @Override
+ public Object mapFromAttributes(Attributes attributes) throws NamingException {
+ return attributes;
+ }
+ };
+
+ /**
+ * Initializes the SpringLdapProducer with the given endpoint
+ */
+ public SpringLdapProducer(SpringLdapEndpoint endpoint) {
+ super(endpoint);
+ this.endpoint = endpoint;
+ }
+
+ /**
+ * 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:
+ LdapOperationsFunction<Object, ?> ldapOperationFunction = (LdapOperationsFunction<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
+ + "'");
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/8fbddbd7/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java b/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
index 8fb304c..d811c99 100644
--- a/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
+++ b/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
@@ -1,255 +1,255 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.camel.component.springldap;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isNull;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import javax.naming.directory.BasicAttribute;
-import javax.naming.directory.BasicAttributes;
-import javax.naming.directory.DirContext;
-import javax.naming.directory.ModificationItem;
-import javax.naming.directory.SearchControls;
-
-import org.apache.camel.Exchange;
-import org.apache.camel.Message;
-import org.apache.camel.impl.DefaultExchange;
-import org.apache.camel.impl.DefaultMessage;
-import org.apache.camel.test.junit4.CamelTestSupport;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Matchers;
-import org.mockito.Mockito;
-import org.springframework.ldap.core.AttributesMapper;
-import org.springframework.ldap.core.LdapOperations;
-import org.springframework.ldap.core.LdapTemplate;
-import org.springframework.ldap.query.LdapQuery;
-
-public class SpringLdapProducerTest extends CamelTestSupport {
-
- private SpringLdapEndpoint ldapEndpoint = Mockito.mock(SpringLdapEndpoint.class);
- private LdapTemplate ldapTemplate = Mockito.mock(LdapTemplate.class);
-
- private SpringLdapProducer ldapProducer = new SpringLdapProducer(ldapEndpoint);
-
- @Before
- public void setUp() {
- when(ldapEndpoint.getLdapTemplate()).thenReturn(ldapTemplate);
- }
-
- @Test(expected = NullPointerException.class)
- public void testEmptyExchange() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- ldapProducer.process(exchange);
- }
-
- @Test(expected = NullPointerException.class)
- public void testWrongBodyType() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
- in.setBody("");
-
- exchange.setIn(in);
- ldapProducer.process(exchange);
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void testNoDN() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
-
- processBody(exchange, in, body);
- }
-
- @Test
- public void testNoDN_FunctionDrivenOperation() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
- body.put(SpringLdapProducer.FUNCTION, Mockito.mock(LdapOperationsFunction.class));
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
-
- processBody(exchange, in, body);
- }
-
- private void processBody(Exchange exchange, Message message, Map<String, Object> body) throws Exception {
- message.setBody(body);
- exchange.setIn(message);
- ldapProducer.process(exchange);
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void testEmptyDN() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
- body.put(SpringLdapProducer.DN, "");
-
- processBody(exchange, in, body);
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void testNullDN() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
- body.put(SpringLdapProducer.DN, null);
-
- processBody(exchange, in, body);
- }
-
- @Test(expected = UnsupportedOperationException.class)
- public void testNullOperation() throws Exception {
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
- body.put(SpringLdapProducer.DN, " ");
-
- processBody(exchange, in, body);
- }
-
- @Test
- public void testSearch() throws Exception {
- String dn = "some dn";
- String filter = "filter";
- Integer scope = SearchControls.SUBTREE_SCOPE;
-
- 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.FILTER, filter);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.SEARCH);
- when(ldapEndpoint.scopeValue()).thenReturn(scope);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).search(eq(dn), eq(filter), eq(scope), any(AttributesMapper.class));
- }
-
- @Test
- public void testBind() throws Exception {
- String dn = "some dn";
- BasicAttributes attributes = new BasicAttributes();
-
- 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.ATTRIBUTES, attributes);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.BIND);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).bind(eq(dn), isNull(), eq(attributes));
- }
-
- @Test
- public void testUnbind() throws Exception {
- String dn = "some dn";
-
- Exchange exchange = new DefaultExchange(context);
- Message in = new DefaultMessage();
-
- Map<String, Object> body = new HashMap<String, Object>();
- body.put(SpringLdapProducer.DN, dn);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.UNBIND);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).unbind(eq(dn));
- }
-
- @Test
- public void testAuthenticate() throws Exception {
- String dn = "cn=dn";
- String filter = "filter";
- String password = "password";
-
- 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.FILTER, filter);
- body.put(SpringLdapProducer.PASSWORD, password);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.AUTHENTICATE);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).authenticate(Matchers.any(LdapQuery.class), eq(password));
- }
-
- @Test
- public void testModifyAttributes() throws Exception {
- String dn = "cn=dn";
- ModificationItem[] modificationItems = new ModificationItem[] {new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("key", "value"))};
-
- 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.MODIFICATION_ITEMS, modificationItems);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.MODIFY_ATTRIBUTES);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).modifyAttributes(eq(dn), eq(modificationItems));
- }
-
- @Test
- public void testFunctionDriven() throws Exception {
- String dn = "cn=dn";
- LdapOperationsFunction<String, Void> function = new LdapOperationsFunction<String, Void>() {
- @Override
- public Void apply(LdapOperations ldapOperations, String request) {
- ldapOperations.lookup(request);
- return null;
- }
- };
-
- 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, function);
-
- when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
-
- processBody(exchange, in, body);
- verify(ldapTemplate).lookup(eq(dn));
- }
-
-}
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.springldap;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.naming.directory.BasicAttribute;
+import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
+import javax.naming.directory.SearchControls;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.impl.DefaultExchange;
+import org.apache.camel.impl.DefaultMessage;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.mockito.Mockito;
+import org.springframework.ldap.core.AttributesMapper;
+import org.springframework.ldap.core.LdapOperations;
+import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.query.LdapQuery;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class SpringLdapProducerTest extends CamelTestSupport {
+
+ private SpringLdapEndpoint ldapEndpoint = Mockito.mock(SpringLdapEndpoint.class);
+ private LdapTemplate ldapTemplate = Mockito.mock(LdapTemplate.class);
+
+ private SpringLdapProducer ldapProducer = new SpringLdapProducer(ldapEndpoint);
+
+ @Before
+ public void setUp() {
+ when(ldapEndpoint.getLdapTemplate()).thenReturn(ldapTemplate);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testEmptyExchange() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ ldapProducer.process(exchange);
+ }
+
+ @Test(expected = NullPointerException.class)
+ public void testWrongBodyType() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+ in.setBody("");
+
+ exchange.setIn(in);
+ ldapProducer.process(exchange);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testNoDN() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+
+ processBody(exchange, in, body);
+ }
+
+ @Test
+ public void testNoDNForFunctionDrivenOperation() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.FUNCTION, Mockito.mock(LdapOperationsFunction.class));
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
+
+ processBody(exchange, in, body);
+ }
+
+ private void processBody(Exchange exchange, Message message, Map<String, Object> body) throws Exception {
+ message.setBody(body);
+ exchange.setIn(message);
+ ldapProducer.process(exchange);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testEmptyDN() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.DN, "");
+
+ processBody(exchange, in, body);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testNullDN() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.DN, null);
+
+ processBody(exchange, in, body);
+ }
+
+ @Test(expected = UnsupportedOperationException.class)
+ public void testNullOperation() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.DN, " ");
+
+ processBody(exchange, in, body);
+ }
+
+ @Test
+ public void testSearch() throws Exception {
+ String dn = "some dn";
+ String filter = "filter";
+ Integer scope = SearchControls.SUBTREE_SCOPE;
+
+ 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.FILTER, filter);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.SEARCH);
+ when(ldapEndpoint.scopeValue()).thenReturn(scope);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).search(eq(dn), eq(filter), eq(scope), any(AttributesMapper.class));
+ }
+
+ @Test
+ public void testBind() throws Exception {
+ String dn = "some dn";
+ BasicAttributes attributes = new BasicAttributes();
+
+ 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.ATTRIBUTES, attributes);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.BIND);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).bind(eq(dn), isNull(), eq(attributes));
+ }
+
+ @Test
+ public void testUnbind() throws Exception {
+ String dn = "some dn";
+
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.DN, dn);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.UNBIND);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).unbind(eq(dn));
+ }
+
+ @Test
+ public void testAuthenticate() throws Exception {
+ String dn = "cn=dn";
+ String filter = "filter";
+ String password = "password";
+
+ 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.FILTER, filter);
+ body.put(SpringLdapProducer.PASSWORD, password);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.AUTHENTICATE);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).authenticate(Matchers.any(LdapQuery.class), eq(password));
+ }
+
+ @Test
+ public void testModifyAttributes() throws Exception {
+ String dn = "cn=dn";
+ ModificationItem[] modificationItems = new ModificationItem[] {new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("key", "value"))};
+
+ 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.MODIFICATION_ITEMS, modificationItems);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.MODIFY_ATTRIBUTES);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).modifyAttributes(eq(dn), eq(modificationItems));
+ }
+
+ @Test
+ public void testFunctionDriven() throws Exception {
+ String dn = "cn=dn";
+ LdapOperationsFunction<String, Void> function = new LdapOperationsFunction<String, Void>() {
+ @Override
+ public Void apply(LdapOperations ldapOperations, String request) {
+ ldapOperations.lookup(request);
+ return null;
+ }
+ };
+
+ 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, function);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).lookup(eq(dn));
+ }
+
+}
[2/2] camel git commit: Added support for authenticate,
modify_attributes and function_driven operations
Posted by ac...@apache.org.
Added support for authenticate, modify_attributes and function_driven operations
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1195acaa
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1195acaa
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1195acaa
Branch: refs/heads/master
Commit: 1195acaa2b823fb8f97fa0fe3198ebffaba2f772
Parents: 77f03fa
Author: Bhavesh Thakker <bh...@gmail.com>
Authored: Wed Nov 16 15:59:45 2016 -0500
Committer: Andrea Cosentino <an...@gmail.com>
Committed: Fri Nov 18 08:53:26 2016 +0100
----------------------------------------------------------------------
.../component/springldap/LdapOperation.java | 10 +-
.../springldap/LdapOperationsFunction.java | 24 +++++
.../springldap/SpringLdapProducer.java | 62 +++++++----
.../springldap/SpringLdapProducerTest.java | 108 ++++++++++++++++---
4 files changed, 166 insertions(+), 38 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/1195acaa/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
index 2710acc..68e219e 100644
--- a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperation.java
@@ -16,10 +16,14 @@
*/
package org.apache.camel.component.springldap;
+import org.springframework.ldap.core.LdapOperations;
+
/**
- * The list of supported LDAP operations. Currently supported operations are
- * search, bind, and unbind. Modify is currently not supported.
+ * The list of supported LDAP operations. Currently supported operations are search, bind, and unbind, authenticate and modify_attributes.
+ *
+ * The function_driven operation expects a request {@link Object} along with an instance of {@link LdapOperationsFunction} that can be used to invoke any method on the
+ * {@link LdapOperations} instance
*/
public enum LdapOperation {
- SEARCH, BIND, UNBIND
+ SEARCH, BIND, UNBIND, AUTHENTICATE, MODIFY_ATTRIBUTES, FUNCTION_DRIVEN
}
http://git-wip-us.apache.org/repos/asf/camel/blob/1195acaa/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
new file mode 100644
index 0000000..403a80c
--- /dev/null
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/LdapOperationsFunction.java
@@ -0,0 +1,24 @@
+package org.apache.camel.component.springldap;
+
+import org.springframework.ldap.core.LdapOperations;
+
+/**
+ * Provides a way to invoke any method on {@link LdapOperations} when an operation is not provided out of the box by this component.
+ *
+ * @param <Q>
+ * - The set of request parameters as expected by the method being invoked
+ * @param <S>
+ * - The response to be returned by the method being invoked
+ */
+public interface LdapOperationsFunction<Q, S> {
+
+ /**
+ * @param ldapOperations
+ * - An instance of {@link LdapOperations}
+ * @param request
+ * - Any object needed by the {@link LdapOperations} method being invoked
+ * @return - result of the {@link LdapOperations} method being invoked
+ */
+ S apply(LdapOperations ldapOperations, Q request);
+
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/1195acaa/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
index 2649427..3a06d75 100644
--- a/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
+++ b/components/camel-spring-ldap/src/main/java/org/apache/camel/component/springldap/SpringLdapProducer.java
@@ -16,25 +16,36 @@
*/
package org.apache.camel.component.springldap;
+import static org.apache.camel.component.springldap.LdapOperation.FUNCTION_DRIVEN;
+
import java.util.Map;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
+import javax.naming.directory.ModificationItem;
import org.apache.camel.Exchange;
import org.apache.camel.impl.DefaultProducer;
+import org.apache.commons.lang.StringUtils;
import org.springframework.ldap.core.AttributesMapper;
+import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
+import org.springframework.ldap.query.LdapQueryBuilder;
public class SpringLdapProducer extends DefaultProducer {
public static final String DN = "dn";
public static final String FILTER = "filter";
public static final String ATTRIBUTES = "attributes";
+ public static final String PASSWORD = "password";
+ public static final String MODIFICATION_ITEMS = "modificationItems";
+
+ public static final String FUNCTION = "function";
+ public static final String REQUEST = "request";
SpringLdapEndpoint endpoint;
- private AttributesMapper mapper = new AttributesMapper() {
+ private AttributesMapper<Object> mapper = new AttributesMapper<Object>() {
@Override
public Object mapFromAttributes(Attributes attributes) throws NamingException {
@@ -51,52 +62,67 @@ public class SpringLdapProducer extends DefaultProducer {
}
/**
- * 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:
+ * 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);
- String dn = (String) body.get(DN);
-
- if (null == dn || dn.length() == 0) {
- throw new UnsupportedOperationException(
- "DN must not be empty, but you provided an empty DN");
- }
LdapOperation operation = endpoint.getOperation();
- LdapTemplate ldapTemplate = endpoint.getLdapTemplate();
-
if (null == operation) {
- throw new UnsupportedOperationException(
- "LDAP operation must not be empty, but you provided an empty operation");
+ throw new UnsupportedOperationException("LDAP operation must not be empty, but you provided an empty operation");
+ }
+
+ String dn = (String)body.get(DN);
+ if (operation != 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);
+ 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);
+ 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:
+ LdapOperationsFunction<Object, ?> ldapOperationFunction = (LdapOperationsFunction<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 + "'");
+ throw new UnsupportedOperationException(
+ "Bug in the Spring-LDAP component. Despite of all assertions, you managed to call an unsupported operation '" + operation + "'");
}
}
}
http://git-wip-us.apache.org/repos/asf/camel/blob/1195acaa/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java b/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
index ea014ed..8fb304c 100644
--- a/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
+++ b/components/camel-spring-ldap/src/test/java/org/apache/camel/component/springldap/SpringLdapProducerTest.java
@@ -16,38 +16,38 @@
*/
package org.apache.camel.component.springldap;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import java.util.HashMap;
import java.util.Map;
+import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.ModificationItem;
import javax.naming.directory.SearchControls;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.impl.DefaultExchange;
import org.apache.camel.impl.DefaultMessage;
-
import org.apache.camel.test.junit4.CamelTestSupport;
-
import org.junit.Before;
import org.junit.Test;
-
+import org.mockito.Matchers;
import org.mockito.Mockito;
-
import org.springframework.ldap.core.AttributesMapper;
+import org.springframework.ldap.core.LdapOperations;
import org.springframework.ldap.core.LdapTemplate;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Matchers.isNull;
-
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import org.springframework.ldap.query.LdapQuery;
public class SpringLdapProducerTest extends CamelTestSupport {
- private SpringLdapEndpoint ldapEndpoint = Mockito
- .mock(SpringLdapEndpoint.class);
+ private SpringLdapEndpoint ldapEndpoint = Mockito.mock(SpringLdapEndpoint.class);
private LdapTemplate ldapTemplate = Mockito.mock(LdapTemplate.class);
private SpringLdapProducer ldapProducer = new SpringLdapProducer(ldapEndpoint);
@@ -82,9 +82,21 @@ public class SpringLdapProducerTest extends CamelTestSupport {
processBody(exchange, in, body);
}
+
+ @Test
+ public void testNoDN_FunctionDrivenOperation() throws Exception {
+ Exchange exchange = new DefaultExchange(context);
+ Message in = new DefaultMessage();
+
+ Map<String, Object> body = new HashMap<String, Object>();
+ body.put(SpringLdapProducer.FUNCTION, Mockito.mock(LdapOperationsFunction.class));
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
+
+ processBody(exchange, in, body);
+ }
- private void processBody(Exchange exchange, Message message,
- Map<String, Object> body) throws Exception {
+ private void processBody(Exchange exchange, Message message, Map<String, Object> body) throws Exception {
message.setBody(body);
exchange.setIn(message);
ldapProducer.process(exchange);
@@ -140,8 +152,7 @@ public class SpringLdapProducerTest extends CamelTestSupport {
when(ldapEndpoint.scopeValue()).thenReturn(scope);
processBody(exchange, in, body);
- verify(ldapTemplate).search(eq(dn), eq(filter), eq(scope),
- any(AttributesMapper.class));
+ verify(ldapTemplate).search(eq(dn), eq(filter), eq(scope), any(AttributesMapper.class));
}
@Test
@@ -178,4 +189,67 @@ public class SpringLdapProducerTest extends CamelTestSupport {
verify(ldapTemplate).unbind(eq(dn));
}
+ @Test
+ public void testAuthenticate() throws Exception {
+ String dn = "cn=dn";
+ String filter = "filter";
+ String password = "password";
+
+ 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.FILTER, filter);
+ body.put(SpringLdapProducer.PASSWORD, password);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.AUTHENTICATE);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).authenticate(Matchers.any(LdapQuery.class), eq(password));
+ }
+
+ @Test
+ public void testModifyAttributes() throws Exception {
+ String dn = "cn=dn";
+ ModificationItem[] modificationItems = new ModificationItem[] {new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("key", "value"))};
+
+ 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.MODIFICATION_ITEMS, modificationItems);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.MODIFY_ATTRIBUTES);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).modifyAttributes(eq(dn), eq(modificationItems));
+ }
+
+ @Test
+ public void testFunctionDriven() throws Exception {
+ String dn = "cn=dn";
+ LdapOperationsFunction<String, Void> function = new LdapOperationsFunction<String, Void>() {
+ @Override
+ public Void apply(LdapOperations ldapOperations, String request) {
+ ldapOperations.lookup(request);
+ return null;
+ }
+ };
+
+ 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, function);
+
+ when(ldapEndpoint.getOperation()).thenReturn(LdapOperation.FUNCTION_DRIVEN);
+
+ processBody(exchange, in, body);
+ verify(ldapTemplate).lookup(eq(dn));
+ }
+
}