Skip to content

Commit

Permalink
addind testcase for configurationcomponent
Browse files Browse the repository at this point in the history
  • Loading branch information
ck-c8y committed Dec 31, 2024
1 parent a0d080e commit d9f0dd9
Show file tree
Hide file tree
Showing 3 changed files with 197 additions and 0 deletions.
10 changes: 10 additions & 0 deletions dynamic-mapping-service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<OptionRepresentation> 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<ConnectorConfiguration> 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;
}
}
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand Down

0 comments on commit d9f0dd9

Please sign in to comment.