-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
addind testcase for configurationcomponent
- Loading branch information
Showing
3 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...vice/src/test/java/dynamic/mapping/configuration/ConnectorConfigurationComponentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters