Skip to content

Commit

Permalink
fix mocking meterRegistry for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CameronRushton committed Aug 21, 2024
1 parent fe3d4a1 commit 3353265
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static com.solace.maas.ep.common.metrics.ObservabilityConstants.ENTITY_TYPE_TAG;
import static com.solace.maas.ep.common.metrics.ObservabilityConstants.MAAS_EMA_SCAN_EVENT_RECEIVED;
import static com.solace.maas.ep.common.metrics.ObservabilityConstants.ORG_ID_TAG;
import static com.solace.maas.ep.common.metrics.ObservabilityConstants.SCAN_ID_TAG;

@Slf4j
@Component
Expand All @@ -36,7 +37,7 @@ public void receiveMessage(String destinationName, ScanCommandMessage message) {
log.debug("Received scan command message: {} for event broker: {}, traceId: {}",
message, message.getMessagingServiceId(), message.getTraceId());
meterRegistry.counter(MAAS_EMA_SCAN_EVENT_RECEIVED, ENTITY_TYPE_TAG, message.getType(),
ORG_ID_TAG, message.getOrgId()).increment();
ORG_ID_TAG, message.getOrgId(), SCAN_ID_TAG, message.getScanId()).increment();
scanCommandMessageProcessor.processMessage(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void failConfigPushCommand() {
CommandMessage message = getCommandMessage("1");

doNothing().when(commandPublisher).sendCommandResponse(any(), any());
doThrow(new RuntimeException("Error running command.")).when(commandManager).configPush(commandMapper.map(message));
doThrow(new RuntimeException("Error running command.")).when(commandManager).configPush(any());

commandManager.execute(message);
await().atMost(10, TimeUnit.SECONDS).until(() -> CommandManagerTestHelper.verifyCommandPublisherIsInvoked(commandPublisher, 1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.solace.maas.ep.event.management.agent.processor.ScanDataProcessor;
import com.solace.maas.ep.event.management.agent.publisher.ScanDataPublisher;
import com.solace.maas.ep.event.management.agent.route.ep.ScanDataPublisherRouteBuilder;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.noop.NoopCounter;
import lombok.SneakyThrows;
import org.apache.camel.CamelContext;
import org.apache.camel.EndpointInject;
Expand All @@ -24,7 +26,9 @@
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.ActiveProfiles;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


@CamelSpringBootTest
Expand Down Expand Up @@ -67,6 +71,8 @@ public static RoutesBuilder createRouteBuilder() {
SolacePublisher solacePublisher = mock(SolacePublisher.class);
EventPortalProperties eventPortalProperties = mock(EventPortalProperties.class);
MeterRegistry meterRegistry = mock(MeterRegistry.class);
when(meterRegistry.counter(any(), any(), any(), any(), any(), any(), any(), any(), any()))
.thenReturn(new NoopCounter(new Meter.Id("noop", null, null, null, null)));

ScanDataPublisher scanDataPublisher = new ScanDataPublisher(solacePublisher, meterRegistry);
ScanDataProcessor scanDataProcessor = new ScanDataProcessor(scanDataPublisher, eventPortalProperties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public class ScanServiceTests {
@Autowired
private ScanServiceHelper scanServiceHelper;

@Mock
@Autowired
private MeterRegistry meterRegistry;

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.solace.maas.ep.event.management.agent.service.ManualImportFilesService;
import com.solace.maas.ep.event.management.agent.subscriber.messageProcessors.ScanCommandMessageProcessor;
import com.solace.messaging.receiver.InboundMessage;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.noop.NoopCounter;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.camel.ProducerTemplate;
Expand All @@ -34,6 +36,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThatNoException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -91,6 +94,8 @@ public void scanReceiver() {
when(inboundMessage.getProperty(MOPConstants.MOP_MSG_META_DECODER)).thenReturn(
"com.solace.maas.ep.common.messages.ScanCommandMessage");
when(inboundMessage.getDestinationName()).thenReturn("anyTopic");
when(meterRegistry.counter(any(), any(), any(), any(), any(), any(), any())).thenReturn(new NoopCounter(
new Meter.Id("mockMeterId", null, null, null, null)));

ScanCommandMessageHandler scanCommandMessageHandler = new ScanCommandMessageHandler(
solaceConfiguration, solaceSubscriber, scanCommandMessageProcessor, meterRegistry);
Expand Down Expand Up @@ -118,6 +123,8 @@ public void testBadClass() {
@Test
@SneakyThrows
public void testScanCommandMessage() {
when(meterRegistry.counter(any(), any(), any(), any(), any(), any(), any())).thenReturn(new NoopCounter(
new Meter.Id("mockMeterId", null, null, null, null)));
ScanCommandMessageHandler scanCommandMessageHandler = new ScanCommandMessageHandler(
solaceConfiguration, solaceSubscriber, scanCommandMessageProcessor, meterRegistry);

Expand Down

0 comments on commit 3353265

Please sign in to comment.