Added: sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/HttpTransportHandlerTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/HttpTransportHandlerTest.java?rev=1547378&view=auto ============================================================================== --- sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/HttpTransportHandlerTest.java (added) +++ sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/HttpTransportHandlerTest.java Tue Dec 3 13:19:50 2013 @@ -0,0 +1,58 @@ +/* + * 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.sling.replication.transport.impl; + +import java.net.URI; +import org.apache.http.client.fluent.Content; +import org.apache.http.client.fluent.Executor; +import org.apache.http.client.fluent.Request; +import org.apache.http.client.fluent.Response; +import org.apache.sling.replication.communication.ReplicationActionType; +import org.apache.sling.replication.communication.ReplicationEndpoint; +import org.apache.sling.replication.serialization.ReplicationPackage; +import org.apache.sling.replication.transport.authentication.AuthenticationContext; +import org.apache.sling.replication.transport.authentication.AuthenticationHandler; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Testcase for {@link HttpTransportHandler} + */ +public class HttpTransportHandlerTest { + @Test + public void testHttpTransport() throws Exception { + HttpTransportHandler httpTransportHandler = new HttpTransportHandler(); + ReplicationPackage replicationPackage = mock(ReplicationPackage.class); + when(replicationPackage.getAction()).thenReturn(ReplicationActionType.ACTIVATE.toString()); + when(replicationPackage.getType()).thenReturn("test"); + when(replicationPackage.getPaths()).thenReturn(new String[]{"/"}); + ReplicationEndpoint replicationEndpoint = new ReplicationEndpoint(new URI("http://localhost:8080/system/replication/receive")); + AuthenticationHandler<Executor, Executor> authenticationHandler = mock(AuthenticationHandler.class); + Executor executor = mock(Executor.class); + Response response = mock(Response.class); + Content content = mock(Content.class); + when(response.returnContent()).thenReturn(content); + when(executor.execute(any(Request.class))).thenReturn(response); + when(authenticationHandler.authenticate(any(Executor.class), any(AuthenticationContext.class))).thenReturn(executor); + httpTransportHandler.transport(replicationPackage, replicationEndpoint, authenticationHandler); + } +}
Added: sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/PollingTransportHandlerTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/PollingTransportHandlerTest.java?rev=1547378&view=auto ============================================================================== --- sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/PollingTransportHandlerTest.java (added) +++ sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/PollingTransportHandlerTest.java Tue Dec 3 13:19:50 2013 @@ -0,0 +1,62 @@ +/* + * 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.sling.replication.transport.impl; + +import java.net.URI; +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.fluent.Executor; +import org.apache.http.client.fluent.Request; +import org.apache.http.client.fluent.Response; +import org.apache.sling.replication.communication.ReplicationActionType; +import org.apache.sling.replication.communication.ReplicationEndpoint; +import org.apache.sling.replication.serialization.ReplicationPackage; +import org.apache.sling.replication.transport.authentication.AuthenticationContext; +import org.apache.sling.replication.transport.authentication.AuthenticationHandler; +import org.junit.Test; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Testcase for {@link PollingTransportHandler} + */ +public class PollingTransportHandlerTest { + + @Test + public void testPollingTransport() throws Exception { + PollingTransportHandler pollingTransportHandler = new PollingTransportHandler(); + ReplicationPackage replicationPackage = mock(ReplicationPackage.class); + when(replicationPackage.getAction()).thenReturn(ReplicationActionType.ACTIVATE.toString()); + when(replicationPackage.getType()).thenReturn("test"); + when(replicationPackage.getPaths()).thenReturn(new String[]{"/"}); + ReplicationEndpoint replicationEndpoint = new ReplicationEndpoint(new URI("http://localhost:8080/system/replication/agent/reverse")); + AuthenticationHandler<Executor, Executor> authenticationHandler = mock(AuthenticationHandler.class); + Executor executor = mock(Executor.class); + Response response = mock(Response.class); + HttpEntity entity = mock(HttpEntity.class); + HttpResponse httpResponse = mock(HttpResponse.class); + when(response.returnResponse()).thenReturn(httpResponse); + when(httpResponse.getEntity()).thenReturn(entity); + when(executor.execute(any(Request.class))).thenReturn(response); + when(authenticationHandler.authenticate(any(Executor.class), any(AuthenticationContext.class))).thenReturn(executor); + pollingTransportHandler.transport(replicationPackage, replicationEndpoint, authenticationHandler); + } +} Added: sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/RepositoryTransportHandlerTest.java URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/RepositoryTransportHandlerTest.java?rev=1547378&view=auto ============================================================================== --- sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/RepositoryTransportHandlerTest.java (added) +++ sling/trunk/contrib/extensions/replication/src/test/java/org/apache/sling/replication/transport/impl/RepositoryTransportHandlerTest.java Tue Dec 3 13:19:50 2013 @@ -0,0 +1,84 @@ +/* + * 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.sling.replication.transport.impl; + +import java.lang.reflect.Field; +import javax.jcr.Credentials; +import javax.jcr.Node; +import javax.jcr.Session; +import org.apache.sling.jcr.api.SlingRepository; +import org.apache.sling.replication.communication.ReplicationEndpoint; +import org.apache.sling.replication.serialization.ReplicationPackage; +import org.apache.sling.replication.transport.ReplicationTransportException; +import org.apache.sling.replication.transport.authentication.AuthenticationHandler; +import org.apache.sling.replication.transport.authentication.impl.RepositoryAuthenticationHandler; +import org.junit.Test; + +import static org.junit.Assert.fail; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Testcase for {@link RepositoryTransportHandler} + */ +public class RepositoryTransportHandlerTest { + + @Test + public void testDeliveryWithoutAuthenticatedSession() throws Exception { + RepositoryTransportHandler handler = new RepositoryTransportHandler(); + @SuppressWarnings("unchecked") + AuthenticationHandler<SlingRepository, Session> authenticationHandler = mock(AuthenticationHandler.class); + try { + handler.transport(null, new ReplicationEndpoint("repo://var/outbox/replication/rev1"), + authenticationHandler); + fail("cannot deliver without a proper session"); + } catch (ReplicationTransportException re) { + // failure expected + } + } + + @Test + public void testDeliveryWithAuthenticatedSession() throws Exception { + String repoPath = "var/outbox/replication/rev1"; + + Node addedNode = mock(Node.class); + when(addedNode.getPath()).thenReturn(repoPath + "/some-id"); + + Node node = mock(Node.class); + when(node.addNode(any(String.class), any(String.class))).thenReturn(addedNode); + + Session session = mock(Session.class); + when(session.getNode(repoPath)).thenReturn(node); + when(session.nodeExists(repoPath)).thenReturn(true); + + SlingRepository repo = mock(SlingRepository.class); + when(repo.login(any(Credentials.class))).thenReturn(session); + + RepositoryTransportHandler handler = new RepositoryTransportHandler(); + Field field = handler.getClass().getDeclaredField("repository"); + field.setAccessible(true); + field.set(handler, repo); + AuthenticationHandler<SlingRepository, Session> authenticationHandler = new RepositoryAuthenticationHandler("user-123", "p455w0rd"); + ReplicationPackage replicationPackage = mock(ReplicationPackage.class); + when(replicationPackage.getId()).thenReturn("some-id"); + handler.transport(replicationPackage, new ReplicationEndpoint("repo://" + repoPath), + authenticationHandler); + } +}
