@Override
  public Image call() throws LayerPropertyNotFoundException {
    try (ProgressEventDispatcher ignored =
            progressEventDispatcherFactory.create("building image format", 1);
        TimerEventDispatcher ignored2 =
            new TimerEventDispatcher(buildContext.getEventHandlers(), DESCRIPTION)) {
      // Constructs the image.
      Image.Builder imageBuilder = Image.builder(buildContext.getTargetFormat());

      // Base image layers
      baseImageLayers.forEach(imageBuilder::addLayer);

      // Passthrough config and count non-empty history entries
      int nonEmptyLayerCount = 0;
      for (HistoryEntry historyObject : baseImage.getHistory()) {
        imageBuilder.addHistory(historyObject);
        if (!historyObject.hasCorrespondingLayer()) {
          nonEmptyLayerCount++;
        }
      }
      imageBuilder
          .setArchitecture(baseImage.getArchitecture())
          .setOs(baseImage.getOs())
          .addEnvironment(baseImage.getEnvironment())
          .addLabels(baseImage.getLabels())
          .setHealthCheck(baseImage.getHealthCheck())
          .addExposedPorts(baseImage.getExposedPorts())
          .addVolumes(baseImage.getVolumes())
          .setUser(baseImage.getUser())
          .setWorkingDirectory(baseImage.getWorkingDirectory());

      ContainerConfiguration containerConfiguration = buildContext.getContainerConfiguration();
      // Add history elements for non-empty layers that don't have one yet
      Instant layerCreationTime = containerConfiguration.getCreationTime();
      for (int count = 0; count < baseImageLayers.size() - nonEmptyLayerCount; count++) {
        imageBuilder.addHistory(
            HistoryEntry.builder()
                .setCreationTimestamp(layerCreationTime)
                .setComment("auto-generated by Jib")
                .build());
      }

      // Add built layers/configuration
      for (PreparedLayer applicationLayer : applicationLayers) {
        imageBuilder
            .addLayer(applicationLayer)
            .addHistory(
                HistoryEntry.builder()
                    .setCreationTimestamp(layerCreationTime)
                    .setAuthor("Jib")
                    .setCreatedBy(buildContext.getToolName() + ":" + buildContext.getToolVersion())
                    .setComment(applicationLayer.getName())
                    .build());
      }

      imageBuilder
          .addEnvironment(containerConfiguration.getEnvironmentMap())
          .setCreated(containerConfiguration.getCreationTime())
          .setEntrypoint(computeEntrypoint(baseImage, containerConfiguration))
          .setProgramArguments(computeProgramArguments(baseImage, containerConfiguration))
          .addExposedPorts(containerConfiguration.getExposedPorts())
          .addVolumes(containerConfiguration.getVolumes())
          .addLabels(containerConfiguration.getLabels());
      if (containerConfiguration.getUser() != null) {
        imageBuilder.setUser(containerConfiguration.getUser());
      }
      if (containerConfiguration.getWorkingDirectory() != null) {
        imageBuilder.setWorkingDirectory(containerConfiguration.getWorkingDirectory().toString());
      }

      // Gets the container configuration content descriptor.
      return imageBuilder.build();
    }
  }
@Test
  public void test_inheritedUser() {
    Mockito.when(mockContainerConfiguration.getUser()).thenReturn(null);

    Image image =
        new BuildImageStep(
                mockBuildContext,
                mockProgressEventDispatcherFactory,
                baseImage,
                baseImageLayers,
                applicationLayers)
            .call();

    Assert.assertEquals("base:user", image.getUser());
  }