Skip to content

Commit

Permalink
add an first simple messageBus test
Browse files Browse the repository at this point in the history
  • Loading branch information
svencc committed Oct 18, 2023
1 parent bb4bd00 commit dcd24e7
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/main/java/com/recom/property/RECOMAsyncProperties.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.recom.property;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "recom.async")
public class RECOMAsyncProperties {

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/recom/property/RECOMSecurityProperties.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.recom.property;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.nio.file.Path;
import java.time.Duration;
import java.util.Optional;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "recom.security")
public class RECOMSecurityProperties {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ private void persistNotification(@NonNull final MessageBusResponseDto messageBus
.filter(Objects::nonNull)
.toList();

messagePersistenceLayer.saveAll(messagesToSave);
if (!messagesToSave.isEmpty()) {
messagePersistenceLayer.saveAll(messagesToSave);
}
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.recom.service.messagebus;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.recom.configuration.AsyncConfiguration;
import com.recom.dto.map.Point2DDto;
import com.recom.model.message.MessageContainer;
import com.recom.model.message.MessageType;
import com.recom.model.message.OneMessage;
import com.recom.observer.Subject;
import com.recom.observer.Subjective;
import com.recom.persistence.message.MessagePersistenceLayer;
import com.recom.property.RECOMAsyncProperties;
import com.recom.service.provider.StaticObjectMapperProvider;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class MessageLongPollObserverTest {

@Mock
private AsyncTaskExecutor asyncTaskExecutor;
@Mock
private MessagePersistenceLayer messagePersistenceLayer;
private MessageLongPollObserver observer;

@BeforeEach
public void setUp() {
asyncTaskExecutor = mock(AsyncTaskExecutor.class);
messagePersistenceLayer = mock(MessagePersistenceLayer.class);
observer = MessageLongPollObserver.builder()
.timeout(10000L) // Set a reasonable timeout value
.asyncTaskExecutor(asyncTaskExecutor)
.messagePersistenceLayer(messagePersistenceLayer)
.build();
}

@Test
public void testScheduleTestResponse() throws InterruptedException, IOException {
// Arrange
final ObjectMapper objectMapper = new ObjectMapper();
final StaticObjectMapperProvider staticObjectMapperProvider = new StaticObjectMapperProvider(objectMapper);
staticObjectMapperProvider.postConstruct();

final RECOMAsyncProperties properties = RECOMAsyncProperties.builder()
.corePoolSize(1)
.maxPoolSize(1)
.build();

// Act
observer.scheduleTestResponse("TestMap", Duration.ofMillis(5), new Subject<>(), new AsyncConfiguration(properties));

// Assert
// Sleep for a duration longer than the scheduled task to complete
Thread.sleep(200);
verify(messagePersistenceLayer, times(1)).saveAll(anyList());
}

}

0 comments on commit dcd24e7

Please sign in to comment.