### Question:
RuntimeManagementServiceCDI implements RuntimeManagementService { @Override public Collection<Container> getContainersByServerInstance(final String serverTemplateId, final String serverInstanceId) { final ServerTemplate serverTemplate = specManagementService.getServerTemplate(serverTemplateId); if (serverTemplate == null) { throw new RuntimeException("Server template with id " + serverTemplateId + " not found"); } return serverTemplate.getServerInstanceKeys().stream() .filter(serverInstanceKey -> serverInstanceKey.getServerInstanceId().equalsIgnoreCase(serverInstanceId)) .findFirst() .map(serverInstanceKey -> Arrays.asList(service.getContainers(serverInstanceKey).getContainers())) .orElse(Collections.emptyList()); }  @Override Collection<Container> getContainersByServerInstance(final String serverTemplateId,
                                                               final String serverInstanceId); @Override ContainerSpecData getContainersByContainerSpec(final String serverTemplateId,
                                                          final String containerSpecId);  }### Answer:
@Test public void getContainersByServerInstance_throwsRuntimeException_whenServerTemplateNotFound() { final String templateId = "this_template_does_NOT_exist"; expectedException.expect(RuntimeException.class); expectedException.expectMessage(containsString("No server template found for id " + templateId)); runtimeManagementServiceCDI.getContainersByServerInstance(templateId, "dummy_container_spec_id"); }
@Test public void getContainersByServerInstance_returnsEmptyList_whenInstanceWithIdDoesntExistInTemplate() { final String templateId = "templateId"; final String serverInstanceId = "serverInstanceId"; KieServerTemplateStorage templateStorageMock = mock(KieServerTemplateStorage.class); when(templateStorageMock.load(eq(templateId))) .thenReturn(new ServerTemplate(null, null, Collections.emptyList(), Collections.emptyMap(), Collections.emptyList(), Collections.emptyList())); runtimeManagementService.setTemplateStorage(templateStorageMock); specManagementService.setTemplateStorage(templateStorageMock); Collection<Container> containers = runtimeManagementServiceCDI.getContainersByServerInstance(templateId, serverInstanceId); assertThat(containers) .as("List of containers should be empty, when Server template doesn't contain server instance id") .isEmpty(); }
@Test public void getContainersByServerInstance_returnsListOfContainers_whenInstanceWithIdExists() { final String templateId = "templateId"; final String templateName = "templateName"; final String serverInstanceId = "serverInstanceId"; ServerInstanceKey serverInstanceKey = new ServerInstanceKey(templateId, templateName, serverInstanceId, "dummyUrl"); ServerTemplate serverTemplate = new ServerTemplate( templateId, templateName, Collections.emptyList(), Collections.emptyMap(), Collections.emptyList(), Collections.singletonList(serverInstanceKey) ); final List<Container> containersInServerInstance = Arrays.asList(new Container(), new Container()); KieServerTemplateStorage templateStorageMock = createMockStorageWithOneTemplate(serverTemplate); KieServerInstanceManager instanceMangerMock = mock(KieServerInstanceManager.class); when(instanceMangerMock.getContainers(eq(serverInstanceKey))) .thenReturn(containersInServerInstance); runtimeManagementService.setTemplateStorage(templateStorageMock); runtimeManagementService.setKieServerInstanceManager(instanceMangerMock); specManagementService.setTemplateStorage(templateStorageMock); specManagementService.setKieServerInstanceManager(instanceMangerMock); Collection<Container> containers = runtimeManagementServiceCDI.getContainersByServerInstance(templateId, serverInstanceId); assertThat(containers) .as("Should return list of containers from server instance id") .hasSameSizeAs(containersInServerInstance); }