Removed some unused classes

This commit is contained in:
Michele Artini 2020-07-09 09:44:55 +02:00
parent f5cbde07d6
commit d7cce231ac
9 changed files with 9 additions and 213 deletions

View File

@ -1,69 +0,0 @@
package eu.dnetlib.lbs.events.input;
import java.util.Map;
public class EventMessage {
public static final int TTH_INFINITE = -1;
private String producerId;
private String topic;
private String payload;
private int tthDays = TTH_INFINITE;
private Map<String, MapValue> map;
public EventMessage() {}
public EventMessage(final String producerId, final String topic, final String payload, final int tthDays, final Map<String, MapValue> map) {
this.setProducerId(producerId);
this.topic = topic;
this.payload = payload;
this.tthDays = tthDays;
this.map = map;
}
public String getProducerId() {
return this.producerId;
}
public void setProducerId(final String producerId) {
this.producerId = producerId;
}
public String getTopic() {
return this.topic;
}
public void setTopic(final String topic) {
this.topic = topic;
}
public String getPayload() {
return this.payload;
}
public void setPayload(final String payload) {
this.payload = payload;
}
public Map<String, MapValue> getMap() {
return this.map;
}
public void setMap(final Map<String, MapValue> map) {
this.map = map;
}
public boolean hasNulls() {
return this.producerId == null || this.topic == null || this.payload == null || this.map == null;
}
public int getTthDays() {
return this.tthDays;
}
public void setTthDays(final int tthDays) {
this.tthDays = tthDays;
}
}

View File

@ -7,10 +7,10 @@ import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import eu.dnetlib.lbs.events.input.MapValueType;
import eu.dnetlib.lbs.subscriptions.ConditionOperator;
import eu.dnetlib.lbs.subscriptions.ConditionParams;
import eu.dnetlib.lbs.subscriptions.MapCondition;
import eu.dnetlib.lbs.utils.MapValueType;
public class AdvQueryObject {

View File

@ -15,8 +15,8 @@ import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.search.MatchQuery.ZeroTermsQuery;
import eu.dnetlib.lbs.elasticsearch.Event;
import eu.dnetlib.lbs.events.input.MapValueType;
import eu.dnetlib.lbs.utils.DateParser;
import eu.dnetlib.lbs.utils.MapValueType;
public class MapCondition {

View File

@ -1,71 +0,0 @@
package eu.dnetlib.lbs.utils;
import java.util.Date;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.google.gson.Gson;
import eu.dnetlib.lbs.elasticsearch.Event;
import eu.dnetlib.lbs.events.input.EventMessage;
public class JsonMessageToEventFunction implements Function<String, Event> {
private final Gson gson = new Gson();
private static final Log log = LogFactory.getLog(JsonMessageToEventFunction.class);
@Override
public Event apply(final String json) {
try {
final EventMessage msg = gson.fromJson(json, EventMessage.class);
if (msg == null || msg.hasNulls()) {
log.warn("Message is null or some required field is null");
return null;
}
final long now = new Date().getTime();
final Event event = new Event();
event.setEventId("event-" + DigestUtils.md5Hex(msg.getTopic()).substring(0, 6) + "-" + calculatePayloadHash(msg.getPayload()));
event.setProducerId(msg.getProducerId());
event.setPayload(msg.getPayload());
event.setMap(msg.getMap()
.entrySet()
.stream()
.filter(e -> e.getValue().asObject() != null)
.collect(Collectors.toMap(
e -> e.getKey(),
e -> e.getValue().asObject())));
event.setTopic(msg.getTopic());
event.setCreationDate(now);
event.setExpiryDate(calculateExpiryDate(now, msg.getTthDays()));
event.setInstantMessage(msg.getTthDays() == 0);
return event;
} catch (final Throwable e) {
log.warn("Error converting json: " + json, e);
return null;
}
}
private Long calculateExpiryDate(final long now, final int ttlDays) {
if (ttlDays < 0) {
return null;
} else if (ttlDays == 0) {
return now;
} else {
final long duration = ttlDays * 24 * 60 * 60 * 1000;
return now + duration;
}
}
private static String calculatePayloadHash(final String payload) {
// TODO : the payload should be normalized before hashing it
return DigestUtils.md5Hex(payload);
}
}

View File

@ -1,4 +1,4 @@
package eu.dnetlib.lbs.events.input;
package eu.dnetlib.lbs.utils;
import java.util.List;
import java.util.stream.Collectors;
@ -9,8 +9,6 @@ import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import eu.dnetlib.lbs.utils.DateParser;
public class MapValue {
private MapValueType type = MapValueType.STRING;

View File

@ -1,4 +1,4 @@
package eu.dnetlib.lbs.events.input;
package eu.dnetlib.lbs.utils;
public enum MapValueType {
STRING, INTEGER, FLOAT, DATE, BOOLEAN, LIST_STRING, LIST_INTEGER, LIST_FLOAT, LIST_DATE, LIST_BOOLEAN;

View File

@ -15,8 +15,8 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import eu.dnetlib.lbs.elasticsearch.Event;
import eu.dnetlib.lbs.events.input.MapValueType;
import eu.dnetlib.lbs.utils.DateParser;
import eu.dnetlib.lbs.utils.MapValueType;
@RunWith(MockitoJUnitRunner.class)
public class ConditionTest {

View File

@ -1,65 +0,0 @@
package eu.dnetlib.lbs.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import eu.dnetlib.lbs.elasticsearch.Event;
@Ignore
public class JsonMessageToEventFunctionTest {
private JsonMessageToEventFunction f;
@Before
public void setUp() throws Exception {
f = new JsonMessageToEventFunction();
}
@Test
public void test() throws IOException {
final String json = IOUtils.toString(getClass().getResourceAsStream("/test-event-1.json"), Charset.defaultCharset());
System.out.println("Original record:\n\n" + json + "\n\n");
final Event event = f.apply(json);
assertNotNull(event);
assertEquals("openaire", event.getProducerId());
assertEquals("INSERT/123", event.getTopic());
assertEquals("Hello World", event.getPayload());
assertEquals(10 * 24 * 60 * 60 * 1000, event.getExpiryDate() - event.getCreationDate());
assertEquals("id_123", event.getMap().get("id"));
assertEquals("Hello", event.getMap().get("title"));
assertEquals(0.7d, event.getMap().get("trust"));
assertEquals(4l, event.getMap().get("count"));
assertEquals(true, event.getMap().get("open"));
assertNotNull(event.getMap().get("date"));
assertTrue(event.getMap().get("date") instanceof Date);
assertNotNull(event.getMap().get("creators"));
assertTrue(event.getMap().get("creators") instanceof List);
assertEquals(3, ((List<?>) event.getMap().get("creators")).size());
assertEquals("michele", ((List<?>) event.getMap().get("creators")).get(0));
assertEquals("claudio", ((List<?>) event.getMap().get("creators")).get(1));
assertEquals("alessia", ((List<?>) event.getMap().get("creators")).get(2));
assertNotNull(event.getMap().get("years"));
assertTrue(event.getMap().get("years") instanceof List);
assertEquals(2, ((List<?>) event.getMap().get("years")).size());
assertEquals(2011L, ((List<?>) event.getMap().get("years")).get(0));
assertEquals(2013L, ((List<?>) event.getMap().get("years")).get(1));
}
}

View File

@ -1,4 +1,4 @@
package eu.dnetlib.lbs.events.input;
package eu.dnetlib.lbs.utils;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -13,6 +13,9 @@ import java.util.List;
import org.junit.Before;
import org.junit.Test;
import eu.dnetlib.lbs.utils.MapValue;
import eu.dnetlib.lbs.utils.MapValueType;
public class MapValueTest {
@Before