Repository: GoogleContainerTools/jib
File: jib-core/src/test/java/com/google/cloud/tools/jib/builder/steps/PlatformCheckerTest.java (Java)

Code before:
  @Mock private BuildContext buildContext;
  @Mock private ContainerConfiguration containerConfig;

  @Before
  public void setUp() {
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(ImageReference.scratch()).build());
    Mockito.when(buildContext.getContainerConfiguration()).thenReturn(containerConfig);
  }

  @Test
  public void testCheckManifestPlatform_mismatch() {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("configured arch", "configured OS")));

    ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
    containerConfigJson.setArchitecture("actual arch");
    containerConfigJson.setOs("actual OS");
    Exception ex =
        assertThrows(
            PlatformNotFoundInBaseImageException.class,
            () -> PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson));
    assertEquals(
        "the configured platform (configured arch/configured OS) doesn't match the "
            + "platform (actual arch/actual OS) of the base image (scratch)",
        ex.getMessage());
  }

  @Test
  public void testCheckManifestPlatform_noWarningIfDefaultAmd64Linux()
      throws PlatformNotFoundInBaseImageException {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("amd64", "linux")));

    ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
    containerConfigJson.setArchitecture("actual arch");
    containerConfigJson.setOs("actual OS");
    PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson);
  }

  @Test
  public void testCheckManifestPlatform_multiplePlatformsConfigured() {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arch", "os")));
    Exception ex =
        assertThrows(
            PlatformNotFoundInBaseImageException.class,
            () ->
                PlatformChecker.checkManifestPlatform(
                    buildContext, new ContainerConfigurationTemplate()));
    assertEquals(

Review comment (suggestion):
Perhaps we can use Truth here to assert on the message? Same below.

`import static com.google.common.truth.Truth.assertThat`
and `assertThat().hasMessageThat().isEqualTo()`

Code after:
  @Mock private BuildContext buildContext;
  @Mock private ContainerConfiguration containerConfig;

  @Before
  public void setUp() {
    Mockito.when(buildContext.getBaseImageConfiguration())
        .thenReturn(ImageConfiguration.builder(ImageReference.scratch()).build());
    Mockito.when(buildContext.getContainerConfiguration()).thenReturn(containerConfig);
  }

  @Test
  public void testCheckManifestPlatform_mismatch() {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("configured arch", "configured OS")));

    ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
    containerConfigJson.setArchitecture("actual arch");
    containerConfigJson.setOs("actual OS");
    Exception ex =
        assertThrows(
            PlatformNotFoundInBaseImageException.class,
            () -> PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson));
    assertThat(ex)
        .hasMessageThat()
        .isEqualTo(
            "the configured platform (configured arch/configured OS) doesn't match the "
                + "platform (actual arch/actual OS) of the base image (scratch)");
  }

  @Test
  public void testCheckManifestPlatform_noExceptionIfDefaultAmd64Linux()
      throws PlatformNotFoundInBaseImageException {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("amd64", "linux")));

    ContainerConfigurationTemplate containerConfigJson = new ContainerConfigurationTemplate();
    containerConfigJson.setArchitecture("actual arch");
    containerConfigJson.setOs("actual OS");
    PlatformChecker.checkManifestPlatform(buildContext, containerConfigJson);
  }

  @Test
  public void testCheckManifestPlatform_multiplePlatformsConfigured() {
    Mockito.when(containerConfig.getPlatforms())
        .thenReturn(ImmutableSet.of(new Platform("amd64", "linux"), new Platform("arch", "os")));
    Exception ex =
        assertThrows(
            PlatformNotFoundInBaseImageException.class,
            () ->
                PlatformChecker.checkManifestPlatform(
                    buildContext, new ContainerConfigurationTemplate()));