gcube-cms-suite/notifications-plugins/src/test/java/CatalogueBindingPluginTest....

252 lines
9.8 KiB
Java

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.gcube.application.cms.notifications.NotificationEventsSubscribedConfig;
import org.gcube.application.cms.notifications.NotificationsPlugin;
import org.gcube.application.cms.notifications.config.NotificationFor;
import org.gcube.application.cms.notifications.config.NotificationWhen;
import org.gcube.application.cms.notifications.config.Notify;
import org.gcube.application.cms.notifications.config.Notify.NOTIFICATION_TYPE;
import org.gcube.application.cms.notifications.config.SubscribeNotificationEvent;
import org.gcube.application.cms.plugins.events.EventManager.Event;
import org.gcube.application.cms.plugins.faults.InitializationException;
import org.gcube.application.cms.serialization.Serialization;
import org.gcube.application.cms.tests.TestProfiles;
import org.gcube.application.cms.tests.plugins.BasicPluginTest;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.UseCaseDescriptor;
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
public class CatalogueBindingPluginTest extends BasicPluginTest {
// @Test
public void checkPlugin() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
NotificationsPlugin plugin = (NotificationsPlugin) plugins.get(NotificationsPlugin.DESCRIPTOR.getId());
try {
plugin.init();
} catch (InitializationException e1) {
e1.printStackTrace();
}
try {
plugin.initInContext();
} catch (InitializationException e1) {
e1.printStackTrace();
}
System.out.println("Plugin check: " + plugin);
}
@Test
public void checkPluginConfig() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
NotificationsPlugin plugin = (NotificationsPlugin) plugins.get(NotificationsPlugin.DESCRIPTOR.getId());
UseCaseDescriptor descriptor = TestProfiles.profiles.get("profiledConcessioni");
try {
plugin.init();
} catch (InitializationException e1) {
e1.printStackTrace();
}
try {
plugin.initInContext();
} catch (InitializationException e1) {
e1.printStackTrace();
}
NotificationEventsSubscribedConfig notificationEventsSubs;
try {
notificationEventsSubs = plugin.readNotificationsSubscribedFromConfigurationInTheUCD(descriptor);
System.out
.println(NotificationEventsSubscribedConfig.class.getSimpleName() + ": " + notificationEventsSubs);
} catch (Exception e) {
e.printStackTrace();
}
}
//@Test
public void checkSerializeDeserialize() {
org.junit.Assume.assumeTrue(GCubeTest.isTestInfrastructureEnabled());
SubscribeNotificationEvent sne = new SubscribeNotificationEvent();
sne.setEvent(Event.LIFECYCLE_STEP_PERFORMED);
List<NotificationFor> listNotificationFor = new ArrayList<NotificationFor>();
NotificationFor notificatioFor = new NotificationFor();
notificatioFor.setRoles(Arrays.asList("Data-Manager"));
List<NotificationWhen> listWhen = new ArrayList<NotificationWhen>();
NotificationWhen notificationWhen = new NotificationWhen();
notificationWhen.setTarget_phase(Arrays.asList("Pending Approval"));
Notify notify = new Notify();
notify.setType(NOTIFICATION_TYPE.EMAIL);
notify.setSend(true);
notify.setAttach_pdf_document(true);
notify.setMessage("message");
notificationWhen.setNotify(Arrays.asList(notify));
listWhen.add(notificationWhen);
notificatioFor.setWhen(listWhen);
listNotificationFor.add(notificatioFor);
sne.setNotificationFor(listNotificationFor);
try {
String serializeTOJSON = Serialization.write(sne);
System.out.println("Serialized toJSON :"+serializeTOJSON);
SubscribeNotificationEvent deserializeToJSON = Serialization.read(serializeTOJSON, SubscribeNotificationEvent.class);
System.out.println("Deserialized from JSON :"+deserializeToJSON);
//Test Notify
/*
String testNotify = "{\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected by ${user}\"\n"
+ "}";
System.out.println("String :"+testNotify);
Notify serialize = Serialization.read(testNotify, Notify.class);
System.out.println("Ser from JSON :"+serialize);
*/
//Test NotificationFor
/*
String testNotificationFor = "{ \"when\": [\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Rejected\"\n"
+ " ],\n"
+ " \"notify\": {\n"
+ " \"send_personal_nofitication\": false,\n"
+ " \"send_email\": true,\n"
+ " \"send_pdf_document\": false,\n"
+ " \"post_on_vre\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected. You are kindly requested to review it\"\n"
+ " }\n"
+ " },\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Published\"\n"
+ " ],\n"
+ " \"notify\": {\n"
+ " \"send_personal_nofitication\": false,\n"
+ " \"send_email\": true,\n"
+ " \"send_pdf_document\": true,\n"
+ " \"post_on_vre\": true,\n"
+ " \"message\": \"The project ${project_name} has just been published. See it at @link\"\n"
+ " }\n"
+ " }\n"
+ " ]}";
System.out.println("String :"+testNotificationFor);
NotificationFor serializeNotificationWhen = Serialization.read(testNotificationFor, NotificationFor.class);
System.out.println("Ser from JSON :"+serializeNotificationWhen);
*/
String testSubribeNotifications = "{\n"
+ " \"event\": \"LIFECYCLE_STEP_PERFORMED\",\n"
+ " \"notificationFor\": [\n"
+ " {\n"
+ " \"roles\": [\n"
+ " \"Data-Manager\"\n"
+ " ],\n"
+ " \"when\": [\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Pending Approval\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"${user} created the project ${project_name}. You are kindly requested to review it and decide either to APPROVE or REJECT it. See it at @link\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Rejected\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected by ${user}\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"roles\": [\n"
+ " \"Item_Creator\"\n"
+ " ],\n"
+ " \"when\": [\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Rejected\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been rejected. You are kindly requested to review it\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"target_phase\": [\n"
+ " \"Published\"\n"
+ " ],\n"
+ " \"notify\": [\n"
+ " {\n"
+ " \"type\": \"EMAIL\",\n"
+ " \"send\": true,\n"
+ " \"attach_pdf_document\": false,\n"
+ " \"message\": \"The project ${project_name} has just been published. See it at @link\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ " ]\n"
+ "}";
testSubribeNotifications = "{event=LIFECYCLE_STEP_PERFORMED, notificationFor=[{roles=[Data-Manager], when=[{target_phase=[Pending Approval], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=${user} created the project ${project_name}. You are kindly requested to review it and decide either to APPROVE or REJECT it. See it at @link}]}, {target_phase=[Rejected], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been rejected by ${user}}]}]}, {roles=[Item_Creator], when=[{target_phase=[Rejected], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been rejected. You are kindly requested to review it}]}, {target_phase=[Published], notify=[{type=EMAIL, send=true, attach_pdf_document=false, message=The project ${project_name} has just been published. See it at @link}]}]}]}";
System.out.println("String :"+testSubribeNotifications);
SubscribeNotificationEvent serializeSubscribeNotificationEvent = Serialization.convert(testSubribeNotifications, SubscribeNotificationEvent.class);
System.out.println("Ser from JSON :"+serializeSubscribeNotificationEvent);
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}