FilterUtils { public static List<Container> filterInstances(ServiceContext context, ClientAMProtocol.GetCompInstancesRequestProto filterReq) { List<Container> results = new ArrayList<>(); 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) { results.add(instance.getContainerSpec()); } })); return results; }  static List<Container> filterInstances(ServiceContext context,
      ClientAMProtocol.GetCompInstancesRequestProto filterReq);  }
@Test public void testNoFilter() throws Exception { GetCompInstancesRequestProto req = GetCompInstancesRequestProto.newBuilder() .build(); List<Container> containers = FilterUtils.filterInstances( new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")), req); Assert.assertEquals("num containers", 4, containers.size()); }
@Test public void testFilterWithComp() throws Exception { GetCompInstancesRequestProto req = GetCompInstancesRequestProto.newBuilder() .addAllComponentNames(Lists.newArrayList("compa")).build(); List<Container> containers = FilterUtils.filterInstances( new MockRunningServiceContext(rule, TestServiceManager.createBaseDef("service")), req); Assert.assertEquals("num containers", 2, containers.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 containers", 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()).size()); reqBuilder.setVersion("v2").build(); Assert.assertEquals("num containers", 0, FilterUtils.filterInstances(sc, reqBuilder.build()).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())); Assert.assertEquals("num containers", 4, FilterUtils.filterInstances(sc, reqBuilder.build()).size()); reqBuilder.clearContainerStates(); reqBuilder.addAllContainerStates(Lists.newArrayList( ContainerState.STOPPED.toString())); Assert.assertEquals("num containers", 0, FilterUtils.filterInstances(sc, reqBuilder.build()).size()); }