ac7dcece0ec3fb24c0bfe18bb555526b3920789e
[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.read.ReadFailedException;
26 import io.fd.honeycomb.translate.util.read.cache.DumpCacheManager;
27 import io.fd.honeycomb.translate.util.read.cache.EntityDumpExecutor;
28 import io.fd.honeycomb.translate.util.read.cache.EntityDumpPostProcessingFunction;
29 import io.fd.vpp.jvpp.core.dto.IpDetails;
30 import io.fd.vpp.jvpp.core.dto.IpDetailsReplyDump;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
36
37 public class DumpCacheManagerTest {
38
39     private static final String KEY = "cacheKey";
40
41     @Mock
42     private EntityDumpExecutor<IpDetailsReplyDump, Void> executor;
43
44     @Mock
45     private InstanceIdentifier identifier;
46
47     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositive;
48     private DumpCacheManager<IpDetailsReplyDump, Void> managerPositiveWithPostProcessing;
49     private DumpCacheManager<IpDetailsReplyDump, Void> managerNegative;
50     private ModificationCache cache;
51
52     @Before
53     public void init() {
54         MockitoAnnotations.initMocks(this);
55         managerPositive =
56                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
57                         .withExecutor(executor)
58                         .build();
59
60         managerPositiveWithPostProcessing = new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
61                 .withExecutor(executor)
62                 .withPostProcessingFunction(createPostProcessor())
63                 .build();
64
65         managerNegative =
66                 new DumpCacheManager.DumpCacheManagerBuilder<IpDetailsReplyDump, Void>()
67                         .withExecutor(executor)
68                         .build();
69
70         cache = new ModificationCache();
71     }
72
73     /**
74      * This test verify full dump-caching cycle
75      */
76     @Test
77     public void testCaching() throws ReadFailedException {
78         final IpDetailsReplyDump stage1Data = new IpDetailsReplyDump();
79
80         // executor cant return null data
81         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(new IpDetailsReplyDump());
82
83         final Optional<IpDetailsReplyDump> stage1Optional = managerNegative.getDump(identifier, KEY, cache, NO_PARAMS);
84
85         // this is first call so instance should be from executor
86         // and it should be cached after calling executor
87         assertEquals(true, stage1Optional.isPresent());
88         assertEquals(stage1Data, stage1Optional.get());
89         assertEquals(true, cache.containsKey(KEY));
90         assertEquals(stage1Data, cache.get(KEY));
91
92         //rebind executor with other data
93         IpDetailsReplyDump stage2LoadedDump = new IpDetailsReplyDump();
94         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage2LoadedDump);
95
96         final Optional<IpDetailsReplyDump> stage2Optional = managerPositive.getDump(identifier, KEY, cache, NO_PARAMS);
97
98         assertEquals(true, stage2Optional.isPresent());
99         assertEquals(stage2LoadedDump, stage2Optional.get());
100
101         //rebind executor with other data
102         IpDetailsReplyDump stage3LoadedDump = new IpDetailsReplyDump();
103         when(executor.executeDump(identifier, NO_PARAMS)).thenReturn(stage3LoadedDump);
104
105         final Optional<IpDetailsReplyDump> stage3Optional = managerPositive.getDump(identifier, KEY, cache, NO_PARAMS);
106         assertEquals(true, stage3Optional.isPresent());
107         //check if it returns instance cached from previous stage
108         assertEquals(stage2LoadedDump, stage3Optional.get());
109     }
110
111     @Test
112     public void testPostprocessing() throws ReadFailedException {
113         IpDetailsReplyDump dump = new IpDetailsReplyDump();
114         IpDetails details = new IpDetails();
115         details.swIfIndex = 2;
116         dump.ipDetails.add(details);
117
118         when(executor.executeDump(identifier, null)).thenReturn(dump);
119
120         Optional<IpDetailsReplyDump> optionalDump =
121                 managerPositiveWithPostProcessing.getDump(identifier, KEY, cache, NO_PARAMS);
122
123         assertEquals(true, optionalDump.isPresent());
124         assertEquals(1, optionalDump.get().ipDetails.size());
125         assertEquals(7, optionalDump.get().ipDetails.get(0).swIfIndex);
126     }
127
128     private EntityDumpPostProcessingFunction<IpDetailsReplyDump> createPostProcessor() {
129         return ipDetailsReplyDump -> {
130             IpDetailsReplyDump modified = new IpDetailsReplyDump();
131
132             for (IpDetails detail : ipDetailsReplyDump.ipDetails) {
133                 IpDetails modifiedDetail = new IpDetails();
134                 modifiedDetail.swIfIndex = detail.swIfIndex + 5;
135
136                 modified.ipDetails.add(modifiedDetail);
137             }
138
139             return modified;
140         };
141     }
142 }