Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Create runner tests #604

Merged
merged 5 commits into from
Jun 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package app.coronawarn.server.services.distribution.objectstore;

import static java.util.Map.entry;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.atLeastOnce;
Expand All @@ -38,7 +40,6 @@
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand Down Expand Up @@ -90,6 +91,14 @@ private LocalFile setUpLocalFileMock() {
return testLocalFile;
}

@Test
void illegalArgumentExceptionIfBucketDoesNotExist() {
when(objectStoreClient.bucketExists(any())).thenReturn(false);

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> new ObjectStoreAccess(distributionServiceConfig, objectStoreClient));
}

@Test
void testPutObjectSetsDefaultCacheControlHeader() {
ArgumentCaptor<Map<HeaderKey, String>> headers = ArgumentCaptor.forClass(Map.class);
Expand All @@ -99,7 +108,7 @@ void testPutObjectSetsDefaultCacheControlHeader() {

verify(objectStoreClient, atLeastOnce())
.putObject(eq(expBucketName), eq(EXP_S3_KEY), eq(expPath), headers.capture());
Assertions.assertThat(headers.getValue()).contains(expHeader);
assertThat(headers.getValue()).contains(expHeader);
}

@Test
Expand All @@ -112,7 +121,7 @@ void testPutObjectSetsSpecifiedCacheControlHeader() {

verify(objectStoreClient, atLeastOnce())
.putObject(eq(expBucketName), eq(EXP_S3_KEY), eq(expPath), headers.capture());
Assertions.assertThat(headers.getValue()).contains(expHeader);
assertThat(headers.getValue()).contains(expHeader);
}

@Test
Expand All @@ -127,7 +136,7 @@ void putObjectSetsSpecifiedFileChecksum() {

verify(objectStoreClient, atLeastOnce())
.putObject(eq(expBucketName), eq(EXP_S3_KEY), eq(expPath), headers.capture());
Assertions.assertThat(headers.getValue()).contains(expHeader);
assertThat(headers.getValue()).contains(expHeader);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* ---license-start
* Corona-Warn-App
* ---
* Copyright (C) 2020 SAP SE and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/

package app.coronawarn.server.services.distribution.runner;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import app.coronawarn.server.services.distribution.assembly.component.CwaApiStructureProvider;
import app.coronawarn.server.services.distribution.assembly.component.OutputDirectoryProvider;
import app.coronawarn.server.services.distribution.assembly.structure.WritableOnDisk;
import app.coronawarn.server.services.distribution.assembly.structure.directory.Directory;
import app.coronawarn.server.services.distribution.assembly.structure.directory.DirectoryOnDisk;
import app.coronawarn.server.services.distribution.config.DistributionServiceConfig;
import java.io.IOException;
import org.junit.Rule;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.rules.TemporaryFolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@EnableConfigurationProperties(value = DistributionServiceConfig.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {Assembly.class}, initializers = ConfigFileApplicationContextInitializer.class)
class AssemblyRunnerTest {

@MockBean
OutputDirectoryProvider outputDirectoryProvider;

@MockBean
CwaApiStructureProvider cwaApiStructureProvider;

@Autowired
Assembly assembly;

private Directory<WritableOnDisk> parentDirectory;
private Directory<WritableOnDisk> childDirectory;

@Rule
private TemporaryFolder outputFolder = new TemporaryFolder();

@BeforeEach
void setup() throws IOException {
outputFolder.create();
var outputDirectory = outputFolder.newFolder("parent");
var outputSubDirectory = outputFolder.newFolder("parent/child");
parentDirectory = new DirectoryOnDisk(outputDirectory);
childDirectory = new DirectoryOnDisk(outputSubDirectory);
}

@Test
void shouldCorrectlyCreatePrepareAndWriteDirectories() throws IOException {
Directory<WritableOnDisk> spyParentDirectory = spy(parentDirectory);

when(outputDirectoryProvider.getDirectory()).thenReturn(spyParentDirectory);
when(cwaApiStructureProvider.getDirectory()).thenReturn(childDirectory);

assembly.run(null);

verify(outputDirectoryProvider, times(1)).getDirectory();
verify(outputDirectoryProvider, times(1)).clear();
verify(cwaApiStructureProvider, times(1)).getDirectory();
verify(spyParentDirectory, times(1)).prepare(any());
verify(spyParentDirectory, times(1)).write();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* ---license-start
* Corona-Warn-App
* ---
* Copyright (C) 2020 SAP SE and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/

package app.coronawarn.server.services.distribution.runner;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import app.coronawarn.server.common.persistence.service.DiagnosisKeyService;
import app.coronawarn.server.services.distribution.config.DistributionServiceConfig;
import app.coronawarn.server.services.distribution.objectstore.S3RetentionPolicy;
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.ConfigFileApplicationContextInitializer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@EnableConfigurationProperties(value = DistributionServiceConfig.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {RetentionPolicy.class}, initializers = ConfigFileApplicationContextInitializer.class)
class RetentionPolicyRunnerTest {

@MockBean
DiagnosisKeyService diagnosisKeyService;

@MockBean
S3RetentionPolicy s3RetentionPolicy;

@Autowired
DistributionServiceConfig distributionServiceConfig;

@Autowired
RetentionPolicy retentionPolicy;

@Test
void shouldCallDatabaseAndS3RetentionRunner() {
retentionPolicy.run(null);

verify(diagnosisKeyService, times(1)).applyRetentionPolicy(distributionServiceConfig.getRetentionDays());
verify(s3RetentionPolicy, times(1)).applyRetentionPolicy(distributionServiceConfig.getRetentionDays());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* ---license-start
* Corona-Warn-App
* ---
* Copyright (C) 2020 SAP SE and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/

package app.coronawarn.server.services.distribution.runner;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import app.coronawarn.server.services.distribution.assembly.component.OutputDirectoryProvider;
import app.coronawarn.server.services.distribution.config.DistributionServiceConfig;
import app.coronawarn.server.services.distribution.objectstore.S3Publisher;
import java.io.IOException;
import java.nio.file.Paths;
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.ConfigFileApplicationContextInitializer;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@EnableConfigurationProperties(value = DistributionServiceConfig.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {S3Distribution.class}, initializers = ConfigFileApplicationContextInitializer.class)
class S3DistributionRunnerTest {

@MockBean
OutputDirectoryProvider outputDirectoryProvider;

@MockBean
S3Publisher s3Publisher;

@Autowired
S3Distribution s3Distribution;

@Test
void shouldPublishCorrectFolder() throws IOException {
var outputPath = Paths.get("test", "mock", "folder");
var outputFile = new java.io.File(String.valueOf(outputPath));

when(outputDirectoryProvider.getFileOnDisk()).thenReturn(outputFile);

s3Distribution.run(null);

verify(s3Publisher, times(1)).publish(outputPath.toAbsolutePath());
}
}