Skip to content

Commit

Permalink
Merge pull request #18 from protegeproject/update_projectId_in_revisi…
Browse files Browse the repository at this point in the history
…onsEvent

Update project id in revisions event
  • Loading branch information
alexsilaghi authored Dec 18, 2024
2 parents 5162365 + c956b2a commit 0e1a532
Show file tree
Hide file tree
Showing 15 changed files with 113 additions and 26 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>webprotege-ipc</artifactId>
<version>1.0.5</version>
<version>1.0.10</version>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,48 @@
package edu.stanford.protege.webprotegeeventshistory.config;

import com.mongodb.*;
import edu.stanford.protege.webprotegeeventshistory.config.converters.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import javax.annotation.Nonnull;
import java.util.Arrays;


@Configuration
@EnableMongoRepositories(basePackages = "edu.stanford.protege.webprotegeeventshistory")
public class MongoConfiguration {
public class MongoConfiguration extends AbstractMongoClientConfiguration {

@Value("${spring.data.mongodb.database}")
private String databaseName;

@Value("${spring.data.mongodb.uri}")
private String mongoUri;

@Nonnull
@Override
protected String getDatabaseName() {
return databaseName;
}

@Nonnull
@Override
public MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(mongoUri))
.build();
}

@Nonnull
@Override
public MongoCustomConversions customConversions() {
return new MongoCustomConversions(Arrays.asList(
new ProjectIdToStringConverter(),
new StringToProjectIdConverter()
));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package edu.stanford.protege.webprotegeeventshistory.config.converters;

import edu.stanford.protege.webprotege.common.ProjectId;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;

@WritingConverter
public class ProjectIdToStringConverter implements Converter<ProjectId, String> {
@Override
public String convert(ProjectId projectId) {
return projectId.id();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.stanford.protege.webprotegeeventshistory.config.converters;

import edu.stanford.protege.webprotege.common.ProjectId;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;

import javax.annotation.Nonnull;

@ReadingConverter
public class StringToProjectIdConverter implements Converter<String, ProjectId> {
@Override
public ProjectId convert(@Nonnull String source) {
return ProjectId.valueOf(source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
import edu.stanford.protege.webprotege.common.ProjectId;
import edu.stanford.protege.webprotegeeventshistory.uiHistoryConcern.dto.ChangeType;
import org.springframework.data.mongodb.core.index.*;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.*;

@Document(collection = "RevisionsEvents")
public record RevisionsEvent(

@Indexed(name = "revisionEventProjectId")
ProjectId projectId,

@Field("projectId") String projectId,
@Indexed(name = "revisionEventEntityIri")
String whoficEntityIri,
ChangeType changeType,
Expand All @@ -30,7 +28,7 @@ public static RevisionsEvent create(ProjectId projectId,
ChangeType changeType,
long timestamp,
org.bson.Document projectChange) {
return new RevisionsEvent(projectId, whoficEntityIri, changeType, timestamp, projectChange);
return new RevisionsEvent(projectId.id(), whoficEntityIri, changeType, timestamp, projectChange);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.List;

public interface RevisionsEventRepository extends MongoRepository<RevisionsEvent, String> {
@Query("{ 'projectId.id': ?0, 'timestamp': { $gt: ?1 } }")
@Query("{ 'projectId': ?0, 'timestamp': { $gt: ?1 } }")
List<RevisionsEvent> findByProjectIdAndTimestampAfter(String projectId, long timestamp);

List<RevisionsEvent> findByProjectIdAndWhoficEntityIriOrderByTimestampDesc(ProjectId projectId, String whoficEntityIri);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void registerEvent(NewRevisionsEvent newRevEvent) {
@Override
public Page<ProjectChange> fetchPaginatedProjectChanges(ProjectId projectId, Optional<OWLEntity> subject, int pageNumber, int pageSize) {
String entityIriSubject = subject.map(sub -> sub.getIRI().toString()).orElse(null);
RevisionsEvent probe = new RevisionsEvent(
RevisionsEvent probe = RevisionsEvent.create(
projectId,
entityIriSubject,
null,
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ spring:
password: guest
data.mongodb:
host: mongo
port: 27017
database: webprotege
auto-index-creation: true
uri: mongodb://${spring.data.mongodb.host}:${spring.data.mongodb.port}/${spring.data.mongodb.database}

webprotege:
rabbitmq:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.test.context.ActiveProfiles;
import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl;

import java.util.ArrayList;
Expand All @@ -28,7 +29,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


@ActiveProfiles("test")
public class HighLevelBusinessEventsIntegrationTest extends IntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ public static void closeContainers(){
private static void setUpMongo(){
var imageName = DockerImageName.parse("mongo");
mongoDBContainer = new MongoDBContainer(imageName)
.withExposedPorts(27017, 27017);
.withExposedPorts(27017);
mongoDBContainer.start();

var mappedHttpPort = mongoDBContainer.getMappedPort(27017);
LOGGER.info("MongoDB port 27017 is mapped to {}", mappedHttpPort);
System.setProperty("spring.data.mongodb.port", Integer.toString(mappedHttpPort));
var mappedPort = mongoDBContainer.getMappedPort(27017);
var mongoUri = String.format("mongodb://localhost:%d", mappedPort);

System.setProperty("spring.data.mongodb.uri", mongoUri);
System.setProperty("spring.data.mongodb.port", Integer.toString(mappedPort));
}

private static void setUpRabbitMq(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@

public class MongoTestExtension implements BeforeAllCallback, AfterAllCallback {

private static MongoDBContainer mongoDBContainer;
private static MongoDBContainer mongoDBContainer;

@Override
public void beforeAll(ExtensionContext extensionContext) {
var imageName = DockerImageName.parse("mongo");
mongoDBContainer = new MongoDBContainer(imageName)
.withExposedPorts(27017, 27017);
.withExposedPorts(27017);
mongoDBContainer.start();

var mappedHttpPort = mongoDBContainer.getMappedPort(27017);
System.setProperty("spring.data.mongodb.port", Integer.toString(mappedHttpPort));
var mappedPort = mongoDBContainer.getMappedPort(27017);
var mongoUri = String.format("mongodb://localhost:%d", mappedPort);

System.setProperty("spring.data.mongodb.uri", mongoUri);
System.setProperty("spring.data.mongodb.port", Integer.toString(mappedPort));
}

@Override
public void afterAll(ExtensionContext extensionContext) {
mongoDBContainer.stop();
if (mongoDBContainer != null) {
mongoDBContainer.stop();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Mono;

Expand All @@ -23,6 +24,7 @@
@ExtendWith({SpringExtension.class, RabbitTestExtension.class, MongoTestExtension.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@Import({WebprotegeEventsHistoryApplication.class})
@ActiveProfiles("test")
public class GetChangedEntitiesCommandHandlerIT {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import reactor.core.publisher.Mono;

Expand All @@ -28,6 +29,7 @@
@ExtendWith({SpringExtension.class, RabbitTestExtension.class, MongoTestExtension.class})
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
@Import({WebprotegeEventsHistoryApplication.class})
@ActiveProfiles("test")
public class GetProjectChangesForHistoryViewCommandHandlerIntegrationTest {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public void GIVEN_validNewLinearizationRevisionsEvent_WHEN_handleEventCalled_THE

RevisionsEvent firstEvent = savedEvents.get(0);
assertEquals("whoficEntityIri1", firstEvent.whoficEntityIri());
assertEquals(projectId, firstEvent.projectId());
assertEquals(projectId.id(), firstEvent.projectId());
assertEquals(12345L, firstEvent.timestamp());

RevisionsEvent secondEvent = savedEvents.get(1);
assertEquals("whoficEntityIri2", secondEvent.whoficEntityIri());
assertEquals(projectId, secondEvent.projectId());
assertEquals(projectId.id(), secondEvent.projectId());
assertEquals(12346L, secondEvent.timestamp());
}

Expand Down Expand Up @@ -117,10 +117,10 @@ public void GIVEN_multipleValidEvents_WHEN_handleEventCalled_THEN_allEventsAreRe

RevisionsEvent firstSavedEvent = savedEvents.get(0);
assertEquals("whoficEntityIri1", firstSavedEvent.whoficEntityIri());
assertEquals(projectId1, firstSavedEvent.projectId());
assertEquals(projectId1.id(), firstSavedEvent.projectId());

RevisionsEvent thirdSavedEvent = savedEvents.get(2);
assertEquals("whoficEntityIri3", thirdSavedEvent.whoficEntityIri());
assertEquals(projectId2, thirdSavedEvent.projectId());
assertEquals(projectId2.id(), thirdSavedEvent.projectId());
}
}
14 changes: 11 additions & 3 deletions src/test/resources/application-test.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spring:
application:
name: webprotege-linearization-service
name: EventsHistory
rabbitmq:
host: rabbitmq
host: localhost
port: 5672
password: guest
username: guest
Expand All @@ -12,4 +12,12 @@ spring:
port: 27017
database: webprotege
auto-index-creation: true
uri: mongodb://${spring.data.mongodb.host}:${spring.data.mongodb.port}/webprotege
uri: mongodb://${spring.data.mongodb.host}:${spring.data.mongodb.port}/webprotege

webprotege:
rabbitmq:
requestqueue: webprotege-events-history-queue
responsequeue: webprotege-events-history-response
eventsqueue: webprotege-events-history-event-queue
timeout: 60000
event-subscribe: true

0 comments on commit 0e1a532

Please sign in to comment.