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 <thak...@tga0045alkhtasc.uswin.ad.vzwcorp.com> Authored: Wed Nov 16 16:23:17 2016 -0500 Committer: Andrea Cosentino <anco...@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)); + } + +}