[BP-843] feat: add utility services

* added MailHelper to set mail parameters and send mail
* added ArchiveHelper to copy files
* added test cases for utility services
* added empty mail.properties config file

Signed-off-by: Tobias Stummer <stummer@develop-group.de>
diff --git a/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/ArchiveHelper.java b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/ArchiveHelper.java
new file mode 100644
index 0000000..8b3ff77
--- /dev/null
+++ b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/ArchiveHelper.java
@@ -0,0 +1,29 @@
+/* *******************************************************************************
+ * Copyright (c) 2020 Basys GmbH
+ * 
+ * See the NOTICE file(s) distributed with this work for additional 
+ * information regarding copyright ownership.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v. 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ * *******************************************************************************/
+package org.eclipse.openk.sp.util;
+
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.StandardCopyOption;
+
+import org.springframework.stereotype.Service;
+
+@Service
+public class ArchiveHelper {
+
+	public void copyFile(String srcPathString, String dstPathString) throws IOException {
+		Files.copy(Paths.get(srcPathString), Paths.get(dstPathString), StandardCopyOption.REPLACE_EXISTING);
+	}
+
+}
diff --git a/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/MailHelper.java b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/MailHelper.java
new file mode 100644
index 0000000..98e20fc
--- /dev/null
+++ b/oKBereitschaftsplanungBackend/src/main/java/org/eclipse/openk/sp/util/MailHelper.java
@@ -0,0 +1,116 @@
+/* *******************************************************************************
+ * Copyright (c) 2020 Basys GmbH
+ * 
+ * See the NOTICE file(s) distributed with this work for additional 
+ * information regarding copyright ownership.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v. 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ * *******************************************************************************/
+package org.eclipse.openk.sp.util;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.Properties;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
+import javax.annotation.PostConstruct;
+import javax.mail.MessagingException;
+import javax.mail.Multipart;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeBodyPart;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMessage.RecipientType;
+import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimeUtility;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+@Service
+public class MailHelper {
+
+	@Autowired
+	private FileHelper fileHelper;
+
+	@Value("${report.mail.propertiesPath}")
+	private String propertiesPath;
+
+	private Session session;
+
+	private String fromAddress;
+
+	@PostConstruct
+	private void init() throws IOException {
+		// load mail properties file
+		File propertiesFile = fileHelper.loadFileFromFileSystemOrResource(propertiesPath);
+		Properties properties = new Properties();
+		properties.load(new FileInputStream(propertiesFile));
+		fromAddress = properties.getProperty("mail.smtp.from");
+		session = Session.getInstance(properties, new javax.mail.Authenticator() {
+			@Override
+			protected PasswordAuthentication getPasswordAuthentication() {
+				return new PasswordAuthentication(properties.getProperty("mail.imap.user"),
+						properties.getProperty("mail.imap.password"));
+			}
+		});
+	}
+
+	public MimeMessage newMultipartMail() throws MessagingException {
+		Multipart mp = new MimeMultipart();
+		MimeMessage msg = new MimeMessage(session);
+		msg.setFrom(fromAddress);
+		msg.setContent(mp);
+		return msg;
+	}
+
+	public static void setToRecipients(MimeMessage mail, List<String> to) throws MessagingException {
+		InternetAddress[] addresses = InternetAddress.parse(String.join(", ", to));
+		mail.addRecipients(RecipientType.TO, addresses);
+	}
+
+	public static void setSubject(MimeMessage mail, String subject) throws MessagingException {
+		mail.setSubject(subject);
+	}
+	
+	public static void addText(MimeMessage mail, String text) throws IOException, MessagingException {
+		if (!(mail.getContent() instanceof MimeMultipart)) {
+			throw new MessagingException("Not a Multipart Message");
+		}
+		MimeMultipart multiPart = (MimeMultipart) mail.getContent();
+		MimeBodyPart textPart = new MimeBodyPart();
+		textPart.setText(text);
+		multiPart.addBodyPart(textPart);
+	}
+
+	public static void addAttachment(MimeMessage mail, File reportFile, String fileName) throws IOException, MessagingException {
+		if (!(mail.getContent() instanceof MimeMultipart)) {
+			throw new MessagingException("Not a Multipart Message");
+		}
+		MimeMultipart multiPart = (MimeMultipart) mail.getContent();
+		MimeBodyPart attachmentPart = new MimeBodyPart();
+		attachmentPart.setFileName(MimeUtility.encodeText(fileName));
+		attachmentPart.setDisposition(MimeBodyPart.ATTACHMENT);
+		DataSource ds = new FileDataSource(reportFile);
+		attachmentPart.setDataHandler(new DataHandler(ds));
+		multiPart.addBodyPart(attachmentPart);
+	}
+
+	public void send(MimeMessage msg) throws MessagingException {
+		System.out.println("send");
+		Transport.send(msg);
+	}
+
+
+}
diff --git a/oKBereitschaftsplanungBackend/src/main/resources/mail.properties b/oKBereitschaftsplanungBackend/src/main/resources/mail.properties
new file mode 100644
index 0000000..076cb23
--- /dev/null
+++ b/oKBereitschaftsplanungBackend/src/main/resources/mail.properties
@@ -0,0 +1 @@
+mail.smtp.from=sender@test.tld
diff --git a/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/ArchiveHelperTest.java b/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/ArchiveHelperTest.java
new file mode 100644
index 0000000..e2510fe
--- /dev/null
+++ b/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/ArchiveHelperTest.java
@@ -0,0 +1,42 @@
+/* *******************************************************************************
+ * Copyright (c) 2020 Basys GmbH
+ * 
+ * See the NOTICE file(s) distributed with this work for additional 
+ * information regarding copyright ownership.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v. 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ * *******************************************************************************/
+package org.eclipse.openk.sp.util;
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.runners.MockitoJUnitRunner;
+
+@RunWith(MockitoJUnitRunner.class)
+public class ArchiveHelperTest {
+
+	@InjectMocks
+	private ArchiveHelper archiveHelper;
+	
+	@Test
+	public void testCopyFile() {
+		String srcPathString = "notExists";
+		String dstPathString = "alsoNotExists";
+		try {
+			archiveHelper.copyFile(srcPathString, dstPathString);
+			fail("Should have thrown IOException");
+		} catch (IOException e) {
+			// pass
+		}
+	}
+
+}
diff --git a/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/MailHelperTest.java b/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/MailHelperTest.java
new file mode 100644
index 0000000..839a56f
--- /dev/null
+++ b/oKBereitschaftsplanungBackend/src/test/java/org/eclipse/openk/sp/util/MailHelperTest.java
@@ -0,0 +1,142 @@
+/* *******************************************************************************
+ * Copyright (c) 2020 Basys GmbH
+ * 
+ * See the NOTICE file(s) distributed with this work for additional 
+ * information regarding copyright ownership.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v. 2.0 which is available at
+ * http://www.eclipse.org/legal/epl-2.0.
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ * *******************************************************************************/
+package org.eclipse.openk.sp.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+
+import javax.mail.BodyPart;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import javax.mail.internet.MimeMessage.RecipientType;
+import javax.mail.internet.MimeMultipart;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.springframework.test.util.ReflectionTestUtils;
+
+@RunWith(MockitoJUnitRunner.class)
+public class MailHelperTest {
+
+	@InjectMocks
+	private MailHelper mailHelper;
+
+	@Mock
+	private FileHelper fileHelper;
+
+	@Before
+	public void init() throws IOException {
+		Properties properties = new Properties();
+		properties.setProperty("mail.smtp.from", "mail@test.tld");
+		properties.setProperty("mail.imap.user", "imap-user");
+		properties.setProperty("mail.imap.password", "imap-password");
+		ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
+		File file = new File(classLoader.getResource("mail.properties").getPath());
+		Mockito.when(fileHelper.loadFileFromFileSystemOrResource(Mockito.eq("mail.properties"))).thenReturn(file );
+		ReflectionTestUtils.setField(mailHelper, "propertiesPath", "mail.properties");
+		ReflectionTestUtils.invokeMethod(mailHelper, "init");
+	}
+
+	@Test
+	public void testNewMultipartMail() throws MessagingException, IOException {
+		MimeMessage msg = mailHelper.newMultipartMail();
+		assertNotNull(msg);
+		assertTrue(msg.getContent() instanceof MimeMultipart);
+	}
+
+	@Test
+	public void testSetToRecipients() throws MessagingException {
+		MimeMessage msg = mailHelper.newMultipartMail();
+
+		List<String> to = new ArrayList<>();
+		to.add("test1@test.tld");
+		to.add("test2@test.tld");
+		MailHelper.setToRecipients(msg, to);
+		assertEquals(2, msg.getRecipients(RecipientType.TO).length);
+	}
+
+	@Test
+	public void testSetSubject() throws MessagingException {
+		MimeMessage msg = mailHelper.newMultipartMail();
+		String subject = "subject";
+		MailHelper.setSubject(msg, subject);
+		assertEquals(subject, msg.getSubject());
+	}
+
+	@Test
+	public void testAddText() throws IOException, MessagingException {
+		MimeMessage msg = mailHelper.newMultipartMail();
+		String text = "test text";
+		MailHelper.addText(msg, text);
+
+		MimeMultipart mult = (MimeMultipart) msg.getContent();
+		BodyPart part = mult.getBodyPart(0);
+		assertEquals("text/plain", part.getContentType());
+		assertEquals(text, part.getContent());
+	}
+	
+	@Test
+	public void testAddTextTestNoMultipart() throws IOException, MessagingException {
+		MimeMessage msg = Mockito.mock(MimeMessage.class);
+		Mockito.when(msg.getContent()).thenReturn("");
+		String text = "test text";
+		try {
+			MailHelper.addText(msg, text);
+			fail("Should have thrown MessagingException");
+		} catch (MessagingException e) {
+			//	pass
+		}
+	}
+
+	@Test
+	public void testAddAttachment() throws MessagingException, IOException {
+		MimeMessage msg = mailHelper.newMultipartMail();
+		String fileName = "fileName.pdf";
+		File reportFile = Mockito.mock(File.class);
+		MailHelper.addAttachment(msg, reportFile, fileName);
+		MimeMultipart mult = (MimeMultipart) msg.getContent();
+		BodyPart part = mult.getBodyPart(0);
+		assertNotNull(part);
+	}
+
+	@Test
+	public void testAddAttachmentTestNoMultipart() throws IOException, MessagingException {
+		MimeMessage msg = Mockito.mock(MimeMessage.class);
+		Mockito.when(msg.getContent()).thenReturn("");
+
+		String fileName = "fileName.pdf";
+		File reportFile = Mockito.mock(File.class);
+		try {
+			MailHelper.addAttachment(msg, reportFile, fileName);
+			fail("Should have thrown MessagingException");
+		} catch (MessagingException e) {
+			//	pass
+		}
+	}
+
+	
+
+}