Skip to content

Commit

Permalink
fix: Delete event stats in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Palafox committed Apr 3, 2024
1 parent 36e7a45 commit e9e15de
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.orcid.persistence.dao;

import org.orcid.persistence.jpa.entities.EventStatsEntity;
import org.orcid.persistence.jpa.entities.EventType;

import java.util.List;

Expand All @@ -16,4 +17,6 @@ public interface EventStatsDao {
void createPapiEventStats();

List<EventStatsEntity> findAll();

void deleteStatsByType(EventType eventType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ public void createPapiEventStats() {
});
}
}

@Override
@Transactional
public void deleteStatsByType(EventType eventType) {
String query = "DELETE FROM event_stats where event_type = :eventType";
Query queryDelete = entityManager.createNativeQuery(query);
queryDelete.setParameter("eventType", eventType.getValue());
queryDelete.executeUpdate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.junit.runner.RunWith;
import org.orcid.persistence.jpa.entities.EventEntity;
import org.orcid.persistence.jpa.entities.EventStatsEntity;
import org.orcid.persistence.jpa.entities.EventType;
import org.orcid.test.OrcidJUnit4ClassRunner;
import org.springframework.test.context.ContextConfiguration;

Expand Down Expand Up @@ -39,6 +40,8 @@ public void createEventStats() {
assertEquals(2, eventStatsEntityList.size());
assertEquals(Integer.valueOf(20), eventStatsEntityList.get(0).getCount());
assertEquals(Integer.valueOf(20), eventStatsEntityList.get(1).getCount());

eventStatsDao.deleteStatsByType(EventType.SIGN_IN);
}

@Test
Expand All @@ -54,12 +57,14 @@ public void createPapiEventStats() {
assertEquals(Integer.valueOf(10), eventStatsEntityList.get(0).getCount());
assertEquals(Integer.valueOf(10), eventStatsEntityList.get(1).getCount());
assertEquals(Integer.valueOf(1100), eventStatsEntityList.get(2).getCount());

eventStatsDao.deleteStatsByType(EventType.PAPI);
}

private void createPapiEvents() {
for (int i = 0; i < 20; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType("Public-API");
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("Client " + (i % 2 == 0 ? 1 : 2));
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
Expand All @@ -69,7 +74,7 @@ private void createPapiEvents() {

for (int i = 0; i < 10; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType("Public-API");
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("105.21.229.71");
eventEntity.setLabel("anonymous");
LocalDate date = LocalDate.now().minusDays(1);
Expand All @@ -80,7 +85,7 @@ private void createPapiEvents() {

for (int i = 0; i < 1100; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType("Public-API");
eventEntity.setEventType(EventType.PAPI.getValue());
eventEntity.setClientId("104.20.228.70");
eventEntity.setLabel("anonymous");
LocalDate date = LocalDate.now().minusDays(1);
Expand All @@ -93,8 +98,8 @@ private void createPapiEvents() {
private void createEvents() {
for (int i = 0; i < 40; i++) {
EventEntity eventEntity = new EventEntity();
eventEntity.setEventType(i % 2 == 0 ? "Sign-In" : "Public-PAPI");
eventEntity.setClientId("Client " + 1);
eventEntity.setEventType(EventType.SIGN_IN.getValue());
eventEntity.setClientId("Client " + (i % 2 == 0 ? 1 : 2)) ;
LocalDate date = LocalDate.now().minusDays(1);
Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant();
eventEntity.setDateCreated(Date.from(instant));
Expand Down

0 comments on commit e9e15de

Please sign in to comment.