Search in sources :

Example 11 with DefinedFieldEntity

use of com.synopsys.integration.alert.database.configuration.DefinedFieldEntity in project hub-alert by blackducksoftware.

the class DefaultConfigurationModelConfigurationAccessorTest method decryptTest.

@Test
public void decryptTest() {
    final String decryptedString = "decryptedString";
    final String providerConfigName = "provider-config-name-test";
    final String emptyProviderConfigName = "bad-config-name";
    final Long fieldId = 1L;
    final Long descriptorId = 4L;
    final Long configurationId = 6L;
    DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(fieldKey, true);
    definedFieldEntity.setId(fieldId);
    FieldValueEntity fieldValueEntity = new FieldValueEntity(2L, 3L, fieldValue);
    DescriptorConfigEntity descriptorConfigEntity = new DescriptorConfigEntity(descriptorId, 5L, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp());
    descriptorConfigEntity.setId(configurationId);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
    EncryptionUtility encryptionUtilityDecrypt = Mockito.mock(EncryptionUtility.class);
    Mockito.when(definedFieldRepository.findFirstByKey(Mockito.any())).thenReturn(Optional.of(definedFieldEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName)).thenReturn(List.of(fieldValueEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, emptyProviderConfigName)).thenReturn(List.of());
    setupGetJobMocks(descriptorConfigEntity, configContextEntity, fieldValueEntity, definedFieldEntity);
    Mockito.when(encryptionUtilityDecrypt.decrypt(Mockito.any())).thenReturn(decryptedString);
    DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = new DefaultConfigurationModelConfigurationAccessor(null, definedFieldRepository, descriptorConfigRepository, configContextRepository, fieldValueRepository, encryptionUtilityDecrypt);
    Optional<ConfigurationModel> configurationModelOptional = configurationModelConfigurationAccessor.getProviderConfigurationByName(providerConfigName);
    assertTrue(configurationModelOptional.isPresent());
    ConfigurationModel configurationModel = configurationModelOptional.get();
    testConfigurationModel(configurationId, descriptorId, configurationModel);
    assertEquals(decryptedString, configurationModel.getField(fieldKey).get().getFieldValue().get());
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DescriptorConfigEntity(com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) FieldValueEntity(com.synopsys.integration.alert.database.configuration.FieldValueEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) EncryptionUtility(com.synopsys.integration.alert.common.security.EncryptionUtility) Test(org.junit.jupiter.api.Test)

Example 12 with DefinedFieldEntity

use of com.synopsys.integration.alert.database.configuration.DefinedFieldEntity in project hub-alert by blackducksoftware.

the class DefaultDescriptorAccessorTest method getFieldsForDescriptorTest.

@Test
public void getFieldsForDescriptorTest() {
    final String name = "name-test";
    final Long typeId = 1L;
    final ConfigContextEnum configContextEnum = ConfigContextEnum.GLOBAL;
    final ConfigContextEnum invalidConfigContextEnum = ConfigContextEnum.DISTRIBUTION;
    final String definedFieldsKey = "defined-field-key-test";
    Boolean isSensitive = Boolean.TRUE;
    RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(name, typeId);
    registeredDescriptorEntity.setId(1L);
    DescriptorKey descriptorKey = createDescriptorKey("descriptorKey-test");
    ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
    configContextEntity.setId(3L);
    DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(definedFieldsKey, isSensitive);
    RegisteredDescriptorRepository registeredDescriptorRepository = Mockito.mock(RegisteredDescriptorRepository.class);
    DefinedFieldRepository definedFieldRepository = Mockito.mock(DefinedFieldRepository.class);
    ConfigContextRepository configContextRepository = Mockito.mock(ConfigContextRepository.class);
    DescriptorTypeRepository descriptorTypeRepository = Mockito.mock(DescriptorTypeRepository.class);
    Mockito.when(registeredDescriptorRepository.findFirstByName(descriptorKey.getUniversalKey())).thenReturn(Optional.of(registeredDescriptorEntity));
    Mockito.when(configContextRepository.findFirstByContext(configContextEnum.name())).thenReturn(Optional.of(configContextEntity));
    // Used to test the optional expression
    Mockito.when(configContextRepository.findFirstByContext(invalidConfigContextEnum.name())).thenReturn(Optional.empty());
    Mockito.when(configContextRepository.save(Mockito.any())).thenReturn(configContextEntity);
    Mockito.when(definedFieldRepository.findByDescriptorIdAndContext(Mockito.any(), Mockito.any())).thenReturn(List.of(definedFieldEntity));
    DefaultDescriptorAccessor descriptorAccessor = new DefaultDescriptorAccessor(registeredDescriptorRepository, definedFieldRepository, configContextRepository, descriptorTypeRepository);
    List<DefinedFieldModel> definedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, configContextEnum);
    List<DefinedFieldModel> emptyConfigContextDefinedFieldModelList = descriptorAccessor.getFieldsForDescriptor(descriptorKey, invalidConfigContextEnum);
    assertEquals(1, definedFieldModelList.size());
    DefinedFieldModel definedFieldModel = definedFieldModelList.get(0);
    assertEquals(definedFieldsKey, definedFieldModel.getKey());
    assertEquals(isSensitive, definedFieldModel.getSensitive());
    List<ConfigContextEnum> configContextList = new ArrayList<>(definedFieldModel.getContexts());
    assertEquals(configContextEnum, configContextList.get(0));
    assertEquals(1, emptyConfigContextDefinedFieldModelList.size());
    DefinedFieldModel emptyConfigContextDefinedFieldModel = emptyConfigContextDefinedFieldModelList.get(0);
    assertEquals(definedFieldsKey, emptyConfigContextDefinedFieldModel.getKey());
    assertEquals(isSensitive, emptyConfigContextDefinedFieldModel.getSensitive());
    List<ConfigContextEnum> configContextList2 = new ArrayList<>(emptyConfigContextDefinedFieldModel.getContexts());
    assertEquals(invalidConfigContextEnum, configContextList2.get(0));
}
Also used : DefinedFieldModel(com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel) RegisteredDescriptorEntity(com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity) ArrayList(java.util.ArrayList) DescriptorKey(com.synopsys.integration.alert.descriptor.api.model.DescriptorKey) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) DefinedFieldRepository(com.synopsys.integration.alert.database.configuration.repository.DefinedFieldRepository) ConfigContextRepository(com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository) DescriptorTypeRepository(com.synopsys.integration.alert.database.configuration.repository.DescriptorTypeRepository) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) RegisteredDescriptorRepository(com.synopsys.integration.alert.database.configuration.repository.RegisteredDescriptorRepository) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 13 with DefinedFieldEntity

use of com.synopsys.integration.alert.database.configuration.DefinedFieldEntity in project hub-alert by blackducksoftware.

the class DefaultConfigurationModelConfigurationAccessorTest method getProviderConfigurationByNameTest.

@Test
public void getProviderConfigurationByNameTest() {
    final String providerConfigName = "provider-config-name-test";
    final String emptyProviderConfigName = "bad-config-name";
    final Long fieldId = 1L;
    final Long descriptorId = 4L;
    final Long configurationId = 6L;
    DefinedFieldEntity definedFieldEntity = new DefinedFieldEntity(fieldKey, false);
    definedFieldEntity.setId(fieldId);
    FieldValueEntity fieldValueEntity = new FieldValueEntity(2L, 3L, fieldValue);
    DescriptorConfigEntity descriptorConfigEntity = new DescriptorConfigEntity(descriptorId, 5L, DateUtils.createCurrentDateTimestamp(), DateUtils.createCurrentDateTimestamp());
    descriptorConfigEntity.setId(configurationId);
    ConfigContextEntity configContextEntity = new ConfigContextEntity(configContextEnum.name());
    Mockito.when(definedFieldRepository.findFirstByKey(Mockito.any())).thenReturn(Optional.of(definedFieldEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, providerConfigName)).thenReturn(List.of(fieldValueEntity));
    Mockito.when(fieldValueRepository.findAllByFieldIdAndValue(fieldId, emptyProviderConfigName)).thenReturn(List.of());
    setupGetJobMocks(descriptorConfigEntity, configContextEntity, fieldValueEntity, definedFieldEntity);
    DefaultConfigurationModelConfigurationAccessor configurationModelConfigurationAccessor = new DefaultConfigurationModelConfigurationAccessor(null, definedFieldRepository, descriptorConfigRepository, configContextRepository, fieldValueRepository, encryptionUtility);
    Optional<ConfigurationModel> configurationModelOptional = configurationModelConfigurationAccessor.getProviderConfigurationByName(providerConfigName);
    Optional<ConfigurationModel> configurationModelProviderConfigsEmpty = configurationModelConfigurationAccessor.getProviderConfigurationByName(emptyProviderConfigName);
    assertTrue(configurationModelOptional.isPresent());
    assertFalse(configurationModelProviderConfigsEmpty.isPresent());
    ConfigurationModel configurationModel = configurationModelOptional.get();
    testConfigurationModel(configurationId, descriptorId, configurationModel);
}
Also used : ConfigurationModel(com.synopsys.integration.alert.common.persistence.model.ConfigurationModel) DescriptorConfigEntity(com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) FieldValueEntity(com.synopsys.integration.alert.database.configuration.FieldValueEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) Test(org.junit.jupiter.api.Test)

Example 14 with DefinedFieldEntity

use of com.synopsys.integration.alert.database.configuration.DefinedFieldEntity in project hub-alert by blackducksoftware.

the class FieldContextRepositoryTestIT method findByFieldId.

@Test
public void findByFieldId() {
    DefinedFieldEntity definedFieldEntity1 = new DefinedFieldEntity(FIELD_KEY_1, Boolean.FALSE);
    DefinedFieldEntity definedFieldEntity2 = new DefinedFieldEntity(FIELD_KEY_2, Boolean.FALSE);
    DefinedFieldEntity savedFieldEntity1 = definedFieldRepository.save(definedFieldEntity1);
    DefinedFieldEntity savedFieldEntity2 = definedFieldRepository.save(definedFieldEntity2);
    assertEquals(2, definedFieldRepository.findAll().size());
    ConfigContextEntity configContextEntity1 = new ConfigContextEntity(CONTEXT_NAME);
    ConfigContextEntity configContextEntity2 = new ConfigContextEntity(CONTEXT_NAME + "2");
    ConfigContextEntity savedContextEntity1 = configContextRepository.save(configContextEntity1);
    ConfigContextEntity savedContextEntity2 = configContextRepository.save(configContextEntity2);
    assertEquals(2, configContextRepository.findAll().size());
    FieldContextRelation fieldContextRelation1 = new FieldContextRelation(savedFieldEntity1.getId(), savedContextEntity1.getId());
    FieldContextRelation fieldContextRelation2 = new FieldContextRelation(savedFieldEntity1.getId(), savedContextEntity2.getId());
    FieldContextRelation fieldContextRelation3 = new FieldContextRelation(savedFieldEntity2.getId(), savedContextEntity1.getId());
    fieldContextRepository.save(fieldContextRelation1);
    fieldContextRepository.save(fieldContextRelation2);
    fieldContextRepository.save(fieldContextRelation3);
    assertEquals(3, fieldContextRepository.findAll().size());
    List<FieldContextRelation> foundList1 = fieldContextRepository.findByFieldId(savedFieldEntity1.getId());
    assertEquals(2, foundList1.size());
    List<FieldContextRelation> foundList2 = fieldContextRepository.findByFieldId(savedFieldEntity2.getId());
    assertEquals(1, foundList2.size());
}
Also used : FieldContextRelation(com.synopsys.integration.alert.database.configuration.FieldContextRelation) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity) AlertIntegrationTest(com.synopsys.integration.alert.util.AlertIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 15 with DefinedFieldEntity

use of com.synopsys.integration.alert.database.configuration.DefinedFieldEntity in project hub-alert by blackducksoftware.

the class DescriptorMocker method addFieldToDescriptor.

public void addFieldToDescriptor(String descriptorName, String fieldKey, Collection<ConfigContextEnum> contexts, Boolean sensitive) {
    Long descriptorId = registeredDescriptorRepository.findFirstByName(descriptorName).map(RegisteredDescriptorEntity::getId).orElseThrow();
    Long fieldId = definedFieldRepository.findFirstByKey(fieldKey).map(DefinedFieldEntity::getId).orElseGet(() -> definedFieldRepository.save(new DefinedFieldEntity(fieldKey, sensitive)).getId());
    try {
        descriptorFieldRepository.save(new DescriptorFieldRelation(descriptorId, fieldId));
    } catch (Exception e) {
    // Ignore exception, relation already registered
    }
    for (ConfigContextEnum context : contexts) {
        String contextName = context.name();
        Long contextId = configContextRepository.findFirstByContext(contextName).map(ConfigContextEntity::getId).orElseGet(() -> configContextRepository.save(new ConfigContextEntity(contextName)).getId());
        try {
            fieldContextRepository.save(new FieldContextRelation(fieldId, contextId));
        } catch (Exception e) {
        // Ignore exception, relation already registered
        }
    }
}
Also used : FieldContextRelation(com.synopsys.integration.alert.database.configuration.FieldContextRelation) DescriptorFieldRelation(com.synopsys.integration.alert.database.configuration.DescriptorFieldRelation) DefinedFieldEntity(com.synopsys.integration.alert.database.configuration.DefinedFieldEntity) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) ConfigContextEntity(com.synopsys.integration.alert.database.configuration.ConfigContextEntity)

Aggregations

DefinedFieldEntity (com.synopsys.integration.alert.database.configuration.DefinedFieldEntity)20 Test (org.junit.jupiter.api.Test)17 ConfigContextEntity (com.synopsys.integration.alert.database.configuration.ConfigContextEntity)14 RegisteredDescriptorEntity (com.synopsys.integration.alert.database.configuration.RegisteredDescriptorEntity)11 DescriptorConfigEntity (com.synopsys.integration.alert.database.configuration.DescriptorConfigEntity)10 FieldValueEntity (com.synopsys.integration.alert.database.configuration.FieldValueEntity)10 AlertIntegrationTest (com.synopsys.integration.alert.util.AlertIntegrationTest)8 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)7 DescriptorFieldRelation (com.synopsys.integration.alert.database.configuration.DescriptorFieldRelation)6 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)4 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)4 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)3 FieldContextRelation (com.synopsys.integration.alert.database.configuration.FieldContextRelation)3 OffsetDateTime (java.time.OffsetDateTime)3 ArrayList (java.util.ArrayList)3 AlertRuntimeException (com.synopsys.integration.alert.api.common.model.exception.AlertRuntimeException)2 DefinedFieldModel (com.synopsys.integration.alert.common.persistence.model.DefinedFieldModel)2 ConfigurationModelMutable (com.synopsys.integration.alert.common.persistence.model.mutable.ConfigurationModelMutable)2 EncryptionUtility (com.synopsys.integration.alert.common.security.EncryptionUtility)2 ConfigContextRepository (com.synopsys.integration.alert.database.configuration.repository.ConfigContextRepository)2