/*******************************************************************************
* Copyright 2019 Graz University of Technology
* MOA ZS has been developed in a cooperation between EGIZ
* and Graz University of Technology.
* Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
* the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use this work except in compliance with the Licence.
* You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/news/understanding-eupl-v12
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* This product combines work with different licenses. See the "NOTICE" text
* file for details on the various modules and licenses.
* The "NOTICE" text file is part of the distribution. Any derivative works
* that you distribute must include a readable copy of the "NOTICE" text file.
*******************************************************************************/
package at.gv.egiz.moazs;
import at.gv.egiz.moazs.backend.SaveResponseToFileSink;
import at.gv.egiz.moazs.repository.DeliveryRepository;
import at.gv.egiz.moazs.scheme.Marshaller;
import at.gv.egiz.moazs.scheme.RequestStatusResponse;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.task.TaskExecutor;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.concurrent.ForkJoinPool;
import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Success.successBuilder;
import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder;
import static java.util.Optional.of;
import static org.apache.commons.io.FileUtils.readFileToString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* @author Christof Rabensteiner
*
*/
@RunWith(MockitoJUnitRunner.class)
public class SaveResponseToFileSinkTest {
private static final Logger log = LoggerFactory.getLogger(SaveResponseToFileSinkTest.class);
private SaveResponseToFileSink sink;
private final String root = "./target/tmp/SaveResponseToFileSinkTestOut";
@Mock
private DeliveryRepository repository;
@Mock
private Marshaller marshaller;
@Before
public void setup() {
sink = new SaveResponseToFileSink(marshaller, repository, ForkJoinPool.commonPool());
deleteRoot();
}
private void deleteRoot() {
try {
FileUtils.deleteDirectory(new File(root));
} catch (IOException e) {
log.warn("Could not delete {}", root);
}
}
@Test
public void saveRequestToFiles() {
var fileContent = "some content";
var status = setupMocks(fileContent);
sink.save(status, root)
.thenRun(() -> assertFilesCreatedAndContentMatches(fileContent));
}
private void assertFilesCreatedAndContentMatches(String fileContent) {
var rootFolder = new File(root);
Collection files = FileUtils.listFiles(rootFolder, null, true);
assertThat(rootFolder.exists()).isTrue();
assertThat(rootFolder.isDirectory()).isTrue();
assertThat(files).isNotEmpty();
files.stream()
.map(file -> readFile(file))
.forEach(content -> assertThat(content).isEqualTo(fileContent));
}
private String readFile(File file) {
try {
return readFileToString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private RequestStatusResponse setupMocks(String fileContent) {
var appDeliveryID = "app-delivery-id";
var responseID = RequestStatusResponse.getResponseID(appDeliveryID);
when(repository.retrieveBinaryResponse(responseID))
.thenReturn(of(fileContent.getBytes(StandardCharsets.UTF_8)));
when(marshaller.marshallXml(any())).thenReturn(fileContent);
var success = successBuilder()
.withAppDeliveryID(appDeliveryID)
.build();
var status = deliveryRequestStatusTypeBuilder()
.withSuccess(success)
.build();
return new RequestStatusResponse(status);
}
}