GetEnvironmentsUtilImpl implements GetEnvironmentsUtil { @NotNull @Override public List<Environment> getEnvironmentsByProjectType(@NotNull RunnerEnvironmentTree tree, @NotNull String projectType, @NotNull Scope scope) { List<RunnerEnvironmentLeaf> leaves = new ArrayList<>(); CurrentProject currentProject = appContext.getCurrentProject(); if (currentProject == null) { return new ArrayList<>(); } String defaultRunner = currentProject.getRunner(); if (defaultRunner == null || defaultRunner.startsWith(DEFAULT_RUNNER_PROJECT_TYPE) || PROJECT.equals(scope)) { RunnerEnvironmentTree categoryNode = getRunnerCategoryByProjectType(tree, projectType); leaves = getAllEnvironments(categoryNode); } else { String category = getCorrectCategoryName(defaultRunner); String[] nodeNames = category.split("/"); for (String nodeName : nodeNames) { tree = tree.getNode(nodeName); } leaves.addAll(getAllEnvironments(tree)); } return getEnvironmentsFromNodes(leaves, scope); } @Inject  GetEnvironmentsUtilImpl(ModelsFactory modelsFactory, ProjectTypeRegistry projectTypeRegistry, AppContext appContext); @NotNull @Override List<RunnerEnvironmentLeaf> getAllEnvironments(@NotNull RunnerEnvironmentTree tree); @NotNull @Override List<Environment> getEnvironmentsFromNodes(@NotNull List<RunnerEnvironmentLeaf> leaves, @NotNull Scope scope); @NotNull @Override List<Environment> getEnvironmentsByProjectType(@NotNull RunnerEnvironmentTree tree,
                                                          @NotNull String projectType,
                                                          @NotNull Scope scope); @Override @NotNull String getType(); @NotNull @Override String getCorrectCategoryName(@NotNull String defaultRunner); @NotNull @Override RunnerEnvironmentTree getRunnerCategoryByProjectType(@NotNull RunnerEnvironmentTree tree, @NotNull String projectType);  }
@Test public void emptyListShouldBeReturnedWhenCurrentProjectIsNull() { when(appContext.getCurrentProject()).thenReturn(null); List<Environment> environments = util.getEnvironmentsByProjectType(tree, PROJECT_TYPE, SYSTEM); assertThat(environments.isEmpty(), is(true)); }
@Test public void systemEnvironmentsShouldBeReturnedWhenDefaultRunnerIsNull() throws Exception { when(currentProject.getRunner()).thenReturn(null); when(tree.getNode(PROJECT_TYPE)).thenReturn(tree); when(runnerEnvLeaf4.getEnvironment()).thenReturn(runEnvironment1); when(runnerEnvLeaf5.getEnvironment()).thenReturn(runEnvironment2); when(modelsFactory.createEnvironment(runEnvironment1, SYSTEM)).thenReturn(environment1); when(modelsFactory.createEnvironment(runEnvironment2, SYSTEM)).thenReturn(environment2); when(environment1.compareTo(environment2)).thenReturn(1); when(environment2.compareTo(environment1)).thenReturn(2); List<Environment> environments = util.getEnvironmentsByProjectType(tree, PROJECT_TYPE, SYSTEM); verify(projectTypeRegistry).getProjectType(PROJECT_TYPE); verify(definition).getRunnerCategories(); verify(tree).getNode("category1"); assertThat(environments.size(), is(2)); }
@Test public void environmentsShouldBeReturnedWhenScopeIsProject() throws Exception { when(runnerEnvLeaf4.getEnvironment()).thenReturn(runEnvironment1); when(runnerEnvLeaf5.getEnvironment()).thenReturn(runEnvironment2); when(modelsFactory.createEnvironment(runEnvironment1, PROJECT)).thenReturn(environment1); when(modelsFactory.createEnvironment(runEnvironment2, PROJECT)).thenReturn(environment2); when(environment1.compareTo(environment2)).thenReturn(1); when(environment2.compareTo(environment1)).thenReturn(2); List<Environment> environments = util.getEnvironmentsByProjectType(tree, PROJECT_TYPE, PROJECT); verify(projectTypeRegistry).getProjectType(PROJECT_TYPE); verify(definition).getRunnerCategories(); verify(tree).getNode("category1"); assertThat(environments.size(), is(2)); }
@Test public void environmentsShouldBeReturnedWhenProjectScopeIsSystem() throws Exception { when(currentProject.getRunner()).thenReturn("text/text1/text2/text"); when(tree.getNode("text1")).thenReturn(runnerEnvTree6); when(runnerEnvTree6.getNode("text2")).thenReturn(runnerEnvTree5); when(runnerEnvLeaf4.getEnvironment()).thenReturn(runEnvironment1); when(runnerEnvLeaf5.getEnvironment()).thenReturn(runEnvironment2); when(runnerEnvLeaf6.getEnvironment()).thenReturn(runEnvironment3); when(modelsFactory.createEnvironment(runEnvironment1, SYSTEM)).thenReturn(environment1); when(modelsFactory.createEnvironment(runEnvironment2, SYSTEM)).thenReturn(environment2); when(modelsFactory.createEnvironment(runEnvironment3, SYSTEM)).thenReturn(environment3); when(environment1.compareTo(environment2)).thenReturn(1); when(environment2.compareTo(environment1)).thenReturn(2); when(environment3.compareTo(environment1)).thenReturn(-1); when(environment3.compareTo(environment2)).thenReturn(5); List<Environment> environments = util.getEnvironmentsByProjectType(tree, PROJECT_TYPE, SYSTEM); assertThat(environments.size(), is(3)); }
@Test public void environmentsShouldBeReturnedWhenDefaultEnvironmentHasProjectScope() throws Exception { when(currentProject.getRunner()).thenReturn(DEFAULT_RUNNER_PROJECT_TYPE + "text1/text2/text"); when(tree.getNode("text1")).thenReturn(runnerEnvTree6); when(runnerEnvTree6.getNode("text2")).thenReturn(runnerEnvTree5); when(runnerEnvLeaf4.getEnvironment()).thenReturn(runEnvironment1); when(runnerEnvLeaf5.getEnvironment()).thenReturn(runEnvironment2); when(runnerEnvLeaf6.getEnvironment()).thenReturn(runEnvironment3); when(modelsFactory.createEnvironment(runEnvironment1, SYSTEM)).thenReturn(environment1); when(modelsFactory.createEnvironment(runEnvironment2, SYSTEM)).thenReturn(environment2); when(modelsFactory.createEnvironment(runEnvironment3, SYSTEM)).thenReturn(environment3); when(environment1.compareTo(environment2)).thenReturn(1); when(environment2.compareTo(environment1)).thenReturn(2); when(environment3.compareTo(environment1)).thenReturn(-1); when(environment3.compareTo(environment2)).thenReturn(5); List<Environment> environments = util.getEnvironmentsByProjectType(tree, PROJECT_TYPE, SYSTEM); assertThat(environments.size(), is(2)); }