Repository: corona-warn-app/cwa-server
File: services/distribution/src/test/java/app/coronawarn/server/services/distribution/assembly/component/BoosterNotificationStructureProviderTest.java (Java)

Code before:
  @MockBean
  DigitalSigningCertificatesClient digitalSigningCertificatesClient;

  @Rule
  TemporaryFolder testOutputFolder = new TemporaryFolder();

  @BeforeEach
  public void setup() throws IOException {
    // create a specific test folder for later assertions of structures.
    testOutputFolder.create();
    File outputDirectory = testOutputFolder.newFolder(PARENT_TEST_FOLDER);
    Directory<WritableOnDisk> testDirectory = new DirectoryOnDisk(outputDirectory);
    when(outputDirectoryProvider.getDirectory()).thenReturn(testDirectory);
  }

  @Test
  void should_contain_booster_notification_rules() throws FetchBusinessRulesException {
    when(digitalCovidCertificateClient.getBnRules()).thenThrow(FetchBusinessRulesException.class);

    Writable<WritableOnDisk> boosterNotification = getBoosterNotificationStructure();

    assertThat(boosterNotification.getName()).isEqualTo("booster-notification-rules");
  }

  private Writable<WritableOnDisk> getBoosterNotificationStructure() {
    BoosterNotificationStructureProvider underTest = new BoosterNotificationStructureProvider(
        distributionServiceConfig, cryptoProvider, dgcToCborMappingMock);
    Writable<WritableOnDisk> digitalGreenCertificates = underTest.getBoosterNotificationRules();

    return digitalGreenCertificates;
  }
}


Review comment (suggestion):
Is there any reason why this is constructed manually?  Usually we can also inject beans here, which makes it easier to see which class is tested

Code after:
package app.coronawarn.server.services.distribution.assembly.component;


import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import app.coronawarn.server.common.shared.collection.ImmutableStack;
import app.coronawarn.server.services.distribution.assembly.structure.Writable;
import app.coronawarn.server.services.distribution.assembly.structure.WritableOnDisk;
import app.coronawarn.server.services.distribution.assembly.structure.archive.Archive;
import app.coronawarn.server.services.distribution.config.DistributionServiceConfig;
import app.coronawarn.server.services.distribution.dgc.DigitalGreenCertificateToCborMapping;
import app.coronawarn.server.services.distribution.dgc.client.DigitalCovidCertificateClient;
import app.coronawarn.server.services.distribution.dgc.dsc.DigitalSigningCertificatesClient;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.ConfigDataApplicationContextInitializer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@EnableConfigurationProperties(value = {DistributionServiceConfig.class})
@ExtendWith(SpringExtension.class)
@ContextConfiguration(
    classes = {DigitalGreenCertificateToCborMapping.class,
        CryptoProvider.class, DistributionServiceConfig.class,
        DigitalSigningCertificatesClient.class, DigitalCovidCertificateClient.class,
        BoosterNotificationStructureProvider.class},
    initializers = ConfigDataApplicationContextInitializer.class)
@ActiveProfiles({"fake-dcc-client", "fake-dsc-client"})
class BoosterNotificationStructureProviderTest {

  @Autowired
  DistributionServiceConfig distributionServiceConfig;

  @Autowired
  CryptoProvider cryptoProvider;

  @Autowired
  DigitalGreenCertificateToCborMapping dgcToCborMappingMock;

  @MockBean
  DigitalCovidCertificateClient digitalCovidCertificateClient;

  @MockBean
  DigitalSigningCertificatesClient digitalSigningCertificatesClient;

  @Autowired
  BoosterNotificationStructureProvider underTest;

  @Test
  void should_create_correct_file_structure_for_booster_notification_business_rules() {
    Writable<WritableOnDisk> boosterNotificationRules = underTest.getBoosterNotificationRules();
    boosterNotificationRules.prepare(new ImmutableStack<>());

    assertEquals("booster-notification-rules", boosterNotificationRules.getName());

    Collection<String> archiveContent;
    archiveContent = ((Archive<WritableOnDisk>) boosterNotificationRules).getWritables().stream()
        .map(Writable::getName).collect(Collectors.toList());
    assertThat(archiveContent).containsAll(Set.of("export.bin", "export.sig"));
  }
}