From c70fcc07dd643654f8c436c5ea4ff8d81bf51603 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Thu, 24 Nov 2016 08:47:31 +0100 Subject: HONEYCOMB-289 - Type-aware support for DumpCacheManager Standard cache key factory made type-aware Added checking for type of returned data from cache Change-Id: Ie4d31a9d2b0d25c4b2f4ea66be98060f449007b6 Signed-off-by: Jan Srnicek --- .../util/read/cache/DumpCacheManagerTest.java | 35 ++++- .../read/cache/IdentifierCacheKeyFactoryTest.java | 144 -------------------- .../TypeAwareIdentifierCacheKeyFactoryTest.java | 146 +++++++++++++++++++++ 3 files changed, 176 insertions(+), 149 deletions(-) delete mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactoryTest.java create mode 100644 infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactoryTest.java (limited to 'infra/translate-utils/src/test/java/io') diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManagerTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManagerTest.java index 3639439ce..74aef67d0 100644 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManagerTest.java +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManagerTest.java @@ -18,6 +18,7 @@ package io.fd.honeycomb.translate.util.read.cache; import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; import com.google.common.base.Optional; @@ -35,13 +36,9 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; public class DumpCacheManagerTest { - private interface DataObj extends DataObject {} - @Mock private EntityDumpExecutor executor; - private InstanceIdentifier identifier; - private DumpCacheManager managerPositive; private DumpCacheManager managerPositiveWithPostProcessing; private DumpCacheManager managerNegative; @@ -54,23 +51,26 @@ public class DumpCacheManagerTest { managerPositive = new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(executor) + .acceptOnly(IpDetailsReplyDump.class) .build(); managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(executor) + .acceptOnly(IpDetailsReplyDump.class) .withPostProcessingFunction(createPostProcessor()) .build(); managerNegative = new DumpCacheManager.DumpCacheManagerBuilder() .withExecutor(executor) + .acceptOnly(IpDetailsReplyDump.class) .build(); cache = new ModificationCache(); identifier = InstanceIdentifier.create(DataObj.class); //manager uses this implementation by default, so it can be used to test behaviour - cacheKeyFactory = new IdentifierCacheKeyFactory(); + cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(IpDetailsReplyDump.class); } @@ -131,6 +131,28 @@ public class DumpCacheManagerTest { assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex); } + @Test + public void testSameKeyDifferentTypes() throws ReadFailedException { + final DumpCacheManager stringManager = + new DumpCacheManager.DumpCacheManagerBuilder() + .withExecutor((InstanceIdentifier, Void) -> "value") + .acceptOnly(String.class) + .build(); + + final DumpCacheManager intManager = new DumpCacheManager.DumpCacheManagerBuilder() + .acceptOnly(Integer.class) + .withExecutor((InstanceIdentifier, Void) -> 3).build(); + + final Optional stringDump = stringManager.getDump(identifier, cache, NO_PARAMS); + final Optional integerDump = intManager.getDump(identifier, cache, NO_PARAMS); + + assertTrue(stringDump.isPresent()); + assertTrue(integerDump.isPresent()); + assertEquals("value", stringDump.get()); + assertEquals(3, integerDump.get().intValue()); + + } + private EntityDumpPostProcessingFunction createPostProcessor() { return ipDetailsReplyDump -> { IpDetailsReplyDump modified = new IpDetailsReplyDump(); @@ -146,6 +168,9 @@ public class DumpCacheManagerTest { }; } + private interface DataObj extends DataObject { + } + private static final class IpDetailsReplyDump { List ipDetails = new ArrayList<>(); diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactoryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactoryTest.java deleted file mode 100644 index e7eae552c..000000000 --- a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactoryTest.java +++ /dev/null @@ -1,144 +0,0 @@ -/* - * Copyright (c) 2016 Cisco and/or its affiliates. - * - * Licensed 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 io.fd.honeycomb.translate.util.read.cache; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import com.google.common.collect.ImmutableSet; -import org.junit.Before; -import org.junit.Test; -import org.opendaylight.yangtools.yang.binding.ChildOf; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.Identifiable; -import org.opendaylight.yangtools.yang.binding.Identifier; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -public class IdentifierCacheKeyFactoryTest { - - private interface SuperDataObject extends DataObject { - } - - private interface DataObjectParent extends DataObject, ChildOf, Identifiable { - } - - private class DataObjectParentKey implements Identifier { - } - - private interface DataObjectChild extends DataObject, ChildOf, Identifiable { - } - - private class DataObjectChildKey implements Identifier { - } - - private DataObjectParentKey parentKey; - private DataObjectChildKey childKey; - private InstanceIdentifier identifierBothKeyed; - private InstanceIdentifier identifierOneMissing; - private InstanceIdentifier identifierNoneKeyed; - - private IdentifierCacheKeyFactory simpleKeyFactory; - private IdentifierCacheKeyFactory complexKeyFactory; - - @Before - public void init() { - parentKey = new DataObjectParentKey(); - childKey = new DataObjectChildKey(); - identifierBothKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class, parentKey) - .child(DataObjectChild.class, childKey); - identifierOneMissing = InstanceIdentifier.create(DataObjectChild.class); - identifierNoneKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class) - .child(DataObjectChild.class); - - complexKeyFactory = new IdentifierCacheKeyFactory(ImmutableSet.of(DataObjectParent.class)); - simpleKeyFactory = new IdentifierCacheKeyFactory(); - } - - @Test - public void createKeyBothKeyedComplex() { - final String key = complexKeyFactory.createKey(identifierBothKeyed); - - /** - * Should pass because key constructed in this case should look like : - * additional_scope_type[additional_scope_type_key]|cached_type - * */ - verifyComplexKey(key); - } - - /** - * Should fail because provided identifier does'nt contain all requested key parts - */ - @Test(expected = IllegalArgumentException.class) - public void createKeyOneMissingComplex() { - complexKeyFactory.createKey(identifierOneMissing); - } - - /** - * Should fail because request paths are not keyed - */ - @Test(expected = IllegalArgumentException.class) - public void createKeyNoneKeyedComplex() { - complexKeyFactory.createKey(identifierNoneKeyed); - } - - @Test - public void createKeyBothKeyedSimple() { - final String key = simpleKeyFactory.createKey(identifierBothKeyed); - - /** - * Should pass because key constructed in this case should look like : cached_type - * */ - verifySimpleKey(key); - } - - @Test - public void createKeyOneMissingSimple() { - final String key = simpleKeyFactory.createKey(identifierOneMissing); - /** - * Should pass because key constructed in this case should look like : cached_type - * */ - verifySimpleKey(key); - } - - /** - * Should fail because request paths are not keyed - */ - @Test - public void createKeyNoneKeyedSimple() { - final String key = simpleKeyFactory.createKey(identifierNoneKeyed); - /** - * Should pass because key constructed in this case should look like : cached_type - * */ - verifySimpleKey(key); - } - - private void verifyComplexKey(final String key) { - assertTrue(key.contains(DataObjectParent.class.getTypeName())); - assertTrue(key.contains(parentKey.toString())); - assertTrue(key.contains(DataObjectChild.class.getTypeName())); - assertFalse(key.contains(childKey.toString())); - assertFalse(key.contains(SuperDataObject.class.getTypeName())); - } - - private void verifySimpleKey(final String key) { - assertFalse(key.contains(DataObjectParent.class.getTypeName())); - assertFalse(key.contains(parentKey.toString())); - assertTrue(key.contains(DataObjectChild.class.getTypeName())); - assertFalse(key.contains(childKey.toString())); - assertFalse(key.contains(SuperDataObject.class.getTypeName())); - } -} \ No newline at end of file diff --git a/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactoryTest.java b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactoryTest.java new file mode 100644 index 000000000..305fba143 --- /dev/null +++ b/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactoryTest.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed 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 io.fd.honeycomb.translate.util.read.cache; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.ImmutableSet; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.yangtools.yang.binding.ChildOf; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.Identifier; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +public class TypeAwareIdentifierCacheKeyFactoryTest { + + private DataObjectParentKey parentKey; + private DataObjectChildKey childKey; + private InstanceIdentifier identifierBothKeyed; + private InstanceIdentifier identifierOneMissing; + private InstanceIdentifier identifierNoneKeyed; + private TypeAwareIdentifierCacheKeyFactory simpleKeyFactory; + private TypeAwareIdentifierCacheKeyFactory complexKeyFactory; + + @Before + public void init() { + parentKey = new DataObjectParentKey(); + childKey = new DataObjectChildKey(); + identifierBothKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class, parentKey) + .child(DataObjectChild.class, childKey); + identifierOneMissing = InstanceIdentifier.create(DataObjectChild.class); + identifierNoneKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class) + .child(DataObjectChild.class); + + complexKeyFactory = + new TypeAwareIdentifierCacheKeyFactory(String.class, ImmutableSet.of(DataObjectParent.class)); + simpleKeyFactory = new TypeAwareIdentifierCacheKeyFactory(String.class); + } + + @Test + public void createKeyBothKeyedComplex() { + final String key = complexKeyFactory.createKey(identifierBothKeyed); + + /** + * Should pass because key constructed in this case should look like : + * additional_scope_type[additional_scope_type_key]|cached_type + * */ + verifyComplexKey(key); + } + + /** + * Should fail because provided identifier does'nt contain all requested key parts + */ + @Test(expected = IllegalArgumentException.class) + public void createKeyOneMissingComplex() { + complexKeyFactory.createKey(identifierOneMissing); + } + + /** + * Should fail because request paths are not keyed + */ + @Test(expected = IllegalArgumentException.class) + public void createKeyNoneKeyedComplex() { + complexKeyFactory.createKey(identifierNoneKeyed); + } + + @Test + public void createKeyBothKeyedSimple() { + final String key = simpleKeyFactory.createKey(identifierBothKeyed); + + /** + * Should pass because key constructed in this case should look like : cached_type + * */ + verifySimpleKey(key); + } + + @Test + public void createKeyOneMissingSimple() { + final String key = simpleKeyFactory.createKey(identifierOneMissing); + /** + * Should pass because key constructed in this case should look like : cached_type + * */ + verifySimpleKey(key); + } + + /** + * Should fail because request paths are not keyed + */ + @Test + public void createKeyNoneKeyedSimple() { + final String key = simpleKeyFactory.createKey(identifierNoneKeyed); + /** + * Should pass because key constructed in this case should look like : cached_type + * */ + verifySimpleKey(key); + } + + private void verifyComplexKey(final String key) { + assertTrue(key.contains(String.class.getTypeName())); + assertTrue(key.contains(DataObjectParent.class.getTypeName())); + assertTrue(key.contains(parentKey.toString())); + assertTrue(key.contains(DataObjectChild.class.getTypeName())); + assertFalse(key.contains(childKey.toString())); + assertFalse(key.contains(SuperDataObject.class.getTypeName())); + } + + private void verifySimpleKey(final String key) { + assertTrue(key.contains(String.class.getTypeName())); + assertFalse(key.contains(DataObjectParent.class.getTypeName())); + assertFalse(key.contains(parentKey.toString())); + assertTrue(key.contains(DataObjectChild.class.getTypeName())); + assertFalse(key.contains(childKey.toString())); + assertFalse(key.contains(SuperDataObject.class.getTypeName())); + } + + private interface SuperDataObject extends DataObject { + } + + private interface DataObjectParent extends DataObject, ChildOf, Identifiable { + } + + private interface DataObjectChild extends DataObject, ChildOf, Identifiable { + } + + private class DataObjectParentKey implements Identifier { + } + + private class DataObjectChildKey implements Identifier { + } +} \ No newline at end of file -- cgit 1.2.3-korg