@Test public void testGetRandomServiceAddressByStackWithoutFlavor() throws Exception { HostIPs hostIps1 = new HostIPs("ipv4", "ipv6"); HostIPs hostIps2 = new HostIPs("ipv4_2", "ipv6_2"); List<String> flavors = new ArrayList<>(2); flavors.add("1.50"); flavors.add("1.49"); when(stacksDAO.getAllStackPaths()).thenReturn(getStackPaths("/po/poc1/1.50/xreGuide")); when(stacksDAO.getHostsCount(new XreStackPath("/po/poc1/1.50/xreGuide"))).thenReturn(1); when(stacksDAO.getHostsCount(new XreStackPath("/po/poc1/1.49/xreGuide"))).thenReturn(1); when(stacksDAO.getFlavorsByStackOnlyPath(any(XreStackPath.class))).thenReturn(flavors); when(stacksDAO.getHostByIndex(new XreStackPath("/po/poc1/1.50/xreGuide"), 0)).thenReturn(hostIps1); when(stacksDAO.getHostByIndex(new XreStackPath("/po/poc1/1.49/xreGuide"), 0)).thenReturn(hostIps2); int returnFirstAddressCount = 0; int returnSecondAddressCount = 0; for (int i = 0; i < 100; i++) { HostIPsListWrapper result = testee.getRandomHostForStackOnlyAndService("/po/poc1", "xreGuide"); Assert.assertTrue(result.getHostIPsList().size() == 1); HostIPs resultHostIps = result.getHostIPsList().get(0); if (new HostIPs("ipv4", "ipv6").equals(resultHostIps)) { returnFirstAddressCount ++; } if (new HostIPs("ipv4_2", "ipv6_2").equals(resultHostIps)) { returnSecondAddressCount ++; } Assert.assertTrue(hostIps1.equals(resultHostIps) || hostIps2.equals(resultHostIps)); } Assert.assertTrue(returnFirstAddressCount > 0); Assert.assertTrue(returnSecondAddressCount > 0); }
@Override public HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName) { XreStackPath noFlavorStackPath = getNoFlavorXreStackPath(stackOnlyPath, serviceName); List<String> flavors = getFlavorsFromStackOnly(noFlavorStackPath); Collections.shuffle(flavors); for (String flavorName : flavors) { try { List<HostIPs> randomIp = getRandomHostForStackAndService(stackOnlyPath + DELIMETER + flavorName, serviceName).getHostIPsList(); if (randomIp.size() > 0) { HostIPsListWrapper result = new HostIPsListWrapper(); result.getHostIPsList().addAll(randomIp); return result; } } catch (Exception ignore) { log.info("Exception '{}' was caught and ignored while getting random host by stack name without flavor", ignore.getMessage()); } } return new HostIPsListWrapper(); }
StacksService implements IStacksService { @Override public HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName) { XreStackPath noFlavorStackPath = getNoFlavorXreStackPath(stackOnlyPath, serviceName); List<String> flavors = getFlavorsFromStackOnly(noFlavorStackPath); Collections.shuffle(flavors); for (String flavorName : flavors) { try { List<HostIPs> randomIp = getRandomHostForStackAndService(stackOnlyPath + DELIMETER + flavorName, serviceName).getHostIPsList(); if (randomIp.size() > 0) { HostIPsListWrapper result = new HostIPsListWrapper(); result.getHostIPsList().addAll(randomIp); return result; } } catch (Exception ignore) { log.info("Exception '{}' was caught and ignored while getting random host by stack name without flavor", ignore.getMessage()); } } return new HostIPsListWrapper(); } }
StacksService implements IStacksService { @Override public HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName) { XreStackPath noFlavorStackPath = getNoFlavorXreStackPath(stackOnlyPath, serviceName); List<String> flavors = getFlavorsFromStackOnly(noFlavorStackPath); Collections.shuffle(flavors); for (String flavorName : flavors) { try { List<HostIPs> randomIp = getRandomHostForStackAndService(stackOnlyPath + DELIMETER + flavorName, serviceName).getHostIPsList(); if (randomIp.size() > 0) { HostIPsListWrapper result = new HostIPsListWrapper(); result.getHostIPsList().addAll(randomIp); return result; } } catch (Exception ignore) { log.info("Exception '{}' was caught and ignored while getting random host by stack name without flavor", ignore.getMessage()); } } return new HostIPsListWrapper(); }  }
StacksService implements IStacksService { @Override public HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName) { XreStackPath noFlavorStackPath = getNoFlavorXreStackPath(stackOnlyPath, serviceName); List<String> flavors = getFlavorsFromStackOnly(noFlavorStackPath); Collections.shuffle(flavors); for (String flavorName : flavors) { try { List<HostIPs> randomIp = getRandomHostForStackAndService(stackOnlyPath + DELIMETER + flavorName, serviceName).getHostIPsList(); if (randomIp.size() > 0) { HostIPsListWrapper result = new HostIPsListWrapper(); result.getHostIPsList().addAll(randomIp); return result; } } catch (Exception ignore) { log.info("Exception '{}' was caught and ignored while getting random host by stack name without flavor", ignore.getMessage()); } } return new HostIPsListWrapper(); }  @PostConstruct void init(); @Override ServicePaths getStacksForService(String serviceName); @Override ServicePaths updateStacksForService(String serviceName, ServicePaths servicePaths, Whitelisted whitelisted); @Override Set<String> getAllServiceNames(); @Override Boolean isServiceExists(String serviceName); @Override ServicePaths getStacksForAllServices(); @Override Set<PathItem> getActiveStacksAndFlavors(String serviceName); Set<PathItem> getActiveStacksAndFlavors(String serviceName, ServicePaths servicePaths); @Override HostIPsListWrapper getHostsForStackOnlyAndService(String stackName, String serviceName); @Override HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName); @Override HostIPsListWrapper getHostsForStackAndService(String stackName, String serviceName); @Override HostIPsListWrapper getRandomHostForStackAndService(String stackName, String serviceName); @Override HostIPsListWrapper getHostsForFlavorAndService(String flavorName, String serviceName); @Override HostIPsListWrapper getRandomHostForFlavorAndService(String flavorName, String serviceName); @Override Set<StackData> getAllStacksAndHosts(String serviceName); Boolean isStackNameWithoutFlavor(String stackName); @Override HostIPsListWrapper getAddressByStackOrFlavor(String appName, String stackName, String flavorName); @Override HostIPsListWrapper getRandomAddressByStackOrFlavor(String serviceName, String stackName, String flavorName); @Override void deleteStack(String serviceName, String dataCenter, String availabilityZone, String flavor); @Override synchronized void deleteStacks(Paths paths); void setWhiteListService(IWhiteListService whiteListService); void setInitialDelay(long initialDelay); void setRefreshDelay(long refreshDelay); }
StacksService implements IStacksService { @Override public HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName) { XreStackPath noFlavorStackPath = getNoFlavorXreStackPath(stackOnlyPath, serviceName); List<String> flavors = getFlavorsFromStackOnly(noFlavorStackPath); Collections.shuffle(flavors); for (String flavorName : flavors) { try { List<HostIPs> randomIp = getRandomHostForStackAndService(stackOnlyPath + DELIMETER + flavorName, serviceName).getHostIPsList(); if (randomIp.size() > 0) { HostIPsListWrapper result = new HostIPsListWrapper(); result.getHostIPsList().addAll(randomIp); return result; } } catch (Exception ignore) { log.info("Exception '{}' was caught and ignored while getting random host by stack name without flavor", ignore.getMessage()); } } return new HostIPsListWrapper(); }  @PostConstruct void init(); @Override ServicePaths getStacksForService(String serviceName); @Override ServicePaths updateStacksForService(String serviceName, ServicePaths servicePaths, Whitelisted whitelisted); @Override Set<String> getAllServiceNames(); @Override Boolean isServiceExists(String serviceName); @Override ServicePaths getStacksForAllServices(); @Override Set<PathItem> getActiveStacksAndFlavors(String serviceName); Set<PathItem> getActiveStacksAndFlavors(String serviceName, ServicePaths servicePaths); @Override HostIPsListWrapper getHostsForStackOnlyAndService(String stackName, String serviceName); @Override HostIPsListWrapper getRandomHostForStackOnlyAndService(String stackOnlyPath, String serviceName); @Override HostIPsListWrapper getHostsForStackAndService(String stackName, String serviceName); @Override HostIPsListWrapper getRandomHostForStackAndService(String stackName, String serviceName); @Override HostIPsListWrapper getHostsForFlavorAndService(String flavorName, String serviceName); @Override HostIPsListWrapper getRandomHostForFlavorAndService(String flavorName, String serviceName); @Override Set<StackData> getAllStacksAndHosts(String serviceName); Boolean isStackNameWithoutFlavor(String stackName); @Override HostIPsListWrapper getAddressByStackOrFlavor(String appName, String stackName, String flavorName); @Override HostIPsListWrapper getRandomAddressByStackOrFlavor(String serviceName, String stackName, String flavorName); @Override void deleteStack(String serviceName, String dataCenter, String availabilityZone, String flavor); @Override synchronized void deleteStacks(Paths paths); void setWhiteListService(IWhiteListService whiteListService); void setInitialDelay(long initialDelay); void setRefreshDelay(long refreshDelay);  }