diff options
author | Jan Srnicek <jsrnicek@cisco.com> | 2016-07-27 11:13:11 +0200 |
---|---|---|
committer | Maros Marsalek <mmarsale@cisco.com> | 2016-07-27 09:49:54 +0000 |
commit | 007d4542388ca89be409ce1a4a4c7a36ddcb538f (patch) | |
tree | 0685e0e6227d1dab10f8ce47d120bdb45a963480 /v3po | |
parent | 6b9de4956520164e52033a964fc20f5f4aebd8ad (diff) |
HONEYCOMB-106 - Support for generic cache management
Changed exception handling/logging
Removed useless utility class/its logic shifted to respective classes
Cleanup of not needed code
Change-Id: Id2fe4ab60b541067c3d0dc6fa442fbfa66d1e618
Signed-off-by: Jan Srnicek <jsrnicek@cisco.com>
Diffstat (limited to 'v3po')
11 files changed, 578 insertions, 0 deletions
diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java new file mode 100644 index 000000000..9486ce171 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManager.java @@ -0,0 +1,124 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import static com.google.common.base.Preconditions.checkNotNull; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.noop.NoopDumpPostProcessingFunction; +import javax.annotation.Nonnull; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Manager responsible for returning Data object dumps<br> either from cache or by invoking specified {@link + * EntityDumpExecutor} + */ +public final class DumpCacheManager<T extends JVppReplyDump, U> { + + private static final Logger LOG = LoggerFactory.getLogger(DumpCacheManager.class); + + private final EntityDumpExecutor<T, U> dumpExecutor; + private final EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck; + private final EntityDumpPostProcessingFunction<T> postProcessor; + + private DumpCacheManager(DumpCacheManagerBuilder builder) { + this.dumpExecutor = builder.dumpExecutor; + this.dumpNonEmptyCheck = builder.dumpNonEmptyCheck; + this.postProcessor = builder.postProcessingFunction; + } + + /** + * Returns {@link Optional<T>} of dump + */ + public Optional<T> getDump(@Nonnull String entityKey, @Nonnull ModificationCache cache) + throws DumpExecutionFailedException { + + //this key binding to every log has its logic ,because every customizer have its own cache manager and if + //there is need for debugging/fixing some complex call with a lot of data,you can get lost in those logs + LOG.debug("Loading dump for KEY[{}]", entityKey); + + T dump = (T) cache.get(entityKey); + + if (dump == null) { + LOG.debug("Dump for KEY[{}] not present in cache,invoking dump executor", entityKey); + + dump = dumpExecutor.executeDump(); + + //this is not a critical exception, so its only logged here + try { + dumpNonEmptyCheck.assertNotEmpty(dump); + } catch (DumpCheckFailedException e) { + LOG.warn("Dump for KEY[{}] has been resolved as empty", entityKey, e); + return Optional.absent(); + } + + //no need to check if post processor active,if wasnt set,default no-op will be used + LOG.debug("Post-processing dump for KEY[{}]", entityKey); + dump = postProcessor.apply(dump); + + LOG.debug("Caching dump for KEY[{}]", entityKey); + cache.put(entityKey, dump); + return Optional.of(dump); + } else { + return Optional.of(dump); + } + } + + public static final class DumpCacheManagerBuilder<T extends JVppReplyDump, U> { + + private EntityDumpExecutor<T, U> dumpExecutor; + private EntityDumpNonEmptyCheck<T> dumpNonEmptyCheck; + private EntityDumpPostProcessingFunction<T> postProcessingFunction; + + public DumpCacheManagerBuilder() { + //for cases when user does not set specific post-processor + postProcessingFunction = new NoopDumpPostProcessingFunction<T>(); + } + + public DumpCacheManagerBuilder<T, U> withExecutor(@Nonnull EntityDumpExecutor<T, U> executor) { + this.dumpExecutor = executor; + return this; + } + + public DumpCacheManagerBuilder<T, U> withNonEmptyPredicate(@Nonnull EntityDumpNonEmptyCheck<T> check) { + this.dumpNonEmptyCheck = check; + return this; + } + + public DumpCacheManagerBuilder<T, U> withPostProcessingFunction( + EntityDumpPostProcessingFunction postProcessingFunction) { + this.postProcessingFunction = postProcessingFunction; + return this; + } + + public DumpCacheManager<T, U> build() { + checkNotNull(dumpExecutor, "Dump executor cannot be null"); + checkNotNull(dumpNonEmptyCheck, "Dump verifier cannot be null"); + checkNotNull(postProcessingFunction, + "Dump post-processor cannot be null cannot be null, default implementation is used if its not set"); + + return new DumpCacheManager<T, U>(this); + } + } +} + + diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java new file mode 100644 index 000000000..82b30cf26 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpExecutor.java @@ -0,0 +1,41 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that return dumps for Data objects + */ +public interface EntityDumpExecutor<T extends JVppReplyDump, U> { + + /** + * Performs dump on {link T} entity + * + * @return dump of specified {@link T} entity + * @throws DumpExecutionFailedException when dump fails + */ + public T executeDump() throws DumpExecutionFailedException; + + /** + * Bind dumping params for executor + */ + default public void bindDumpParams(U params) { + throw new UnsupportedOperationException("You should override this method if you need to bind dumping params"); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java new file mode 100644 index 000000000..6fb404a7a --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpNonEmptyCheck.java @@ -0,0 +1,33 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for classes that verifies if dump of data object is non-empty + */ +public interface EntityDumpNonEmptyCheck<T extends JVppReplyDump> { + + /** + * Verifies if data are non-empty,if not throws {@link DumpEmptyException} + * @throws DumpEmptyException + */ + public void assertNotEmpty(T data) throws DumpCheckFailedException; +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java new file mode 100644 index 000000000..3d0040ea6 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/EntityDumpPostProcessingFunction.java @@ -0,0 +1,36 @@ +/* + * 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.v3po.translate.v3po.util.cache; + +import java.util.function.Function; +import org.openvpp.jvpp.dto.JVppReplyDump; + +/** + * Generic interface for class that are post-processing data dumped from vpp + */ +@FunctionalInterface +public interface EntityDumpPostProcessingFunction<T extends JVppReplyDump> extends Function<T, T> { + + + /** + * Performs postprocessing on dumped data + * + * @return Post-processed data + */ + @Override + T apply(T t); +}
\ No newline at end of file diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java new file mode 100644 index 000000000..f3f8f7fef --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/DumpCheckFailedException.java @@ -0,0 +1,31 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.check; + +/** + * Abstract parent of exceptions thrown while checking if dump is not empty + */ +public abstract class DumpCheckFailedException extends Exception { + + public DumpCheckFailedException(String message) { + super(message); + } + + public DumpCheckFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java new file mode 100644 index 000000000..a5a228823 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/check/i/DumpEmptyException.java @@ -0,0 +1,40 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.check.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.EntityDumpNonEmptyCheck; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.DumpCheckFailedException; + +/** + * This exception occurs when dump is resolved as empty by {@link EntityDumpNonEmptyCheck} + */ +public class DumpEmptyException extends DumpCheckFailedException { + + /** + * Creates {@link DumpEmptyException} with specified reason + */ + public DumpEmptyException(String reason) { + super(reason); + } + + /** + * Creates {@link DumpEmptyException} with specified reason and sub-exception + */ + public DumpEmptyException(String reason, Exception e) { + super(reason, e); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java new file mode 100644 index 000000000..5f4939cfb --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/DumpExecutionFailedException.java @@ -0,0 +1,27 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.execution; + +/** + * Default parent for all exceptions connected to dumping of VPP data + */ +public abstract class DumpExecutionFailedException extends Exception { + + public DumpExecutionFailedException(String message, Exception cause) { + super(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java new file mode 100644 index 000000000..9b92409f5 --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpCallFailedException.java @@ -0,0 +1,34 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.openvpp.jvpp.VppBaseCallException; + +/** + * Wrapper exception for {@link org.openvpp.jvpp.VppBaseCallException} during dumping + */ +public class DumpCallFailedException extends DumpExecutionFailedException { + + public DumpCallFailedException(String message, VppBaseCallException cause) { + super(message, cause); + } + + public static final DumpCallFailedException wrapFailedCallException(String message, VppBaseCallException cause) { + return new DumpCallFailedException(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java new file mode 100644 index 000000000..afb278c9d --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/exceptions/execution/i/DumpTimeoutException.java @@ -0,0 +1,35 @@ +/* + * 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.v3po.translate.v3po.util.cache.exceptions.execution.i; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import java.util.concurrent.TimeoutException; + +/** + * Exception thrown when dump call ends in timeout + */ +public class DumpTimeoutException extends DumpExecutionFailedException { + + public DumpTimeoutException(String message, TimeoutException cause) { + super(message, cause); + + } + + public static final DumpTimeoutException wrapTimeoutException(String message, TimeoutException cause) { + return new DumpTimeoutException(message, cause); + } +} diff --git a/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java new file mode 100644 index 000000000..5efe439ab --- /dev/null +++ b/v3po/vpp-translate-utils/src/main/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/noop/NoopDumpPostProcessingFunction.java @@ -0,0 +1,33 @@ +/* + * 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.v3po.translate.v3po.util.cache.noop; + +import io.fd.honeycomb.v3po.translate.v3po.util.cache.EntityDumpPostProcessingFunction; +import org.openvpp.jvpp.dto.JVppReplyDump; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class NoopDumpPostProcessingFunction<T extends JVppReplyDump> implements EntityDumpPostProcessingFunction<T> { + + private static final Logger LOG = LoggerFactory.getLogger(NoopDumpPostProcessingFunction.class); + + @Override + public T apply(final T t) { + LOG.debug("Default post processing function called for {}", t); + return t; + } +} diff --git a/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java new file mode 100644 index 000000000..9cbbda8ad --- /dev/null +++ b/v3po/vpp-translate-utils/src/test/java/io/fd/honeycomb/v3po/translate/v3po/util/cache/DumpCacheManagerTest.java @@ -0,0 +1,144 @@ +/* + * 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.v3po.translate.v3po.util.cache; + + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + +import com.google.common.base.Optional; +import io.fd.honeycomb.v3po.translate.ModificationCache; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.check.i.DumpEmptyException; +import io.fd.honeycomb.v3po.translate.v3po.util.cache.exceptions.execution.DumpExecutionFailedException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openvpp.jvpp.dto.IpDetails; +import org.openvpp.jvpp.dto.IpDetailsReplyDump; + +public class DumpCacheManagerTest { + + private static final String KEY = "cacheKey"; + + @Mock + private EntityDumpExecutor<IpDetailsReplyDump, Void> executor; + + private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive; + private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing; + private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative; + private ModificationCache cache; + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + managerPositive = + new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .build(); + + managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>() + .withExecutor(executor) + .withNonEmptyPredicate(createPositivePredicate()) + .withPostProcessingFunction(createPostProcessor()) + .build(); + + managerNegative = + new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>() + .withExecutor(executor) + .withNonEmptyPredicate(createNegativePredicate()) + .build(); + + cache = new ModificationCache(); + } + + /** + * This test verify full dump-caching cycle + */ + @Test + public void testCaching() throws DumpExecutionFailedException { + + + Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache); + + //this is first call so instance should be from executor + assertEquals(false, stage1Optional.isPresent()); + assertEquals(false, cache.containsKey(KEY)); + + //rebind executor with other data + IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage2LoadedDump); + + Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache); + + assertEquals(true, stage2Optional.isPresent()); + assertEquals(stage2LoadedDump, stage2Optional.get()); + + //rebind executor with other data + IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump(); + when(executor.executeDump()).thenReturn(stage3LoadedDump); + + Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache); + assertEquals(true, stage3Optional.isPresent()); + //check if it returns instance cached from previous stage + assertEquals(stage2LoadedDump, stage3Optional.get()); + } + + @Test + public void testPostprocessing() throws DumpExecutionFailedException { + IpDetailsReplyDump dump = new IpDetailsReplyDump(); + IpDetails details = new IpDetails(); + details.swIfIndex = 2; + dump.ipDetails.add(details); + + when(executor.executeDump()).thenReturn(dump); + + Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache); + + assertEquals(true, optionalDump.isPresent()); + assertEquals(1, optionalDump.get().ipDetails.size()); + assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex); + } + + private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createNegativePredicate() { + return data -> { + throw new DumpEmptyException("Empty dump", new IllegalArgumentException()); + }; + } + + private EntityDumpNonEmptyCheck<IpDetailsReplyDump> createPositivePredicate() { + return data -> { + //DO NOTHING + }; + } + + private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() { + return ipDetailsReplyDump -> { + IpDetailsReplyDump modified = new IpDetailsReplyDump(); + + for (IpDetails detail : ipDetailsReplyDump.ipDetails) { + IpDetails modifiedDetail = new IpDetails(); + modifiedDetail.swIfIndex = detail.swIfIndex + 5; + + modified.ipDetails.add(modifiedDetail); + } + + return modified; + }; + } +}
\ No newline at end of file |