1 package org.onap.aai.sparky.viewandinspect.services;
 
   3 import static org.junit.Assert.assertEquals;
 
   4 import static org.junit.Assert.assertNotNull;
 
   6 import java.security.SecureRandom;
 
   7 import java.util.ArrayList;
 
   8 import java.util.HashSet;
 
  12 import java.util.concurrent.ExecutorService;
 
  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;
 
  38 public class BaseVisualizationContextTest {
 
  40   private static SecureRandom secureRandom = new SecureRandom();
 
  41   private static Logger LOG = LoggerFactory.getInstance().getLogger(BaseVisualizationContextTest.class);
 
  43   private BaseVisualizationContext baseVisualizationContext;
 
  44   private ExecutorService aaiExecutorService;
 
  45   private VisualizationConfigs visualizationConfig;
 
  47   private OxmEntityLookup oxmEntityLookup;
 
  49   private ActiveInventoryAdapter aaiAdapter;
 
  50   private RestEndpointConfig aaiRestEndPointConfig;
 
  53   public void init() throws Exception {
 
  55     aaiExecutorService = NodeUtils.createNamedExecutor("SLNC-WORKER", 5, LOG);
 
  56     visualizationConfig = new VisualizationConfigs();
 
  58     ArrayList<String> shallowEntities = new ArrayList<String>();
 
  59     shallowEntities.add("cloud-region");
 
  61     visualizationConfig.setShallowEntities(shallowEntities);
 
  62     visualizationConfig.setMaxSelfLinkTraversalDepth(2);
 
  64     oxmEntityLookup = new OxmEntityLookup();
 
  66     aaiAdapter = Mockito.mock(ActiveInventoryAdapter.class);
 
  69     Set<OxmModelProcessor> processors = new HashSet<OxmModelProcessor>();
 
  71     processors.add(oxmEntityLookup);
 
  74     OxmModelLoader oxmModelLoader = new OxmModelLoader(-1, processors);
 
  75     oxmModelLoader.loadLatestOxmModel();
 
  77     aaiRestEndPointConfig = new RestEndpointConfig();
 
  78     aaiRestEndPointConfig.setNumRequestRetries(5);
 
  80     Mockito.when(aaiAdapter.getEndpointConfig()).thenReturn(aaiRestEndPointConfig);
 
  82     MdcContext.initialize("" + secureRandom.nextLong(), "AAI-UI", "", "partner-name",
 
  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("");
 
  91   private Matcher<List<String>> listContainsValue(String expectedValue) {
 
  92     return new StringCollectionContainsMatcher(expectedValue);
 
  97   public void validateBasicConstruction() throws Exception {
 
  99     long contextId = secureRandom.nextLong();
 
 101     baseVisualizationContext = new BaseVisualizationContext(contextId, aaiAdapter,
 
 102         aaiExecutorService, visualizationConfig, oxmEntityLookup);
 
 104     assertEquals(contextId, baseVisualizationContext.getContextId());
 
 109   public void validateSmallGraphAssembly() throws Exception {
 
 112      * We have a tiny graph that we will validate assembly of:
 
 114      * <li>customer -> tenant
 
 115      * <li>customer -> service-subscription
 
 116      * <li>service-subscription -> service-instance-1
 
 117      * <li>service-subscription -> service-instance-2
 
 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
 
 125     long contextId = secureRandom.nextLong();
 
 127     baseVisualizationContext = new BaseVisualizationContext(contextId, aaiAdapter,
 
 128         aaiExecutorService, visualizationConfig, oxmEntityLookup);
 
 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);
 
 135     searchableEntity.setId(customerNodeId);
 
 136     searchableEntity.setEntityType("customer");
 
 137     searchableEntity.setEntityPrimaryKeyValue("customer-4");
 
 138     searchableEntity.setLink(customerSelfLink);
 
 140     QueryParams queryParams = new QueryParams();
 
 141     queryParams.setSearchTargetNodeId(customerNodeId);
 
 142     queryParams.setSearchTargetPrimaryKeyValues("customer-4");
 
 144     //  aai customer resource dip
 
 147         .when(aaiAdapter.queryActiveInventoryWithRetries(
 
 148             Matchers.contains("customer-4"), Mockito.anyString(),
 
 150         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
 
 151             "/sync/aai/aai-resources/customer/customer-4.json")));
 
 153     //  aai tenant resource dip
 
 156         .when(aaiAdapter.queryActiveInventoryWithRetries(
 
 157             Matchers.contains("tenant/tenant-1"), Mockito.anyString(),
 
 159         .thenReturn(new OperationResult(200, TestResourceLoader.getTestResourceDataJson(
 
 160             "/sync/aai/aai-resources/tenant/tenant-1.json")));
 
 162     // generic-queries for service-subscription
 
 165     .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-subscription"),
 
 166         Matchers.argThat(listContainsValue("service-subscription.service-type:service-subscription-2"))))
 
 168         "https://server.proxy:8443/aai/v11/search/generic-query/service-subscription-2");
 
 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")));    
 
 176     // generic-queries for service-instance-1
 
 179         .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-instance"),
 
 181                 listContainsValue("service-instance-id:service-instance-54"))))
 
 183             "https://server.proxy:8443/aai/v11/search/generic-query/service-instance-id/service-instance-54");
 
 186         .when(aaiAdapter.queryActiveInventoryWithRetries(
 
 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")));
 
 193     // generic-queries for service-instance-2
 
 196         .when(aaiAdapter.getGenericQueryForSelfLink(Matchers.contains("service-instance"),
 
 198                 listContainsValue("service-instance-id:service-instance-55"))))
 
 200             "https://server.proxy:8443/aai/v11/search/generic-query/service-instance-id/service-instance-55");
 
 203         .when(aaiAdapter.queryActiveInventoryWithRetries(
 
 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")));    
 
 214     baseVisualizationContext.processSelfLinks(searchableEntity, queryParams);
 
 217      * validation can be in the form of validating nodes + relationships from the node cache
 
 218      * baseVisualizationContext.getNodeCache();
 
 221     Map<String, ActiveInventoryNode> nodeCache = baseVisualizationContext.getNodeCache();
 
 223     assertEquals(5, nodeCache.size());
 
 224     assertNotNull(nodeCache.get(customerNodeId));
 
 225     assertEquals("customer", nodeCache.get(customerNodeId).getEntityType());
 
 227     // verify node collection nodes
 
 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");
 
 235     assertNotNull( customerNode );
 
 236     assertNotNull( ssNode );
 
 237     assertNotNull( tenantNode );
 
 241     // verify node depths
 
 243     assertEquals( 0, customerNode.getNodeDepth() );
 
 244     assertEquals( 1, ssNode.getNodeDepth() );
 
 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.
 
 252     assertEquals( 2, tenantNode.getNodeDepth() );
 
 253     assertEquals( 2, s1.getNodeDepth() );
 
 254     assertEquals( 2, s2.getNodeDepth() );
 
 256     // verify node states
 
 258     assertEquals( NodeProcessingState.READY, customerNode.getState() );
 
 259     assertEquals( NodeProcessingState.READY, ssNode.getState() );
 
 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
 
 267     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, tenantNode.getState() );
 
 268     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, s1.getState() );
 
 269     assertEquals( NodeProcessingState.NEIGHBORS_UNPROCESSED, s2.getState() );