InstanceConfigController { @GetMapping("/by-release") public PageDTO<InstanceDTO> getByRelease(@RequestParam("releaseId") long releaseId, Pageable pageable) { Release release = releaseService.findOne(releaseId); if (release == null) { throw new NotFoundException(String.format("release not found for %s", releaseId)); } Page<InstanceConfig> instanceConfigsPage = instanceService.findActiveInstanceConfigsByReleaseKey (release.getReleaseKey(), pageable); List<InstanceDTO> instanceDTOs = Collections.emptyList(); if (instanceConfigsPage.hasContent()) { Multimap<Long, InstanceConfig> instanceConfigMap = HashMultimap.create(); Set<String> otherReleaseKeys = Sets.newHashSet(); for (InstanceConfig instanceConfig : instanceConfigsPage.getContent()) { instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig); otherReleaseKeys.add(instanceConfig.getReleaseKey()); } Set<Long> instanceIds = instanceConfigMap.keySet(); List<Instance> instances = instanceService.findInstancesByIds(instanceIds); if (!CollectionUtils.isEmpty(instances)) { instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances); } for (InstanceDTO instanceDTO : instanceDTOs) { Collection<InstanceConfig> configs = instanceConfigMap.get(instanceDTO.getId()); List<InstanceConfigDTO> configDTOs = configs.stream().map(instanceConfig -> { InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO(); instanceConfigDTO.setRelease(null); instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime()); instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig .getDataChangeLastModifiedTime()); return instanceConfigDTO; }).collect(Collectors.toList()); instanceDTO.setConfigs(configDTOs); } } return new PageDTO<>(instanceDTOs, pageable, instanceConfigsPage.getTotalElements()); } InstanceConfigController(final ReleaseService releaseService, final InstanceService instanceService); @GetMapping("/by-release") PageDTO<InstanceDTO> getByRelease(@RequestParam("releaseId") long releaseId,
                                           Pageable pageable); @GetMapping("/by-namespace-and-releases-not-in") List<InstanceDTO> getByReleasesNotIn(@RequestParam("appId") String appId,
                                              @RequestParam("clusterName") String clusterName,
                                              @RequestParam("namespaceName") String namespaceName,
                                              @RequestParam("releaseIds") String releaseIds); @GetMapping("/by-namespace") PageDTO<InstanceDTO> getInstancesByNamespace(
      @RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName,
      @RequestParam("namespaceName") String namespaceName,
      @RequestParam(value = "instanceAppId", required = false) String instanceAppId,
      Pageable pageable); @GetMapping("/by-namespace/count") long getInstancesCountByNamespace(@RequestParam("appId") String appId,
                                          @RequestParam("clusterName") String clusterName,
                                          @RequestParam("namespaceName") String namespaceName);  }
@Test public void getByRelease() throws Exception { long someReleaseId = 1; long someInstanceId = 1; long anotherInstanceId = 2; String someReleaseKey = "someKey"; Release someRelease = new Release(); someRelease.setReleaseKey(someReleaseKey); String someAppId = "someAppId"; String anotherAppId = "anotherAppId"; String someCluster = "someCluster"; String someDataCenter = "someDC"; String someConfigAppId = "someConfigAppId"; String someConfigNamespace = "someNamespace"; String someIp = "someIp"; Date someReleaseDeliveryTime = new Date(); Date anotherReleaseDeliveryTime = new Date(); when(releaseService.findOne(someReleaseId)).thenReturn(someRelease); InstanceConfig someInstanceConfig = assembleInstanceConfig(someInstanceId, someConfigAppId, someConfigNamespace, someReleaseKey, someReleaseDeliveryTime); InstanceConfig anotherInstanceConfig = assembleInstanceConfig(anotherInstanceId, someConfigAppId, someConfigNamespace, someReleaseKey, anotherReleaseDeliveryTime); List<InstanceConfig> instanceConfigs = Lists.newArrayList(someInstanceConfig, anotherInstanceConfig); Page<InstanceConfig> instanceConfigPage = new PageImpl<>(instanceConfigs, pageable, instanceConfigs.size()); when(instanceService.findActiveInstanceConfigsByReleaseKey(someReleaseKey, pageable)) .thenReturn(instanceConfigPage); Instance someInstance = assembleInstance(someInstanceId, someAppId, someCluster, someDataCenter, someIp); Instance anotherInstance = assembleInstance(anotherInstanceId, anotherAppId, someCluster, someDataCenter, someIp); List<Instance> instances = Lists.newArrayList(someInstance, anotherInstance); Set<Long> instanceIds = Sets.newHashSet(someInstanceId, anotherInstanceId); when(instanceService.findInstancesByIds(instanceIds)) .thenReturn(instances); PageDTO<InstanceDTO> result = instanceConfigController.getByRelease(someReleaseId, pageable); assertEquals(2, result.getContent().size()); InstanceDTO someInstanceDto = null; InstanceDTO anotherInstanceDto = null; for (InstanceDTO instanceDTO : result.getContent()) { if (instanceDTO.getId() == someInstanceId) { someInstanceDto = instanceDTO; } else if (instanceDTO.getId() == anotherInstanceId) { anotherInstanceDto = instanceDTO; } } verifyInstance(someInstance, someInstanceDto); verifyInstance(anotherInstance, anotherInstanceDto); assertEquals(1, someInstanceDto.getConfigs().size()); assertEquals(someReleaseDeliveryTime, someInstanceDto.getConfigs().get(0).getReleaseDeliveryTime()); assertEquals(1, anotherInstanceDto.getConfigs().size()); assertEquals(anotherReleaseDeliveryTime, anotherInstanceDto.getConfigs().get(0).getReleaseDeliveryTime()); }
@Test(expected = NotFoundException.class) public void testGetByReleaseWhenReleaseIsNotFound() throws Exception { long someReleaseIdNotExists = 1; when(releaseService.findOne(someReleaseIdNotExists)).thenReturn(null); instanceConfigController.getByRelease(someReleaseIdNotExists, pageable); }