abb9866ec1fce075708254eb814ac37cdd75ba27
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.vpp.util.cache;
18
19 import static io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor.NO_PARAMS;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Mockito.when;
22
23 import com.google.common.base.Optional;
24 import io.fd.honeycomb.translate.ModificationCache;
25 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
26 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
28 import io.fd.honeycomb.translate.util.read.cache.exceptions.execution.DumpExecutionFailedException;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.openvpp.jvpp.core.dto.IpDetails;
34 import org.openvpp.jvpp.core.dto.IpDetailsReplyDump;
35
36 public class DumpCacheManagerTest {
37
38     private static final String KEY = "cacheKey";
39
40     @Mock
41     private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
42
43     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
44     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
45     private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
46     private ModificationCache cache;
47
48     @Before
49     public void init() {
50         MockitoAnnotations.initMocks(this);
51         managerPositive =
52                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
53                         .withExecutor(executor)
54                         .build();
55
56         managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
57                 .withExecutor(executor)
58                 .withPostProcessingFunction(createPostProcessor())
59                 .build();
60
61         managerNegative =
62                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
63                         .withExecutor(executor)
64                         .build();
65
66         cache = new ModificationCache();
67     }
68
69     /**
70      * This test verify full dump-caching cycle
71      */
72     @Test
73     public void testCaching() throws DumpExecutionFailedException {
74         final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
75
76         // executor cant return null data
77         when(executor.executeDump(NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
78
79         final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(KEY, cache, NO_PARAMS);
80
81         // this is first call so instance should be from executor
82         // and it should be cached after calling executor
83         assertEquals(true, stage1Optional.isPresent());
84         assertEquals(stage1Data, stage1Optional.get());
85         assertEquals(true, cache.containsKey(KEY));
86         assertEquals(stage1Data, cache.get(KEY));
87
88         //rebind executor with other data
89         IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
90         when(executor.executeDump(NO_PARAMS)).thenReturn(stage2LoadedDump);
91
92         final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(KEY, cache, NO_PARAMS);
93
94         assertEquals(true, stage2Optional.isPresent());
95         assertEquals(stage2LoadedDump, stage2Optional.get());
96
97         //rebind executor with other data
98         IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
99         when(executor.executeDump(NO_PARAMS)).thenReturn(stage3LoadedDump);
100
101         final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(KEY, cache, NO_PARAMS);
102         assertEquals(true, stage3Optional.isPresent());
103         //check if it returns instance cached from previous stage
104         assertEquals(stage2LoadedDump, stage3Optional.get());
105     }
106
107     @Test
108     public void testPostprocessing() throws DumpExecutionFailedException {
109         IpDetailsReplyDump dump = new IpDetailsReplyDump();
110         IpDetails details = new IpDetails();
111         details.swIfIndex = 2;
112         dump.ipDetails.add(details);
113
114         when(executor.executeDump(null)).thenReturn(dump);
115
116         Optional<IpDetailsReplyDump> optionalDump = managerPositiveWithPostProcessing.getDump(KEY, cache, NO_PARAMS);
117
118         assertEquals(true, optionalDump.isPresent());
119         assertEquals(1, optionalDump.get().ipDetails.size());
120         assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
121     }
122
123     private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
124         return ipDetailsReplyDump -> {
125             IpDetailsReplyDump modified = new IpDetailsReplyDump();
126
127             for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
128                 IpDetails modifiedDetail = new IpDetails();
129                 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
130
131                 modified.ipDetails.add(modifiedDetail);
132             }
133
134             return modified;
135         };
136     }
137 }