dnet-hadoop/dhp-workflows/dhp-broker-events/src/test/java/eu/dnetlib/dhp/broker/oa/GenerateNotificationsJobTes...

134 lines
4.3 KiB
Java
Raw Normal View History

2021-10-04 12:01:56 +02:00
package eu.dnetlib.dhp.broker.oa;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
2021-10-04 12:01:56 +02:00
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import eu.dnetlib.dhp.broker.model.ConditionParams;
2021-10-04 12:01:56 +02:00
import eu.dnetlib.dhp.broker.model.Event;
import eu.dnetlib.dhp.broker.model.MappedFields;
import eu.dnetlib.dhp.broker.model.Subscription;
import eu.dnetlib.dhp.broker.oa.util.NotificationGroup;
class GenerateNotificationsJobTest {
2021-10-04 12:01:56 +02:00
private List<Subscription> subscriptions;
private Map<String, Map<String, List<ConditionParams>>> conditionsMap;
2021-10-04 15:46:12 +02:00
private static final int N_TIMES = 1_000_000;
2021-10-04 12:01:56 +02:00
@BeforeEach
void setUp() throws Exception {
final Subscription s = new Subscription();
s.setTopic("ENRICH/MISSING/PID");
s
.setConditions(
"[{\"field\":\"targetDatasourceName\",\"fieldType\":\"STRING\",\"operator\":\"EXACT\",\"listParams\":[{\"value\":\"reposiTUm\"}]},{\"field\":\"trust\",\"fieldType\":\"FLOAT\",\"operator\":\"RANGE\",\"listParams\":[{\"value\":\"0\",\"otherValue\":\"1\"}]}]");
2021-10-04 12:01:56 +02:00
subscriptions = Arrays.asList(s);
conditionsMap = GenerateNotificationsJob.prepareConditionsMap(subscriptions);
2021-10-04 12:01:56 +02:00
}
@Test
void testGenerateNotifications_invalid_topic() {
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PROJECT");
final NotificationGroup res = GenerateNotificationsJob
.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 15:46:12 +02:00
assertEquals(0, res.getData().size());
}
@Test
void testGenerateNotifications_topic_match() {
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PID");
event.setMap(new MappedFields());
event.getMap().setTargetDatasourceName("reposiTUm");
event.getMap().setTrust(0.8f);
2021-10-04 12:01:56 +02:00
final NotificationGroup res = GenerateNotificationsJob
.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 15:46:12 +02:00
assertEquals(1, res.getData().size());
}
2021-10-04 12:01:56 +02:00
2021-10-04 15:46:12 +02:00
@Test
void testGenerateNotifications_topic_no_match() {
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PID");
event.setMap(new MappedFields());
event.getMap().setTargetDatasourceName("Puma");
event.getMap().setTrust(0.8f);
final NotificationGroup res = GenerateNotificationsJob
.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 15:46:12 +02:00
assertEquals(0, res.getData().size());
}
@Test
void testGenerateNotifications_invalid_topic_repeated() {
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PROJECT");
// warm up
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 15:46:12 +02:00
final long start = System.currentTimeMillis();
for (int i = 0; i < N_TIMES; i++) {
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 12:01:56 +02:00
}
2021-10-04 15:46:12 +02:00
final long end = System.currentTimeMillis();
System.out
.println(String.format("no topic - repeated %s times - execution time: %s ms ", N_TIMES, end - start));
2021-10-04 15:46:12 +02:00
2021-10-04 12:01:56 +02:00
}
@Test
2021-10-04 15:46:12 +02:00
void testGenerateNotifications_topic_match_repeated() {
2021-10-04 12:01:56 +02:00
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PID");
event.setMap(new MappedFields());
event.getMap().setTargetDatasourceName("reposiTUm");
event.getMap().setTrust(0.8f);
2021-10-04 15:46:12 +02:00
// warm up
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 12:01:56 +02:00
2021-10-04 15:46:12 +02:00
final long start = System.currentTimeMillis();
for (int i = 0; i < N_TIMES; i++) {
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 12:01:56 +02:00
}
2021-10-04 15:46:12 +02:00
final long end = System.currentTimeMillis();
System.out
.println(String.format("topic match - repeated %s times - execution time: %s ms ", N_TIMES, end - start));
2021-10-04 12:01:56 +02:00
}
@Test
2021-10-04 15:46:12 +02:00
void testGenerateNotifications_topic_no_match_repeated() {
2021-10-04 12:01:56 +02:00
final Event event = new Event();
event.setTopic("ENRICH/MISSING/PID");
event.setMap(new MappedFields());
event.getMap().setTargetDatasourceName("Puma");
event.getMap().setTrust(0.8f);
2021-10-04 15:46:12 +02:00
// warm up
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 12:01:56 +02:00
2021-10-04 15:46:12 +02:00
final long start = System.currentTimeMillis();
for (int i = 0; i < N_TIMES; i++) {
GenerateNotificationsJob.generateNotifications(event, subscriptions, conditionsMap, 0);
2021-10-04 12:01:56 +02:00
}
2021-10-04 15:46:12 +02:00
final long end = System.currentTimeMillis();
System.out
.println(
String.format("topic no match - repeated %s times - execution time: %s ms ", N_TIMES, end - start));
2021-10-04 12:01:56 +02:00
}
}