diff --git a/dynamic-mapping-service/pom.xml b/dynamic-mapping-service/pom.xml index 1cfc9e63..95b7986c 100644 --- a/dynamic-mapping-service/pom.xml +++ b/dynamic-mapping-service/pom.xml @@ -91,6 +91,16 @@ junit-jupiter test + + org.mockito + mockito-core + test + + + org.mockito + mockito-junit-jupiter + test + org.apache.kafka kafka-clients diff --git a/dynamic-mapping-service/src/test/java/dynamic/mapping/configuration/ConnectorConfigurationComponentTest.java b/dynamic-mapping-service/src/test/java/dynamic/mapping/configuration/ConnectorConfigurationComponentTest.java new file mode 100644 index 00000000..924b03f9 --- /dev/null +++ b/dynamic-mapping-service/src/test/java/dynamic/mapping/configuration/ConnectorConfigurationComponentTest.java @@ -0,0 +1,174 @@ +package dynamic.mapping.configuration; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.contains; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.List; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.cumulocity.microservice.subscription.service.MicroserviceSubscriptionsService; +import com.cumulocity.model.option.OptionPK; +import com.cumulocity.rest.representation.tenant.OptionRepresentation; +import com.cumulocity.sdk.client.option.TenantOptionApi; +import com.fasterxml.jackson.databind.ObjectMapper; + +@ExtendWith(MockitoExtension.class) +class ConnectorConfigurationComponentTest { + + @Mock + private TenantOptionApi tenantOptionApi; + + @Mock + private MicroserviceSubscriptionsService subscriptionsService; + + @Mock + private ObjectMapper objectMapper; + + @InjectMocks + private ConnectorConfigurationComponent configurationComponent; + + private static final String TEST_TENANT = "test-tenant"; + private static final String TEST_IDENTIFIER = "test-identifier"; + private static final String OPTION_CATEGORY = "dynamic.mapper.service"; + + @BeforeEach + void setUp() throws Exception { + // Create the component with the mocked dependencies + configurationComponent = new ConnectorConfigurationComponent(tenantOptionApi); + + // Use reflection to set the private subscriptionsService field + Field subscriptionsServiceField = ConnectorConfigurationComponent.class.getDeclaredField("subscriptionsService"); + subscriptionsServiceField.setAccessible(true); + subscriptionsServiceField.set(configurationComponent, subscriptionsService); + + // Set ObjectMapper + configurationComponent.setObjectMapper(objectMapper); + } + + @Test + void testSaveConnectorConfiguration() throws Exception { + // Arrange + ConnectorConfiguration configuration = new ConnectorConfiguration(); + configuration.setIdentifier(TEST_IDENTIFIER); + String jsonConfig = "{\"identifier\":\"test-identifier\"}"; + + when(objectMapper.writeValueAsString(configuration)).thenReturn(jsonConfig); + + // Act + configurationComponent.saveConnectorConfiguration(configuration); + + // Assert + verify(tenantOptionApi).save(any(OptionRepresentation.class)); + } + + @Test + void testGetConnectorConfiguration() throws Exception { + // Arrange + ConnectorConfiguration expectedConfig = new ConnectorConfiguration(); + expectedConfig.setIdentifier(TEST_IDENTIFIER); + + OptionRepresentation optionRepresentation = new OptionRepresentation(); + optionRepresentation.setValue("{\"identifier\":\"test-identifier\"}"); + + when(tenantOptionApi.getOption(any(OptionPK.class))).thenReturn(optionRepresentation); + when(objectMapper.readValue(anyString(), eq(ConnectorConfiguration.class))).thenReturn(expectedConfig); + when(subscriptionsService.callForTenant(eq(TEST_TENANT), any())).thenAnswer(invocation -> { + return ((java.util.concurrent.Callable) invocation.getArgument(1)).call(); + }); + + // Act + ConnectorConfiguration result = configurationComponent.getConnectorConfiguration(TEST_IDENTIFIER, TEST_TENANT); + + // Assert + assertNotNull(result); + assertEquals(TEST_IDENTIFIER, result.getIdentifier()); + } + + @Test + void testGetConnectorConfigurations() throws Exception { + // Arrange + List options = Arrays.asList( + createOptionRepresentation("connection.configuration.1", "{\"identifier\":\"1\"}"), + createOptionRepresentation("connection.configuration.2", "{\"identifier\":\"2\"}") + ); + + ConnectorConfiguration config1 = new ConnectorConfiguration(); + config1.setIdentifier("1"); + ConnectorConfiguration config2 = new ConnectorConfiguration(); + config2.setIdentifier("2"); + + when(tenantOptionApi.getAllOptionsForCategory(OPTION_CATEGORY)).thenReturn(options); + when(objectMapper.readValue(contains("1"), eq(ConnectorConfiguration.class))).thenReturn(config1); + when(objectMapper.readValue(contains("2"), eq(ConnectorConfiguration.class))).thenReturn(config2); + + doAnswer(invocation -> { + Runnable runnable = invocation.getArgument(1); + runnable.run(); + return null; + }).when(subscriptionsService).runForTenant(eq(TEST_TENANT), any(Runnable.class)); + + // Act + List results = configurationComponent.getConnectorConfigurations(TEST_TENANT); + + // Assert + assertNotNull(results); + assertEquals(2, results.size()); + } + + @Test + void testDeleteConnectorConfiguration() throws Exception { + // Act + configurationComponent.deleteConnectorConfiguration(TEST_IDENTIFIER); + + // Assert + verify(tenantOptionApi).delete(any(OptionPK.class)); + } + + @Test + void testEnableConnection() throws Exception { + // Arrange + OptionRepresentation optionRepresentation = new OptionRepresentation(); + optionRepresentation.setValue("{\"identifier\":\"test-identifier\",\"enabled\":false}"); + + ConnectorConfiguration config = new ConnectorConfiguration(); + config.setIdentifier(TEST_IDENTIFIER); + config.enabled = false; + + when(tenantOptionApi.getOption(any(OptionPK.class))).thenReturn(optionRepresentation); + when(objectMapper.readValue(anyString(), eq(ConnectorConfiguration.class))).thenReturn(config); + when(objectMapper.writeValueAsString(any(ConnectorConfiguration.class))) + .thenReturn("{\"identifier\":\"test-identifier\",\"enabled\":true}"); + when(subscriptionsService.getTenant()).thenReturn(TEST_TENANT); + + // Act + ConnectorConfiguration result = configurationComponent.enableConnection(TEST_IDENTIFIER, true); + + // Assert + assertNotNull(result); + assertTrue(result.enabled); + verify(tenantOptionApi).save(any(OptionRepresentation.class)); + } + + private OptionRepresentation createOptionRepresentation(String key, String value) { + OptionRepresentation option = new OptionRepresentation(); + option.setKey(key); + option.setValue(value); + return option; + } +} diff --git a/pom.xml b/pom.xml index 8a79dc55..07a61024 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,19 @@ org.junit.jupiter junit-jupiter 5.11.0 + test + + + org.mockito + mockito-core + 5.11.0 + test + + + org.mockito + mockito-junit-jupiter + 5.11.0 + test org.projectlombok