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 --- .../translate/util/read/cache/CacheKeyFactory.java | 7 + .../util/read/cache/DumpCacheManager.java | 40 +++++- .../util/read/cache/IdentifierCacheKeyFactory.java | 122 ----------------- .../cache/TypeAwareIdentifierCacheKeyFactory.java | 132 +++++++++++++++++++ .../util/read/cache/DumpCacheManagerTest.java | 35 ++++- .../read/cache/IdentifierCacheKeyFactoryTest.java | 144 -------------------- .../TypeAwareIdentifierCacheKeyFactoryTest.java | 146 +++++++++++++++++++++ 7 files changed, 348 insertions(+), 278 deletions(-) delete mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactory.java create mode 100644 infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactory.java 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 diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/CacheKeyFactory.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/CacheKeyFactory.java index 1b444ba3c..bf4659e89 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/CacheKeyFactory.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/CacheKeyFactory.java @@ -27,5 +27,12 @@ public interface CacheKeyFactory { /** * Construct key accordingly to provided {@code InstanceIdentifier} */ + @Nonnull String createKey(@Nonnull final InstanceIdentifier actualContextIdentifier); + + /** + * Returns type of data, for which is this factory creating keys + */ + @Nonnull + Class getCachedDataType(); } diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java index f1b265dec..adcd32e0c 100644 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/DumpCacheManager.java @@ -17,6 +17,8 @@ package io.fd.honeycomb.translate.util.read.cache; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; +import static java.util.Objects.nonNull; import com.google.common.base.Optional; import io.fd.honeycomb.translate.ModificationCache; @@ -41,11 +43,13 @@ public final class DumpCacheManager { private final EntityDumpExecutor dumpExecutor; private final EntityDumpPostProcessingFunction postProcessor; private final CacheKeyFactory cacheKeyFactory; + private final Class acceptOnly; private DumpCacheManager(DumpCacheManagerBuilder builder) { this.dumpExecutor = builder.dumpExecutor; this.postProcessor = builder.postProcessingFunction; this.cacheKeyFactory = builder.cacheKeyFactory; + this.acceptOnly = builder.acceptOnly; } /** @@ -79,6 +83,12 @@ public final class DumpCacheManager { cache.put(entityKey, dump); return Optional.of(dump); } else { + // if specified, check whether data returned from cache can be used as result of this dump manager + // used as a secondary check if cache does not have any data of different type stored under the same key + checkState(acceptOnly.isInstance(dump), + "This dump manager accepts only %s as data, but %s was returned from cache", + acceptOnly, dump.getClass()); + LOG.debug("Cached instance of dump was found for KEY[{}]", entityKey); return Optional.of(dump); } @@ -86,18 +96,14 @@ public final class DumpCacheManager { public static final class DumpCacheManagerBuilder { - private static final CacheKeyFactory DEFAULT_CACHE_KEY_FACTORY_INSTANCE = new IdentifierCacheKeyFactory(); - private EntityDumpExecutor dumpExecutor; private EntityDumpPostProcessingFunction postProcessingFunction; private CacheKeyFactory cacheKeyFactory; + private Class acceptOnly; public DumpCacheManagerBuilder() { // for cases when user does not set specific post-processor postProcessingFunction = new NoopDumpPostProcessingFunction(); - - //use no additional scopes version by default - cacheKeyFactory = DEFAULT_CACHE_KEY_FACTORY_INSTANCE; } public DumpCacheManagerBuilder withExecutor(@Nonnull final EntityDumpExecutor executor) { @@ -111,17 +117,37 @@ public final class DumpCacheManager { return this; } + /** + * Key providing unique(type-aware) keys. + */ public DumpCacheManagerBuilder withCacheKeyFactory(@Nonnull final CacheKeyFactory cacheKeyFactory) { this.cacheKeyFactory = cacheKeyFactory; return this; } + /** + * If modification returns object of different type that this, throw exception to prevent processing data + * of different type. + */ + public DumpCacheManagerBuilder acceptOnly(@Nonnull final Class acceptOnly) { + this.acceptOnly = acceptOnly; + return this; + } + public DumpCacheManager build() { checkNotNull(dumpExecutor, "Dump executor cannot be null"); checkNotNull(postProcessingFunction, "Dump post-processor cannot be null cannot be null, default implementation is used when not set explicitly"); - checkNotNull(cacheKeyFactory, - "Cache key factory cannot be null, default non-extended implementation is used when not set explicitly"); + + if (acceptOnly != null) { + cacheKeyFactory = new TypeAwareIdentifierCacheKeyFactory(acceptOnly); + } else if (cacheKeyFactory != null) { + acceptOnly = cacheKeyFactory.getCachedDataType(); + } else { + throw new IllegalStateException( + "Invalid combination - either acceptOnly type must be defined[defined=" + nonNull(acceptOnly) + + "], or type-aware cache key factory[defined=" + nonNull(cacheKeyFactory) + "]"); + } return new DumpCacheManager<>(this); } diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactory.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactory.java deleted file mode 100644 index 51f47e137..000000000 --- a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/IdentifierCacheKeyFactory.java +++ /dev/null @@ -1,122 +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 com.google.common.base.Preconditions.checkArgument; -import static com.google.common.base.Preconditions.checkNotNull; -import static org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; - -import java.util.Collections; -import java.util.Set; -import java.util.stream.Collectors; -import java.util.stream.StreamSupport; -import javax.annotation.Nonnull; -import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.Identifiable; -import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; - -/** - * Factory providing cache keys to easier switching between scopes of caching - */ -public final class IdentifierCacheKeyFactory implements CacheKeyFactory { - - private static final String KEY_PARTS_SEPARATOR = "|"; - - // should be Set>>, but that's not possible for wildcards - private final Set> additionalKeyTypes; - - /** - * Construct simple cache key factory - */ - public IdentifierCacheKeyFactory() { - this(Collections.emptySet()); - } - - /** - * @param additionalKeyTypes Additional types from path of cached type, that are specifying scope - */ - public IdentifierCacheKeyFactory(@Nonnull final Set> additionalKeyTypes) { - // verify that all are non-null and identifiable - this.additionalKeyTypes = checkNotNull(additionalKeyTypes, "Additional key types can't be null").stream() - .map(IdentifierCacheKeyFactory::verifyNotNull) - .map(IdentifierCacheKeyFactory::verifyIsIdentifiable) - .collect(Collectors.toSet()); - } - - @Override - public String createKey(@Nonnull final InstanceIdentifier actualContextIdentifier) { - - checkNotNull(actualContextIdentifier, "Cannot construct key for null InstanceIdentifier"); - - // easiest case when only simple key is needed - if (additionalKeyTypes.isEmpty()) { - return actualContextIdentifier.getTargetType().toString(); - } - - checkArgument(isUniqueKeyConstructable(actualContextIdentifier), - "Unable to construct unique key, required key types : %s, provided paths : %s", additionalKeyTypes, - actualContextIdentifier.getPathArguments()); - - return String - .join(KEY_PARTS_SEPARATOR, additionalKeys(actualContextIdentifier), - actualContextIdentifier.getTargetType().toString()); - } - - /** - * Verifies that all requested key parts have keys - */ - private boolean isUniqueKeyConstructable(final InstanceIdentifier actualContextIdentifier) { - return StreamSupport.stream(actualContextIdentifier.getPathArguments().spliterator(), false) - .filter(this::isAdditionalScope) - .filter(this::isIdentifiable) - .count() == additionalKeyTypes.size(); - } - - private boolean isAdditionalScope(final InstanceIdentifier.PathArgument pathArgument) { - return additionalKeyTypes.contains(pathArgument.getType()); - } - - private boolean isIdentifiable(final InstanceIdentifier.PathArgument pathArgument) { - return pathArgument instanceof IdentifiableItem; - } - - - private String additionalKeys(final InstanceIdentifier actualContextIdentifier) { - return StreamSupport.stream(actualContextIdentifier.getPathArguments().spliterator(), false) - .filter(this::isAdditionalScope) - .filter(this::isIdentifiable) - .map(IdentifiableItem.class::cast) - .map(IdentifierCacheKeyFactory::bindKeyString) - .collect(Collectors.joining(KEY_PARTS_SEPARATOR)); - } - - private static String bindKeyString(IdentifiableItem identifiableItem) { - return String.format("%s[%s]", identifiableItem.getType().getTypeName(), identifiableItem.getKey()); - } - - private static Class verifyNotNull(final Class type) { - return checkNotNull(type, "Cannot use null as key"); - } - - /** - * Initial check if provided scope variables are identifiable aka. can be used to create unique cache key - */ - private static Class verifyIsIdentifiable(final Class type) { - checkArgument(Identifiable.class.isAssignableFrom(type), "Type %s is not Identifiable", type); - return type; - } -} diff --git a/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactory.java b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactory.java new file mode 100644 index 000000000..ba4e7e493 --- /dev/null +++ b/infra/translate-utils/src/main/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactory.java @@ -0,0 +1,132 @@ +/* + * 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 com.google.common.base.Preconditions.checkArgument; +import static com.google.common.base.Preconditions.checkNotNull; +import static org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; + +import java.util.Collections; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; +import javax.annotation.Nonnull; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Identifiable; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; + +/** + * Factory providing cache keys to easier switching between scopes of caching + */ +public final class TypeAwareIdentifierCacheKeyFactory implements CacheKeyFactory { + + private static final String KEY_PARTS_SEPARATOR = "|"; + + // should be Set>>, but that's not possible for wildcards + private final Set> additionalKeyTypes; + // factory must be aware of type of data, to prevent creating same key for same identifier but different data + private final Class type; + + /** + * Construct simple cache key factory + */ + public TypeAwareIdentifierCacheKeyFactory(@Nonnull final Class type) { + this(type, Collections.emptySet()); + } + + /** + * @param additionalKeyTypes Additional types from path of cached type, that are specifying scope + */ + public TypeAwareIdentifierCacheKeyFactory(@Nonnull final Class type, + @Nonnull final Set> additionalKeyTypes) { + // verify that all are non-null and identifiable + this.type = checkNotNull(type, "Type cannot be null"); + this.additionalKeyTypes = checkNotNull(additionalKeyTypes, "Additional key types can't be null").stream() + .map(TypeAwareIdentifierCacheKeyFactory::verifyNotNull) + .map(TypeAwareIdentifierCacheKeyFactory::verifyIsIdentifiable) + .collect(Collectors.toSet()); + } + + private static String bindKeyString(IdentifiableItem identifiableItem) { + return String.format("%s[%s]", identifiableItem.getType().getTypeName(), identifiableItem.getKey()); + } + + private static Class verifyNotNull(final Class type) { + return checkNotNull(type, "Cannot use null as key"); + } + + /** + * Initial check if provided scope variables are identifiable aka. can be used to create unique cache key + */ + private static Class verifyIsIdentifiable(final Class type) { + checkArgument(Identifiable.class.isAssignableFrom(type), "Type %s is not Identifiable", type); + return type; + } + + @Override + public String createKey(@Nonnull final InstanceIdentifier actualContextIdentifier) { + + checkNotNull(actualContextIdentifier, "Cannot construct key for null InstanceIdentifier"); + + // easiest case when only simple key is needed + if (additionalKeyTypes.isEmpty()) { + return String + .join(KEY_PARTS_SEPARATOR, type.getTypeName(), actualContextIdentifier.getTargetType().toString()); + } + + checkArgument(isUniqueKeyConstructable(actualContextIdentifier), + "Unable to construct unique key, required key types : %s, provided paths : %s", additionalKeyTypes, + actualContextIdentifier.getPathArguments()); + + // joins unique key in form : type | additional keys | actual context + return String + .join(KEY_PARTS_SEPARATOR, type.getTypeName(), additionalKeys(actualContextIdentifier), + actualContextIdentifier.getTargetType().toString()); + } + + @Override + public Class getCachedDataType() { + return type; + } + + /** + * Verifies that all requested key parts have keys + */ + private boolean isUniqueKeyConstructable(final InstanceIdentifier actualContextIdentifier) { + return StreamSupport.stream(actualContextIdentifier.getPathArguments().spliterator(), false) + .filter(this::isAdditionalScope) + .filter(this::isIdentifiable) + .count() == additionalKeyTypes.size(); + } + + private boolean isAdditionalScope(final InstanceIdentifier.PathArgument pathArgument) { + return additionalKeyTypes.contains(pathArgument.getType()); + } + + private boolean isIdentifiable(final InstanceIdentifier.PathArgument pathArgument) { + return pathArgument instanceof IdentifiableItem; + } + + private String additionalKeys(final InstanceIdentifier actualContextIdentifier) { + return StreamSupport.stream(actualContextIdentifier.getPathArguments().spliterator(), false) + .filter(this::isAdditionalScope) + .filter(this::isIdentifiable) + .map(IdentifiableItem.class::cast) + .map(TypeAwareIdentifierCacheKeyFactory::bindKeyString) + .collect(Collectors.joining(KEY_PARTS_SEPARATOR)); + } +} 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