Adding interfaces in documentation
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / viewandinspect / services / BaseVisualizationContextTest.java
1 package org.onap.aai.sparky.viewandinspect.services;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5
6 import java.security.SecureRandom;
7 import java.util.ArrayList;
8 import java.util.HashSet;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Set;
12 import java.util.concurrent.ExecutorService;
13
14 import org.hamcrest.Matcher;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Matchers;
18 import org.mockito.Mockito;
19 import org.onap.aai.cl.api.Logger;
20 import org.onap.aai.cl.eelf.LoggerFactory;
21 import org.onap.aai.cl.mdc.MdcContext;
22 import org.onap.aai.restclient.client.OperationResult;
23 import org.onap.aai.sparky.config.oxm.OxmEntityLookup;
24 import org.onap.aai.sparky.config.oxm.OxmModelLoader;
25 import org.onap.aai.sparky.config.oxm.OxmModelProcessor;
26 import org.onap.aai.sparky.dal.ActiveInventoryAdapter;
27 import org.onap.aai.sparky.dal.rest.config.RestEndpointConfig;
28 import org.onap.aai.sparky.sync.entity.SearchableEntity;
29 import org.onap.aai.sparky.util.NodeUtils;
30 import org.onap.aai.sparky.util.StringCollectionContainsMatcher;
31 import org.onap.aai.sparky.util.TestResourceLoader;
32 import org.onap.aai.sparky.viewandinspect.config.VisualizationConfigs;
33 import org.onap.aai.sparky.viewandinspect.entity.ActiveInventoryNode;
34 import org.onap.aai.sparky.viewandinspect.entity.QueryParams;
35 import org.onap.aai.sparky.viewandinspect.enumeration.NodeProcessingState;
36
37
38 public class BaseVisualizationContextTest {
39   
40   private static SecureRandom secureRandom = new SecureRandom();
41   private static Logger LOG = LoggerFactory.getInstance().getLogger(BaseVisualizationContextTest.class);
42
43   private BaseVisualizationContext baseVisualizationContext;
44   private ExecutorService aaiExecutorService;
45   private VisualizationConfigs visualizationConfig;
46
47   private OxmEntityLookup oxmEntityLookup;
48   
49   private ActiveInventoryAdapter aaiAdapter;
50   private RestEndpointConfig aaiRestEndPointConfig;
51
52   @Before
53   public void init() throws Exception {
54
55     aaiExecutorService = NodeUtils.createNamedExecutor("SLNC-WORKER", 5, LOG);
56     visualizationConfig = new VisualizationConfigs();
57     
58     ArrayList<String> shallowEntities = new ArrayList<String>();
59     shallowEntities.add("cloud-region");
60     
61     visualizationConfig.setShallowEntities(shallowEntities);
62     visualizationConfig.setMaxSelfLinkTraversalDepth(2);
63  
64     oxmEntityLookup = new OxmEntityLookup();
65
66     aaiAdapter = Mockito.mock(ActiveInventoryAdapter.class);
67
68
69     Set<OxmModelProcessor> processors = new HashSet<OxmModelProcessor>();
70
71     processors.add(oxmEntityLookup);
72
73      
74     OxmModelLoader oxmModelLoader = new OxmModelLoader(-1, processors);
75     oxmModelLoader.loadLatestOxmModel();
76
77     aaiRestEndPointConfig = new RestEndpointConfig();
78     aaiRestEndPointConfig.setNumRequestRetries(5);
79     
80     Mockito.when(aaiAdapter.getEndpointConfig()).thenReturn(aaiRestEndPointConfig);
81     
82     MdcContext.initialize("" + secureRandom.nextLong(), "AAI-UI", "", "partner-name",
83         "localhost:4242");
84     
85     // all our resources are prefixed already, so the repairSelfLink shouldn't do anything to the link
86     Mockito.when(aaiAdapter.repairSelfLink(Matchers.contains(""))).thenReturn("");
87
88     
89   }
90   
91   private Matcher<List<String>> listContainsValue(String expectedValue) {
92     return new StringCollectionContainsMatcher(expectedValue);
93   }
94
95
96   @Test
97   public void validateBasicConstruction() throws Exception {
98
99     long contextId = secureRandom.nextLong();
100     
101     baseVisualizationContext = new BaseVisualizationContext(contextId, aaiAdapter,
102         aaiExecutorService, visualizationConfig, oxmEntityLookup);
103     
104     assertEquals(contextId, baseVisualizationContext.getContextId());
105
106   }
107   
108   @Test
109   public void validateSmallGraphAssembly() throws Exception {
110
111     /**
112      * We have a tiny graph that we will validate assembly of:
113      * 
114      * <li>customer -> tenant
115      * <li>customer -> service-subscription
116      * <li>service-subscription -> service-instance-1
117      * <li>service-subscription -> service-instance-2
118      * 
119      * At the end of this success path, we should have 5 nodes in the node cache. Once we have this
120      * flow we can experiment with error paths involving resource download failures to ensure graph
121      * nodes are in the correct state and that expected nodes are successfully represented in the
122      * cache.
123      */
124
125     long contextId = secureRandom.nextLong();
126
127     baseVisualizationContext = new BaseVisualizationContext(contextId, aaiAdapter,
128         aaiExecutorService, visualizationConfig, oxmEntityLookup);
129
130     SearchableEntity searchableEntity = new SearchableEntity();
131     String customerSelfLink =
132         "https://server.proxy:8443/aai/v11/business/customers/customer/customer-4";
133     String customerNodeId = NodeUtils.generateUniqueShaDigest(customerSelfLink);
134
135     searchableEntity.setId(customerNodeId);
136     searchableEntity.setEntityType("customer");
137     searchableEntity.setEntityPrimaryKeyValue("customer-4");
138     searchableEntity.setLink(customerSelfLink);
139
140     QueryParams queryParams = new QueryParams();
141     queryParams.setSearchTargetNodeId(customerNodeId);
142     queryParams.setSearchTargetPrimaryKeyValues("customer-4");
143
144     //  aai customer resource dip
145
146     Mockito
147         .when(aaiAdapter.queryActiveInventoryWithRetries(
148             Matchers.contains("customer-4"), Mockito.anyString(),
149             Mockito.anyInt()))
150         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
151             "/sync/aai/aai-resources/customer/customer-4.json")));
152
153     //  aai tenant resource dip
154
155     Mockito
156         .when(aaiAdapter.queryActiveInventoryWithRetries(
157             Matchers.contains("tenant/tenant-1"), Mockito.anyString(),
158             Mockito.anyInt()))
159         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
160             "/sync/aai/aai-resources/tenant/tenant-1.json")));
161     
162     // generic-queries for service-subscription
163     
164     Mockito
165     .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-subscription"),
166         Matchers.argThat(listContainsValue("service-subscription.service-type:service-subscription-2"))))
167     .thenReturn(
168         "https://server.proxy:8443/aai/v11/search/generic-query/service-subscription-2");
169
170     Mockito
171         .when(aaiAdapter.queryActiveInventoryWithRetries(
172             Matchers.contains("generic-query/service-subscription-2"), Mockito.anyString(), Mockito.anyInt()))
173         .thenReturn(new OperationResult(200, TestResourceLoader
174             .getTestResourceDataJson("/sync/aai/aai-traversal/generic-query/service-subscription/service-subscription-2.json")));    
175     
176     // generic-queries for service-instance-1
177     
178     Mockito
179         .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-instance"),
180             Matchers.argThat(
181                 listContainsValue("service-instance-id:service-instance-54"))))
182         .thenReturn(
183             "https://server.proxy:8443/aai/v11/search/generic-query/service-instance-id/service-instance-54");
184
185     Mockito
186         .when(aaiAdapter.queryActiveInventoryWithRetries(
187             Matchers
188                 .contains("generic-query/service-instance-id/service-instance-54"),
189             Mockito.anyString(), Mockito.anyInt()))
190         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
191             "/sync/aai/aai-traversal/generic-query/service-instance/service-instance-54.json")));
192
193     // generic-queries for service-instance-2
194     
195     Mockito
196         .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-instance"),
197             Matchers.argThat(
198                 listContainsValue("service-instance-id:service-instance-55"))))
199         .thenReturn(
200             "https://server.proxy:8443/aai/v11/search/generic-query/service-instance-id/service-instance-55");
201
202     Mockito
203         .when(aaiAdapter.queryActiveInventoryWithRetries(
204             Matchers
205                 .contains("generic-query/service-instance-id/service-instance-55"),
206             Mockito.anyString(), Mockito.anyInt()))
207         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
208             "/sync/aai/aai-traversal/generic-query/service-instance/service-instance-55.json")));    
209     
210     
211     
212     // start the test
213     
214     baseVisualizationContext.processSelfLinks(searchableEntity, queryParams);
215
216     /*
217      * validation can be in the form of validating nodes + relationships from the node cache
218      * baseVisualizationContext.getNodeCache();
219      */
220
221     Map<String, ActiveInventoryNode> nodeCache = baseVisualizationContext.getNodeCache();
222
223     assertEquals(5, nodeCache.size());
224     assertNotNull(nodeCache.get(customerNodeId));
225     assertEquals("customer", nodeCache.get(customerNodeId).getEntityType());
226     
227     // verify node collection nodes
228     
229     ActiveInventoryNode customerNode = nodeCache.get("da4101ad19b3c380a1c12ffeda8ab390e1489fb4a22a392c9a1939db63c3dec5");
230     ActiveInventoryNode ssNode = nodeCache.get("f4ceaf19459993c4fc9438a7579dd20d786109f4455e38682c579045b7ae615e");
231     ActiveInventoryNode tenantNode = nodeCache.get("4735439b29e446b339535668238076e4b392eaa3eec218936e12f735179bc55e");
232     ActiveInventoryNode s1 = nodeCache.get("f975ab453b142197af5d0173e0a9cf2aa22d10502f8ad655c8d17de81b066e8f");
233     ActiveInventoryNode s2 = nodeCache.get("de77ef8f76dd6f19662b163527ff839891b9596cac655e3143fdd7ad39e2e4e3");
234     
235     assertNotNull( customerNode );
236     assertNotNull( ssNode );
237     assertNotNull( tenantNode );
238     assertNotNull( s1 );
239     assertNotNull( s2 );
240     
241     // verify node depths
242     
243     assertEquals( 0, customerNode.getNodeDepth() );
244     assertEquals( 1, ssNode.getNodeDepth() );
245     
246     /*
247      * I think there is a bug in the way the node depth is represented due to the enforcement of
248      * bidirectional links being disabled. We may have to circle back to this behavior at some point
249      * and re-verify that the behavior works properly.
250      */
251     
252     assertEquals( 2, tenantNode.getNodeDepth() );
253     assertEquals( 2, s1.getNodeDepth() );
254     assertEquals( 2, s2.getNodeDepth() );
255
256     // verify node states
257     
258     assertEquals( NodeProcessingState.READY, customerNode.getState() );
259     assertEquals( NodeProcessingState.READY, ssNode.getState() );
260     
261     /*
262      * these nodes have a NEIGHBORS_UNPROCESSED state because the max traversal depth was hit before
263      * processing all the nested relationships.  I think what we should look at is advancing the state
264      * to READY if in fact there are no relationships to process, which I think could be the case
265      * sometimes.
266      */
267     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, tenantNode.getState() );
268     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, s1.getState() );
269     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, s2.getState() );
270
271   }  
272   
273 }