public static ImageReference getGeneratedTargetDockerTag(
      @Nullable String targetImage,
      ProjectProperties projectProperties,
      HelpfulSuggestions helpfulSuggestions)
      throws InvalidImageReferenceException {
    String generatedName = projectProperties.getName();
    String generatedTag =
        projectProperties.getVersion().equals("unspecified")
            ? "latest"
            : projectProperties.getVersion();
    if (Strings.isNullOrEmpty(targetImage)) {
      projectProperties.log(
          LogEvent.lifecycle(helpfulSuggestions.forGeneratedTag(generatedName, generatedTag)));

      // Try to parse generated tag to verify that project name and version are valid (throws an
      // exception if parse fails)
      ImageReference.parse(generatedName + ":" + generatedTag);
      return ImageReference.of(null, generatedName, generatedTag);
    } else {
      return ImageReference.parse(targetImage);
    }
  }
@Test
  public void testGetGeneratedTargetDockerTag() throws InvalidImageReferenceException {
    HelpfulSuggestions helpfulSuggestions =
        new HelpfulSuggestions("", "", "to", "--to", "build.txt");

    // Target configured
    ProjectProperties mockProjectProperties = mock(ProjectProperties.class);
    when(mockProjectProperties.getName()).thenReturn("project-name");
    when(mockProjectProperties.getVersion()).thenReturn("project-version");

    ImageReference result =
        ConfigurationPropertyValidator.getGeneratedTargetDockerTag(
            "a/b:c", mockProjectProperties, helpfulSuggestions);
    assertThat(result.getRepository()).isEqualTo("a/b");
    assertThat(result.getTag()).hasValue("c");
    verify(mockLogger, never()).accept(LogEvent.lifecycle(any()));

    // Target not configured
    result =
        ConfigurationPropertyValidator.getGeneratedTargetDockerTag(
            null, mockProjectProperties, helpfulSuggestions);
    assertThat(result.getRepository()).isEqualTo("project-name");
    assertThat(result.getTag()).hasValue("project-version");
    verify(mockProjectProperties)
        .log(
            LogEvent.lifecycle(
                "Tagging image with generated image reference project-name:project-version. If you'd "
                    + "like to specify a different tag, you can set the to parameter in your "
                    + "build.txt, or use the --to=<MY IMAGE> commandline flag."));

    // Generated tag invalid
    when(mockProjectProperties.getName()).thenReturn("%#&///*@(");
    when(mockProjectProperties.getVersion()).thenReturn("%$#//&*@($");
    assertThrows(
        InvalidImageReferenceException.class,
        () ->
            ConfigurationPropertyValidator.getGeneratedTargetDockerTag(
                null, mockProjectProperties, helpfulSuggestions));
  }