diff --git a/README.md b/README.md
new file mode 100644
index 0000000..5075c32
--- /dev/null
+++ b/README.md
@@ -0,0 +1,28 @@
+# SpringBootApplications
+
+1) All the RestTemplate tests were implemented in RestClientTest.java using Junit.
+After you start the application, you can run RestClientTest.java.
+
+Please remember that test methods have to be executed with the given order because results of one method is affecting the result of another one.
+For example; we need to upload first to be able to make a download, get by id etc.
+
+2) CommonUtils class was added to define a few utility methods.
+
+3) Email, EmailSender, ScheduledCronJobTests classes were created to send emails
+
+4) Application.java, FileController.java, FileMetaDataSearchCriteria.java, FileUploadUtil.java, RESTClient.java, RestClientTest.java,
+ScheduledCronJobs.java were updated.
+
+
+Important notes:
+
+For testing purposes, I assume that files in 'C://UploadRepo' and 'C://UploadRepo' folder itself will not be removed manually while the application is running.
+
+Please run RestClientTest.java to run the unit tests. The tests are connected to each other. Please make sure you run the whole class(not single methods) for the convenience of tests.
+
+Please run RestClientTest.java once for each run on Application.java. Otherwise, the test results will be unexpected due to uploading duplicate name files.
+
+Please replace the email addresses in application.properties with your own email addresses to see whether the cron job is sending an email. It would be a better practice to keep these type of values in a SYSTEM_PARAMETERS table.
+Application.createJavaMailSender needs update in terms of SMTP settings.
+
+Please check 'src/main/resourcec/DownloadRepository' and 'C://UploadRepo' for downloaded and uploaded files.
diff --git a/SpringRESTExample/pom.xml b/SpringRESTExample/pom.xml
index dea964b..5c7d277 100644
--- a/SpringRESTExample/pom.xml
+++ b/SpringRESTExample/pom.xml
@@ -83,6 +83,34 @@
spring-boot-starter-cloud-connectors
+
+ com.jayway.jsonpath
+ json-path-assert
+
+
+ org.skyscreamer
+ jsonassert
+
+
+ org.springframework.boot
+
+ spring-boot-starter-data-elasticsearch
+
+
+
+ org.springframework.boot
+ spring-boot-starter-artemis
+
+
+ org.springframework.boot
+ spring-boot-starter-freemarker
+
+
+
+ javax.mail
+ mail
+ 1.4.7
+
diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java b/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java
index 3497f0b..31880b6 100644
--- a/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java
+++ b/SpringRESTExample/src/main/java/com/springboot/restapi/Application.java
@@ -1,9 +1,12 @@
package com.springboot.restapi;
+import java.util.Properties;
+
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@@ -14,72 +17,20 @@ public static void main(String[] args) {
args);
/**
- * This method call will create a the folder "C://UploadRepo". This
- * folder will be used to save the content of uploaded files.
+ * This method call will create the folder "C://UploadRepo". This folder
+ * will be used to save the content of the uploaded files.
*/
((FileUploadUtil) context.getBean("fileUploadUtil"))
.createUploadRepository();
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getAllFiles());
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile(
- "src/main/resources/UploadFromRepository/TextFile.txt",
- null, null));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getAllFiles());
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile(
- "src/main/resources/UploadFromRepository/TextFile.txt",
- "TextFile_1.txt", null));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile(
- "src/main/resources/UploadFromRepository/TextFile.txt",
- "TextFile_2.txt", "Test"));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile("C://FileRepo/TextFileee.txt", "TextFile_3.txt",
- "Test"));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile("C://FileRepo///", "TextFile_4.txt", "Test"));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .uploadFile("C://FileRepo/TextFile.txt", "Text//File_5.txt",
- "Test"));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getAllFiles());
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getFileMetaDataById(1));
+ System.out
+ .println("Important: For testing purposes, I assume that files in 'C://UploadRepo' and 'C://UploadRepo' folder itself will not be removed manually while the application is running.");
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getFileMetaDataById(null));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .getFileMetaDataById(100));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .downloadFile(1));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .downloadFile(null));
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .downloadFile(100));
-
- FileMetaDataSearchCriteria fileMetaDataSearchCriteria = new FileMetaDataSearchCriteria();
-
- fileMetaDataSearchCriteria.setName("File_1");
-
- System.out.println(((RESTClient) context.getBean("restClient"))
- .searchFiles(fileMetaDataSearchCriteria));
+ System.out
+ .println("Important: Please run RestClientTest.java to run the unit tests. The tests are connected to each other. Please make sure you run the whole class(not single methods) for the convenience of tests.");
+ System.out
+ .println("Important: Please run RestClientTest.java once for each run on Application.java. Otherwise, the test results will be unexpected due to uploading duplicate name files.");
}
@Bean
@@ -87,4 +38,25 @@ public RestTemplate getRestTemplate() {
return new RestTemplate();
}
+ /**
+ * This part may need update due to the network settings in the machine that
+ * code is executed.
+ *
+ * @return
+ */
+ @Bean
+ public JavaMailSenderImpl createJavaMailSender() {
+ JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();
+ javaMailSender.setHost("smtp.gmail.com");
+ javaMailSender.setPort(587);
+ Properties properties = new Properties();
+ properties.put("spring.mail.username", "*****");
+ properties.put("spring.mail.password", "*****");
+
+ properties.put("spring.mail.properties.mail.smtp.auth", true);
+ properties
+ .put("spring.mail.properties.mail.smtp.starttls.enable", true);
+ return javaMailSender;
+ }
+
}
diff --git a/SpringRESTExample/src/main/java/com/springboot/restapi/CommonUtils.java b/SpringRESTExample/src/main/java/com/springboot/restapi/CommonUtils.java
new file mode 100644
index 0000000..d7c0f87
--- /dev/null
+++ b/SpringRESTExample/src/main/java/com/springboot/restapi/CommonUtils.java
@@ -0,0 +1,108 @@
+package com.springboot.restapi;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+public class CommonUtils {
+
+ @Value("${run_scheduled_jobs}")
+ private String runScheduleJobs;
+
+ @Value("${from_address}")
+ private String mailFromAddress;
+
+ @Value("${to_address}")
+ private String mailToAddress;
+
+ @Value("${reply_to_address}")
+ private String replyToAddress;
+
+ /**
+ * This method is to convert a JSONArray to JSONObject list
+ *
+ * @param array
+ * @return
+ * @throws JSONException
+ */
+ public List getJSONObjectListFromJSONArray(JSONArray array)
+ throws JSONException {
+ ArrayList jsonObjects = new ArrayList();
+ for (int i = 0; i < (array != null ? array.length() : 0); jsonObjects
+ .add(array.getJSONObject(i++)))
+ ;
+ return jsonObjects;
+ }
+
+ /**
+ * Returns whether or not a scheduled job is set to run based on the
+ * property value "run_scheduled_jobs"
+ *
+ * @return
+ */
+ public boolean runScheduledJob() {
+ return Boolean.parseBoolean(runScheduleJobs);
+ }
+
+ /**
+ * Returns the to_address entry in defined application.properties
+ *
+ * @return
+ */
+ public String getMailToAddress() {
+ return mailToAddress;
+ }
+
+ /**
+ * Returns the reply_to_address in defined application.properties
+ *
+ * @return
+ */
+ public String getMailReplyToAddress() {
+ return replyToAddress;
+ }
+
+ /**
+ * Returns the from_address in defined application.properties
+ *
+ * @return
+ */
+ public String getMailFromAddress() {
+ return mailFromAddress;
+ }
+
+ /**
+ * This method is to create an Email object from a list of FileMetaData .
+ *
+ * @param fileMetaDatas
+ * @return
+ */
+ public Email generateEmailFrom(List fileMetaDatas) {
+ Email email = new Email();
+ if (fileMetaDatas == null || fileMetaDatas.size() == 0) {
+ email.setSubject("No FileMetaData records added in last one hour");
+ } else {
+ email.setSubject("New FileMetaData records added in last one hour: "
+ + fileMetaDatas.size());
+ }
+ email.setFromAddress(getMailFromAddress());
+ email.setToAddresses(getMailToAddress());
+ email.setReplyTo(getMailReplyToAddress());
+
+ StringBuilder body = new StringBuilder();
+
+ for (FileMetaData fileMetaData : fileMetaDatas) {
+ body.append("