FilterUtils { public static List<ComponentContainers> filterInstances( ServiceContext context, ClientAMProtocol.GetCompInstancesRequestProto filterReq) { Map<String, ComponentContainers> containersByComp = new HashMap<>(); Map<ContainerId, ComponentInstance> instances = context.scheduler.getLiveInstances(); instances.forEach(((containerId, instance) -> { boolean include = true; if (filterReq.getComponentNamesList() != null && !filterReq.getComponentNamesList().isEmpty()) { if (!filterReq.getComponentNamesList().contains( instance.getComponent().getName())) { include = false; } } if (filterReq.getVersion() != null && !filterReq.getVersion().isEmpty()) { String instanceServiceVersion = instance.getServiceVersion(); if (instanceServiceVersion == null || !instanceServiceVersion.equals( filterReq.getVersion())) { include = false; } } if (filterReq.getContainerStatesList() != null && !filterReq.getContainerStatesList().isEmpty()) { if (!filterReq.getContainerStatesList().contains( instance.getContainerState().toString())) { include = false; } } if (include) { ComponentContainers compContainers = containersByComp.computeIfAbsent(instance.getCompName(), k -> { ComponentContainers result = new ComponentContainers(); result.setContainers(new ArrayList<>()); result.setComponentName(instance.getCompName()); return result; }); compContainers.addContainer(instance.getContainerSpec()); } })); List<ComponentContainers> result = new ArrayList<>(); result.addAll(containersByComp.values()); return result; }  static List<ComponentContainers> filterInstances(
      ServiceContext context,
      ClientAMProtocol.GetCompInstancesRequestProto filterReq);  }
@Test public void testNoFilter() throws Exception { GetCompInstancesRequestProto req = GetCompInstancesRequestProto.newBuilder() .build(); List<ComponentContainers> compContainers = FilterUtils.filterInstances( new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")), req); Assert.assertEquals("num comps", 2, compContainers.size()); compContainers.forEach(item -> { Assert.assertEquals("num containers", 2, item.getContainers().size()); }); }
@Test public void testFilterWithComp() throws Exception { GetCompInstancesRequestProto req = GetCompInstancesRequestProto.newBuilder() .addAllComponentNames(Lists.newArrayList("compa")).build(); List<ComponentContainers> compContainers = FilterUtils.filterInstances( new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")), req); Assert.assertEquals("num comps", 1, compContainers.size()); Assert.assertEquals("comp name", "compa", compContainers.get(0).getComponentName()); Assert.assertEquals("num containers", 2, compContainers.get(0).getContainers().size()); }
@Test public void testFilterWithVersion() throws Exception { ServiceContext sc = new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")); GetCompInstancesRequestProto.Builder reqBuilder = GetCompInstancesRequestProto.newBuilder(); reqBuilder.setVersion("v2"); Assert.assertEquals("num comps", 0, FilterUtils.filterInstances(sc, reqBuilder.build()).size()); reqBuilder.addAllComponentNames(Lists.newArrayList("compa")) .setVersion("v1").build(); Assert.assertEquals("num containers", 2, FilterUtils.filterInstances(sc, reqBuilder.build()).get(0) .getContainers().size()); }
@Test public void testFilterWithState() throws Exception { ServiceContext sc = new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")); GetCompInstancesRequestProto.Builder reqBuilder = GetCompInstancesRequestProto.newBuilder(); reqBuilder.addAllContainerStates(Lists.newArrayList( ContainerState.READY.toString())); List<ComponentContainers> compContainers = FilterUtils.filterInstances(sc, reqBuilder.build()); Assert.assertEquals("num comps", 2, compContainers.size()); compContainers.forEach(item -> { Assert.assertEquals("num containers", 2, item.getContainers().size()); }); reqBuilder.clearContainerStates(); reqBuilder.addAllContainerStates(Lists.newArrayList( ContainerState.STOPPED.toString())); Assert.assertEquals("num comps", 0, FilterUtils.filterInstances(sc, reqBuilder.build()).size()); }