NameGenerator { @NotNull public static String generateCopy(@NotNull String name, @NotNull List<Environment> projectEnvironments) { List<String> existingNames = new ArrayList<>(); for (Environment environment : projectEnvironments) { existingNames.add(environment.getName()); } name = removeCopyPrefix(name); name = name.replace("+", ""); String copyName = "Copy of ".concat(name); boolean alreadyExists = existingNames.contains(copyName); int index = 2; while (alreadyExists) { copyName = "Copy".concat(String.valueOf(index)).concat(" of ").concat(name); alreadyExists = existingNames.contains(copyName); index++; } return copyName.endsWith("++") ? copyName.replace("++", "") : copyName; } private  NameGenerator(); @NotNull static String generateCopy(@NotNull String name, @NotNull List<Environment> projectEnvironments); static String generateCustomEnvironmentName(@NotNull List<Environment> environments, @NotNull String projectName);  }
@Test public void generateFirstName() { when(environment1.getName()).thenReturn(HELLO); String generated = NameGenerator.generateCopy(HELLO, Arrays.asList(environment1)); String expectedName = "Copy of hello"; assertEquals(expectedName, generated); }
@Test public void generateAlreadyExistsFirstName() { when(environment1.getName()).thenReturn(COPY_HELLO); String generated = NameGenerator.generateCopy(HELLO, Arrays.asList(environment1)); assertEquals(COPY2_HELLO, generated); }
@Test public void cutPlusPlusWhenCreateCopyFromCppEnvironment() { when(environment1.getName()).thenReturn(HELLO + "++"); String generated = NameGenerator.generateCopy(HELLO, Arrays.asList(environment1)); assertEquals(COPY_HELLO, generated); }
@Test public void generateAlreadyExistsTwiceName() { when(environment1.getName()).thenReturn(COPY_HELLO); when(environment2.getName()).thenReturn(COPY2_HELLO); String generated = NameGenerator.generateCopy(HELLO, Arrays.asList(environment1, environment2)); assertEquals(COPY3_HELLO, generated); }
@Test public void generateCopyOfCopy() { when(environment1.getName()).thenReturn(COPY_HELLO); when(environment2.getName()).thenReturn(COPY2_HELLO); when(environment3.getName()).thenReturn(COPY3_HELLO); String generated = NameGenerator.generateCopy(COPY3_HELLO, Arrays.asList(environment1, environment2, environment3)); assertEquals(COPY4_HELLO, generated); }