Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/ContentRemoteTreeTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/ContentRemoteTreeTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/ContentRemoteTreeTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/ContentRemoteTreeTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,947 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import com.google.common.collect.Sets; +import org.apache.jackrabbit.oak.api.Blob; +import org.apache.jackrabbit.oak.api.PropertyState; +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.api.Type; +import org.apache.jackrabbit.oak.remote.RemoteTree; +import org.apache.jackrabbit.oak.remote.RemoteTreeFilters; +import org.apache.jackrabbit.oak.remote.RemoteValue; +import org.apache.jackrabbit.util.ISO8601; +import org.junit.Test; + +import java.io.InputStream; +import java.math.BigDecimal; +import java.util.Calendar; +import java.util.Map; +import java.util.Set; + +import static com.google.common.collect.Iterables.getOnlyElement; +import static com.google.common.collect.Sets.newHashSet; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class ContentRemoteTreeTest { + + private ContentRemoteTree createTree(Tree tree) { + return new ContentRemoteTree(tree, 0, new RemoteTreeFilters(), mock(ContentRemoteBinaries.class)); + } + + private ContentRemoteTree createTree(Tree tree, ContentRemoteBinaries binaries) { + return new ContentRemoteTree(tree, 0, new RemoteTreeFilters(), binaries); + } + + private ContentRemoteTree createTree(Tree tree, RemoteTreeFilters filters) { + return new ContentRemoteTree(tree, 0, filters, mock(ContentRemoteBinaries.class)); + } + + @Test + public void testGetBinaryProperty() { + InputStream stream = mock(InputStream.class); + + Blob blob = mock(Blob.class); + doReturn(stream).when(blob).getNewStream(); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BINARY).when(property).getType(); + doReturn(blob).when(property).getValue(Type.BINARY); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public long getBinaryThreshold() { + return Long.MAX_VALUE; + } + + }); + + Map<String, RemoteValue> properties = remoteTree.getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isBinary()); + assertEquals(stream, properties.get("name").asBinary().get()); + } + + @Test + public void testGetMultiBinaryProperty() { + InputStream stream = mock(InputStream.class); + + Blob blob = mock(Blob.class); + doReturn(stream).when(blob).getNewStream(); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BINARIES).when(property).getType(); + doReturn(singletonList(blob)).when(property).getValue(Type.BINARIES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public long getBinaryThreshold() { + return Long.MAX_VALUE; + } + + }); + + Map<String, RemoteValue> properties = remoteTree.getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiBinary()); + assertEquals(stream, getOnlyElement(properties.get("name").asMultiBinary()).get()); + } + + @Test + public void testGetBinaryIdProperty() { + Blob blob = mock(Blob.class); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BINARY).when(property).getType(); + doReturn(blob).when(property).getValue(Type.BINARY); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + ContentRemoteBinaries binaries = mock(ContentRemoteBinaries.class); + doReturn("id").when(binaries).put(blob); + + Map<String, RemoteValue> properties = createTree(tree, binaries).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isBinaryId()); + assertEquals("id", properties.get("name").asBinaryId()); + } + + @Test + public void testGetMultiBinaryIdProperty() { + Blob blob = mock(Blob.class); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BINARIES).when(property).getType(); + doReturn(singletonList(blob)).when(property).getValue(Type.BINARIES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + ContentRemoteBinaries binaries = mock(ContentRemoteBinaries.class); + doReturn("id").when(binaries).put(blob); + + Map<String, RemoteValue> properties = createTree(tree, binaries).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiBinaryId()); + assertEquals("id", getOnlyElement(properties.get("name").asMultiBinaryId())); + } + + @Test + public void testGetBooleanProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BOOLEAN).when(property).getType(); + doReturn(true).when(property).getValue(Type.BOOLEAN); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isBoolean()); + assertEquals(true, properties.get("name").asBoolean()); + } + + @Test + public void testGetMultiBooleanProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.BOOLEANS).when(property).getType(); + doReturn(singletonList(true)).when(property).getValue(Type.BOOLEANS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiBoolean()); + assertEquals(true, getOnlyElement(properties.get("name").asMultiBoolean())); + } + + @Test + public void testGetDateProperty() { + Calendar calendar = Calendar.getInstance(); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DATE).when(property).getType(); + doReturn(ISO8601.format(calendar)).when(property).getValue(Type.DATE); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isDate()); + assertEquals(calendar.getTimeInMillis(), properties.get("name").asDate().longValue()); + } + + @Test + public void testGetMultiDateProperty() { + Calendar calendar = Calendar.getInstance(); + + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DATES).when(property).getType(); + doReturn(singletonList(ISO8601.format(calendar))).when(property).getValue(Type.DATES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiDate()); + assertEquals(calendar.getTimeInMillis(), getOnlyElement(properties.get("name").asMultiDate()).longValue()); + } + + @Test + public void testDecimalProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DECIMAL).when(property).getType(); + doReturn(BigDecimal.ONE).when(property).getValue(Type.DECIMAL); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isDecimal()); + assertEquals(BigDecimal.ONE, properties.get("name").asDecimal()); + } + + @Test + public void testGetMultiDecimalProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DECIMALS).when(property).getType(); + doReturn(singletonList(BigDecimal.ONE)).when(property).getValue(Type.DECIMALS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiDecimal()); + assertEquals(BigDecimal.ONE, getOnlyElement(properties.get("name").asMultiDecimal())); + } + + @Test + public void testDoubleProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DOUBLE).when(property).getType(); + doReturn(4.2).when(property).getValue(Type.DOUBLE); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isDouble()); + assertEquals(4.2, properties.get("name").asDouble(), 1e-9); + } + + @Test + public void testGetMultiDoubleProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.DOUBLES).when(property).getType(); + doReturn(singletonList(4.2)).when(property).getValue(Type.DOUBLES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiDouble()); + assertEquals(4.2, getOnlyElement(properties.get("name").asMultiDouble()), 1e-9); + } + + @Test + public void testLongProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.LONG).when(property).getType(); + doReturn(42L).when(property).getValue(Type.LONG); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isLong()); + assertEquals(42L, properties.get("name").asLong().longValue()); + } + + @Test + public void testGetMultiLongProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.LONGS).when(property).getType(); + doReturn(singletonList(42L)).when(property).getValue(Type.LONGS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiLong()); + assertEquals(42L, getOnlyElement(properties.get("name").asMultiLong()).longValue()); + } + + @Test + public void testNameProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.NAME).when(property).getType(); + doReturn("value").when(property).getValue(Type.NAME); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isName()); + assertEquals("value", properties.get("name").asName()); + } + + @Test + public void testGetMultiNameProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.NAMES).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.NAMES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiName()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiName())); + } + + @Test + public void testPathProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.PATH).when(property).getType(); + doReturn("value").when(property).getValue(Type.PATH); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isPath()); + assertEquals("value", properties.get("name").asPath()); + } + + @Test + public void testGetMultiPathProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.PATHS).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.PATHS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiPath()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiPath())); + } + + @Test + public void testReferenceProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.REFERENCE).when(property).getType(); + doReturn("value").when(property).getValue(Type.REFERENCE); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isReference()); + assertEquals("value", properties.get("name").asReference()); + } + + @Test + public void testGetMultiReferenceProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.REFERENCES).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.REFERENCES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiReference()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiReference())); + } + + @Test + public void testTextProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.STRING).when(property).getType(); + doReturn("value").when(property).getValue(Type.STRING); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isText()); + assertEquals("value", properties.get("name").asText()); + } + + @Test + public void testGetMultiTextProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.STRINGS).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.STRINGS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiText()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiText())); + } + + @Test + public void testUriProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.URI).when(property).getType(); + doReturn("value").when(property).getValue(Type.URI); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isUri()); + assertEquals("value", properties.get("name").asUri()); + } + + @Test + public void testGetMultiUriProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.URIS).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.URIS); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiUri()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiUri())); + } + + @Test + public void testWeakReferenceProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.WEAKREFERENCE).when(property).getType(); + doReturn("value").when(property).getValue(Type.WEAKREFERENCE); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isWeakReference()); + assertEquals("value", properties.get("name").asWeakReference()); + } + + @Test + public void testGetMultiWeakReferenceProperty() { + PropertyState property = mock(PropertyState.class); + doReturn("name").when(property).getName(); + doReturn(Type.WEAKREFERENCES).when(property).getType(); + doReturn(singletonList("value")).when(property).getValue(Type.WEAKREFERENCES); + + Tree tree = mock(Tree.class); + doReturn(singletonList(property)).when(tree).getProperties(); + + Map<String, RemoteValue> properties = createTree(tree).getProperties(); + + assertTrue(properties.containsKey("name")); + assertTrue(properties.get("name").isMultiWeakReference()); + assertEquals("value", getOnlyElement(properties.get("name").asMultiWeakReference())); + } + + @Test + public void testFilterPropertyIn() { + PropertyState fooProperty = mock(PropertyState.class); + doReturn("foo").when(fooProperty).getName(); + doReturn(Type.BOOLEAN).when(fooProperty).getType(); + doReturn(true).when(fooProperty).getValue(Type.BOOLEAN); + + PropertyState barProperty = mock(PropertyState.class); + doReturn("bar").when(barProperty).getName(); + doReturn(Type.BOOLEAN).when(barProperty).getType(); + doReturn(true).when(barProperty).getValue(Type.BOOLEAN); + + Tree tree = mock(Tree.class); + doReturn(asList(fooProperty, barProperty)).when(tree).getProperties(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public Set<String> getPropertyFilters() { + return newHashSet("foo"); + } + + }); + + Map<String, RemoteValue> properties = remoteTree.getProperties(); + + assertTrue(properties.containsKey("foo")); + assertFalse(properties.containsKey("bar")); + } + + @Test + public void testFilterPropertyOut() { + PropertyState fooProperty = mock(PropertyState.class); + doReturn("foo").when(fooProperty).getName(); + doReturn(Type.BOOLEAN).when(fooProperty).getType(); + doReturn(true).when(fooProperty).getValue(Type.BOOLEAN); + + PropertyState barProperty = mock(PropertyState.class); + doReturn("bar").when(barProperty).getName(); + doReturn(Type.BOOLEAN).when(barProperty).getType(); + doReturn(true).when(barProperty).getValue(Type.BOOLEAN); + + Tree tree = mock(Tree.class); + doReturn(asList(fooProperty, barProperty)).when(tree).getProperties(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public Set<String> getPropertyFilters() { + return newHashSet("-bar"); + } + + }); + + Map<String, RemoteValue> properties = remoteTree.getProperties(); + + assertTrue(properties.containsKey("foo")); + assertFalse(properties.containsKey("bar")); + } + + @Test + public void testGetChildrenMaxDepth() { + Tree child = mock(Tree.class); + doReturn("child").when(child).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(child)).when(tree).getChildren(); + + Map<String, RemoteTree> children = createTree(tree).getChildren(); + + assertTrue(children.containsKey("child")); + assertNull(children.get("child")); + } + + @Test + public void testGetChildren() { + Tree child = mock(Tree.class); + doReturn("child").when(child).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(child)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getDepth() { + return 1; + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("child")); + assertNotNull(children.get("child")); + } + + @Test + public void testGetChildrenWithStart() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenStart() { + return 1; + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertFalse(children.containsKey("foo")); + assertTrue(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithNegativeStart() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenStart() { + return -1; + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertTrue(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithStartTooBig() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenStart() { + return 2; + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertFalse(children.containsKey("foo")); + assertFalse(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithCount() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenCount() { + return 1; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertFalse(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithNegativeCount() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenCount() { + return -1; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertTrue(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithZeroCount() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenCount() { + return 0; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertFalse(children.containsKey("foo")); + assertFalse(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithCountTooBig() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenCount() { + return 3; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertTrue(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithSlicing() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree baz = mock(Tree.class); + doReturn("baz").when(baz).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar, baz)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public int getChildrenStart() { + return 1; + } + + @Override + public int getChildrenCount() { + return 1; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertFalse(children.containsKey("foo")); + assertTrue(children.containsKey("bar")); + assertFalse(children.containsKey("baz")); + } + + @Test + public void testGetChildrenWithIncludeFilters() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public Set<String> getNodeFilters() { + return newHashSet("foo"); + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertFalse(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithExcludeFilters() { + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(foo, bar)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public Set<String> getNodeFilters() { + return newHashSet("-bar"); + } + + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertTrue(children.containsKey("foo")); + assertFalse(children.containsKey("bar")); + } + + @Test + public void testGetChildrenWithSlicingAndFiltering() { + Tree bar = mock(Tree.class); + doReturn("bar").when(bar).getName(); + + Tree foo = mock(Tree.class); + doReturn("foo").when(foo).getName(); + + Tree baz = mock(Tree.class); + doReturn("baz").when(baz).getName(); + + Tree tree = mock(Tree.class); + doReturn(asList(bar, foo, baz)).when(tree).getChildren(); + + ContentRemoteTree remoteTree = createTree(tree, new RemoteTreeFilters() { + + @Override + public Set<String> getNodeFilters() { + return Sets.newHashSet("ba*"); + } + + @Override + public int getChildrenStart() { + return 1; + } + + @Override + public int getChildrenCount() { + return 1; + } + }); + + Map<String, RemoteTree> children = remoteTree.getChildren(); + + assertFalse(children.containsKey("bar")); + assertFalse(children.containsKey("foo")); + assertFalse(children.containsKey("baz")); + } + +}
Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/CopyContentRemoteOperationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/CopyContentRemoteOperationTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/CopyContentRemoteOperationTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/CopyContentRemoteOperationTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,105 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import org.apache.jackrabbit.oak.api.Root; +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.remote.RemoteCommitException; +import org.junit.Test; + +import static java.util.Collections.emptyList; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class CopyContentRemoteOperationTest { + + CopyContentRemoteOperation createOperation(String source, String target) { + return new CopyContentRemoteOperation(source, target); + } + + @Test + public void testCopy() throws Exception { + Tree parent = mock(Tree.class); + doReturn(true).when(parent).exists(); + + Tree target = mock(Tree.class); + doReturn(false).when(target).exists(); + doReturn(parent).when(target).getParent(); + doReturn("target").when(target).getName(); + + Tree source = mock(Tree.class); + doReturn(true).when(source).exists(); + doReturn(emptyList()).when(source).getProperties(); + doReturn(emptyList()).when(source).getChildren(); + + Root root = mock(Root.class); + doReturn(source).when(root).getTree("/source"); + doReturn(target).when(root).getTree("/target"); + + createOperation("/source", "/target").apply(root); + + verify(parent).addChild("target"); + } + + @Test(expected = RemoteCommitException.class) + public void testCopyWithNonExistingSource() throws Exception { + Tree source = mock(Tree.class); + doReturn(false).when(source).exists(); + + Root root = mock(Root.class); + doReturn(source).when(root).getTree("/source"); + + createOperation("/source", "/target").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testCopyWithExistingTarget() throws Exception { + Tree target = mock(Tree.class); + doReturn(true).when(target).exists(); + + Tree source = mock(Tree.class); + doReturn(true).when(source).exists(); + + Root root = mock(Root.class); + doReturn(source).when(root).getTree("/source"); + doReturn(target).when(root).getTree("/target"); + + createOperation("/source", "/target").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testCopyWithNonExistingTargetParent() throws Exception { + Tree parent = mock(Tree.class); + doReturn(false).when(parent).exists(); + + Tree target = mock(Tree.class); + doReturn(false).when(target).exists(); + doReturn(parent).when(target).getParent(); + + Tree source = mock(Tree.class); + doReturn(true).when(source).exists(); + + Root root = mock(Root.class); + doReturn(source).when(root).getTree("/source"); + doReturn(target).when(root).getTree("/target"); + + createOperation("/source", "/target").apply(root); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/MoveContentRemoteOperationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/MoveContentRemoteOperationTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/MoveContentRemoteOperationTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/MoveContentRemoteOperationTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,49 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import org.apache.jackrabbit.oak.api.Root; +import org.apache.jackrabbit.oak.remote.RemoteCommitException; +import org.junit.Test; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class MoveContentRemoteOperationTest { + + MoveContentRemoteOperation createOperation(String source, String target) { + return new MoveContentRemoteOperation(source, target); + } + + @Test + public void testMove() throws Exception { + Root root = mock(Root.class); + doReturn(true).when(root).move("/source", "/target"); + + createOperation("/source", "/target").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testMoveUnsuccessful() throws Exception { + Root root = mock(Root.class); + doReturn(false).when(root).move("/source", "/target"); + + createOperation("/source", "/target").apply(root); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/RemoveContentRemoteOperationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/RemoveContentRemoteOperationTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/RemoveContentRemoteOperationTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/RemoveContentRemoteOperationTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,69 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import org.apache.jackrabbit.oak.api.Root; +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.remote.RemoteCommitException; +import org.junit.Test; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; + +public class RemoveContentRemoteOperationTest { + + private RemoveContentRemoteOperation createOperation(String path) { + return new RemoveContentRemoteOperation(path); + } + + @Test + public void testRemove() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + doReturn(true).when(tree).remove(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testRemoveWithNonExistingTree() throws Exception { + Tree tree = mock(Tree.class); + doReturn(false).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testRemoveWithNonRemovableTree() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + doReturn(false).when(tree).remove(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test").apply(root); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/SetContentRemoteOperationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/SetContentRemoteOperationTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/SetContentRemoteOperationTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/SetContentRemoteOperationTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,535 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import com.google.common.base.Predicate; +import org.apache.jackrabbit.oak.api.Blob; +import org.apache.jackrabbit.oak.api.Root; +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.api.Type; +import org.apache.jackrabbit.oak.remote.RemoteCommitException; +import org.apache.jackrabbit.oak.remote.RemoteValue; +import org.apache.jackrabbit.oak.remote.RemoteValue.Supplier; +import org.apache.jackrabbit.util.ISO8601; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.junit.Test; + +import java.io.InputStream; +import java.math.BigDecimal; +import java.util.Calendar; + +import static com.google.common.collect.Iterables.any; +import static java.util.Collections.singletonList; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toBinary; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toBinaryId; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toBoolean; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toDate; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toDecimal; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toDouble; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toLong; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiBinary; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiBinaryId; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiBoolean; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiDate; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiDecimal; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiDouble; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiLong; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiName; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiPath; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiReference; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiText; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiUri; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toMultiWeakReference; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toName; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toPath; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toReference; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toText; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toUri; +import static org.apache.jackrabbit.oak.remote.RemoteValue.toWeakReference; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class SetContentRemoteOperationTest { + + private SetContentRemoteOperation createOperation(String path, String name, RemoteValue value) { + return new SetContentRemoteOperation(mock(ContentRemoteBinaries.class), path, name, value); + } + + private SetContentRemoteOperation createOperation(ContentRemoteBinaries binaries, String path, String name, RemoteValue value) { + return new SetContentRemoteOperation(binaries, path, name, value); + } + + private <T> Matcher<Iterable<T>> isIterableReferencing(final T value) { + return new BaseMatcher<Iterable<T>>() { + + @Override + public boolean matches(Object item) { + Iterable iterable = null; + + if (item instanceof Iterable) { + iterable = (Iterable) item; + } + + if (iterable == null) { + return false; + } + + return any(iterable, new Predicate() { + + @Override + public boolean apply(Object element) { + return element == value; + } + + }); + } + + @Override + public void describeTo(Description description) { + description.appendText("an iterable referencing ").appendValue(value); + } + + }; + } + + private <T> Matcher<Iterable<T>> isIterableContaining(final T value) { + return new BaseMatcher<Iterable<T>>() { + + @Override + public boolean matches(Object item) { + Iterable iterable = null; + + if (item instanceof Iterable) { + iterable = (Iterable) item; + } + + if (iterable == null) { + return false; + } + + return any(iterable, new Predicate() { + + @Override + public boolean apply(Object element) { + if (element == null && value == null) { + return true; + } + + if (element != null && value != null) { + return element.equals(value); + } + + return false; + } + + }); + } + + @Override + public void describeTo(Description description) { + description.appendText("an iterable containing ").appendValue(value); + } + + }; + } + + @Test(expected = RemoteCommitException.class) + public void testSetWithNonExistingTree() throws Exception { + Tree tree = mock(Tree.class); + doReturn(false).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", mock(RemoteValue.class)).apply(root); + } + + @Test + public void testSetBinaryProperty() throws Exception { + Blob blob = mock(Blob.class); + + InputStream stream = mock(InputStream.class); + + Supplier<InputStream> supplier = mock(Supplier.class); + doReturn(stream).when(supplier).get(); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + doReturn(blob).when(root).createBlob(stream); + + createOperation("/test", "name", toBinary(supplier)).apply(root); + + verify(tree).setProperty("name", blob, Type.BINARY); + } + + @Test + public void testSetMultiBinaryProperty() throws Exception { + Blob blob = mock(Blob.class); + + InputStream stream = mock(InputStream.class); + + Supplier<InputStream> supplier = mock(Supplier.class); + doReturn(stream).when(supplier).get(); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + doReturn(blob).when(root).createBlob(stream); + + createOperation("/test", "name", toMultiBinary(singletonList(supplier))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableReferencing(blob)), eq(Type.BINARIES)); + } + + @Test + public void testSetBinaryIdProperty() throws Exception { + Blob blob = mock(Blob.class); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + ContentRemoteBinaries binaries = mock(ContentRemoteBinaries.class); + doReturn(blob).when(binaries).get("id"); + + createOperation(binaries, "/test", "name", toBinaryId("id")).apply(root); + + verify(tree).setProperty("name", blob, Type.BINARY); + } + + @Test + public void setMultiBinaryIdProperty() throws Exception { + Blob blob = mock(Blob.class); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + ContentRemoteBinaries binaries = mock(ContentRemoteBinaries.class); + doReturn(blob).when(binaries).get("id"); + + createOperation(binaries, "/test", "name", toMultiBinaryId(singletonList("id"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableReferencing(blob)), eq(Type.BINARIES)); + } + + @Test + public void testSetBooleanProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toBoolean(true)).apply(root); + + verify(tree).setProperty("name", true, Type.BOOLEAN); + } + + @Test + public void testSetMultiBooleanProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiBoolean(singletonList(true))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining(true)), eq(Type.BOOLEANS)); + } + + @Test + public void testSetDateProperty() throws Exception { + Calendar calendar = Calendar.getInstance(); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toDate(calendar.getTimeInMillis())).apply(root); + + verify(tree).setProperty("name", ISO8601.format(calendar), Type.DATE); + } + + @Test + public void testSetMultiDateProperty() throws Exception { + Calendar calendar = Calendar.getInstance(); + + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiDate(singletonList(calendar.getTimeInMillis()))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining(ISO8601.format(calendar))), eq(Type.DATES)); + } + + @Test + public void testSetDecimalProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toDecimal(BigDecimal.ONE)).apply(root); + + verify(tree).setProperty("name", BigDecimal.ONE, Type.DECIMAL); + } + + @Test + public void testSetMultiDecimalProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiDecimal(singletonList(BigDecimal.ONE))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining(BigDecimal.ONE)), eq(Type.DECIMALS)); + } + + @Test + public void testSetDoubleProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toDouble(4.2)).apply(root); + + verify(tree).setProperty("name", 4.2, Type.DOUBLE); + } + + @Test + public void testSetMultiDoubleProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiDouble(singletonList(4.2))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining(4.2)), eq(Type.DOUBLES)); + } + + @Test + public void testSetLongProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toLong(42L)).apply(root); + + verify(tree).setProperty("name", 42L, Type.LONG); + } + + @Test + public void testSetMultiLongProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiLong(singletonList(42L))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining(42L)), eq(Type.LONGS)); + } + + @Test + public void testSetNameProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toName("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.NAME); + } + + @Test + public void testSetMultiNameProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiName(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.NAMES)); + } + + @Test + public void testSetPathProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toPath("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.PATH); + } + + @Test + public void testSetMultiPathProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiPath(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.PATHS)); + } + + @Test + public void testSetReferenceProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toReference("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.REFERENCE); + } + + @Test + public void testSetMultiReferenceProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiReference(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.REFERENCES)); + } + + @Test + public void testSetStringProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toText("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.STRING); + } + + @Test + public void testSetMultiStringProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiText(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.STRINGS)); + } + + @Test + public void testSetUriProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toUri("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.URI); + } + + @Test + public void testSetMultiUriProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiUri(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.URIS)); + } + + @Test + public void testSetWeakReferenceProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toWeakReference("value")).apply(root); + + verify(tree).setProperty("name", "value", Type.WEAKREFERENCE); + } + + @Test + public void testSetMultiWeakReferenceProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name", toMultiWeakReference(singletonList("value"))).apply(root); + + verify(tree).setProperty(eq("name"), argThat(isIterableContaining("value")), eq(Type.WEAKREFERENCES)); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/UnsetContentRemoteOperationTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/UnsetContentRemoteOperationTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/UnsetContentRemoteOperationTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/content/UnsetContentRemoteOperationTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,72 @@ +/* + * 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.jackrabbit.oak.remote.content; + +import org.apache.jackrabbit.oak.api.Root; +import org.apache.jackrabbit.oak.api.Tree; +import org.apache.jackrabbit.oak.remote.RemoteCommitException; +import org.junit.Test; + +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +public class UnsetContentRemoteOperationTest { + + private UnsetContentRemoteOperation createOperation(String path, String name) { + return new UnsetContentRemoteOperation(path, name); + } + + @Test + public void testUnset() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + doReturn(true).when(tree).hasProperty("name"); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name").apply(root); + + verify(tree).removeProperty("name"); + } + + @Test(expected = RemoteCommitException.class) + public void testUnsetWithNonExistingTree() throws Exception { + Tree tree = mock(Tree.class); + doReturn(false).when(tree).exists(); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name").apply(root); + } + + @Test(expected = RemoteCommitException.class) + public void testUnsetWithNonExistingProperty() throws Exception { + Tree tree = mock(Tree.class); + doReturn(true).when(tree).exists(); + doReturn(false).when(tree).hasProperty("name"); + + Root root = mock(Root.class); + doReturn(tree).when(root).getTree("/test"); + + createOperation("/test", "name").apply(root); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FilterTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FilterTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FilterTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FilterTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,57 @@ +/* + * 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.jackrabbit.oak.remote.filter; + +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FilterTest { + + @Test + public void testExact() { + Filter filter = new Filter("name"); + + assertTrue(filter.matches("name")); + assertFalse(filter.matches("nam")); + assertFalse(filter.matches("named")); + } + + @Test + public void testWildcard() { + Filter filter = new Filter("na*e"); + + assertTrue(filter.matches("nae")); + assertTrue(filter.matches("name")); + assertTrue(filter.matches("namme")); + assertFalse(filter.matches("nam")); + assertFalse(filter.matches("named")); + } + + @Test + public void testEscapedWildcard() { + Filter filter = new Filter("na\\*e"); + + assertTrue(filter.matches("na*e")); + assertFalse(filter.matches("nae")); + assertFalse(filter.matches("name")); + assertFalse(filter.matches("namme")); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FiltersTest.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FiltersTest.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FiltersTest.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/filter/FiltersTest.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,86 @@ +/* + * 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.jackrabbit.oak.remote.filter; + +import com.google.common.collect.Sets; +import org.junit.Test; + +import java.util.HashSet; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class FiltersTest { + + @Test(expected = IllegalArgumentException.class) + public void testNullFilters() { + new Filters(null); + } + + @Test(expected = IllegalArgumentException.class) + public void testNullFilter() { + new Filters(Sets.newHashSet((String) null)); + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyIncludeFilter() { + new Filters(Sets.newHashSet("")); + } + + @Test(expected = IllegalArgumentException.class) + public void testEmptyExcludeFilter() { + new Filters(Sets.newHashSet("-")); + } + + @Test + public void testEmptyFilters() { + Filters filters = new Filters(new HashSet<String>()); + + assertTrue(filters.matches("foo")); + assertTrue(filters.matches("bar")); + assertTrue(filters.matches("baz")); + } + + @Test + public void testIncludeFilter() { + Filters filters = new Filters(Sets.newHashSet("ba*")); + + assertFalse(filters.matches("foo")); + assertTrue(filters.matches("bar")); + assertTrue(filters.matches("baz")); + } + + @Test + public void testExcludeFilter() { + Filters filters = new Filters(Sets.newHashSet("-foo")); + + assertFalse(filters.matches("foo")); + assertTrue(filters.matches("bar")); + assertTrue(filters.matches("baz")); + } + + @Test + public void testIncludeExcludeFilters() { + Filters filters = new Filters(Sets.newHashSet("ba*", "-baz")); + + assertFalse(filters.matches("foo")); + assertTrue(filters.matches("bar")); + assertFalse(filters.matches("baz")); + } + +} Added: jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/http/handler/RemoteServer.java URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/http/handler/RemoteServer.java?rev=1684861&view=auto ============================================================================== --- jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/http/handler/RemoteServer.java (added) +++ jackrabbit/oak/trunk/oak-remote/src/test/java/org/apache/jackrabbit/oak/remote/http/handler/RemoteServer.java Thu Jun 11 12:09:15 2015 @@ -0,0 +1,65 @@ +/* + * 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.jackrabbit.oak.remote.http.handler; + +import org.apache.jackrabbit.oak.remote.RemoteRepository; +import org.apache.jackrabbit.oak.remote.http.RemoteServlet; +import org.eclipse.jetty.server.Handler; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.servlet.ServletHandler; +import org.eclipse.jetty.servlet.ServletHolder; + +import java.net.InetSocketAddress; + +public class RemoteServer { + + private final Server server; + + public RemoteServer(RemoteRepository repository, String host, int port) { + this.server = createServer(repository, new InetSocketAddress(host, port)); + } + + private Server createServer(RemoteRepository repository, InetSocketAddress address) { + Server server = new Server(address); + + server.setHandler(createHandler(repository)); + + return server; + } + + private Handler createHandler(RemoteRepository repository) { + ServletHandler handler = new ServletHandler(); + + handler.addServletWithMapping(new ServletHolder(new RemoteServlet(repository)), "/*"); + + return handler; + } + + public void start() throws Exception { + server.start(); + } + + public void stop() throws Exception { + server.stop(); + } + + public void join() throws Exception { + server.join(); + } + +} \ No newline at end of file
