Search in sources :

Example 1 with CustomUserField

use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceImpl method map.

private CustomUserField map(CustomUserFieldEntity entity) {
    CustomUserField result = new CustomUserField();
    result.setKey(formatKeyValue(entity.getKey()));
    result.setLabel(entity.getLabel());
    result.setRequired(entity.isRequired());
    // Currently the type is restricted to String
    // We use the MetadataFormat Enum as commodity because these fields
    // will be stored into the Metadata repository with USER as referenceType
    result.setFormat(MetadataFormat.STRING);
    if (entity.getValues() != null) {
        result.setValues(entity.getValues().stream().distinct().collect(Collectors.toList()));
    }
    return result;
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField)

Example 2 with CustomUserField

use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceTest method shouldDeleteField.

@Test
public void shouldDeleteField() throws Exception {
    final CustomUserField fieldMock = mock(CustomUserField.class);
    when(fieldMock.getKey()).thenReturn("validkey");
    when(fieldMock.getReferenceId()).thenReturn(ORG_ID);
    when(fieldMock.getReferenceType()).thenReturn(REF_TYPE);
    when(customUserFieldsRepository.findById(anyString(), anyString(), any())).thenReturn(Optional.of(fieldMock));
    // no issue with upper case here, we want to test the sanitizer on the key
    service.delete("validKEY");
    verify(customUserFieldsRepository).delete("validkey", ORG_ID, REF_TYPE);
    verify(auditService).createOrganizationAuditLog(anyMap(), any(), any(), any(), any());
    verify(ueUserMetadataService).deleteAllByCustomFieldId("validkey", ORG_ID, REF_TYPE);
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) Test(org.junit.Test)

Example 3 with CustomUserField

use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceTest method innerShouldCreateField.

private void innerShouldCreateField(boolean withValues) throws TechnicalException {
    final CustomUserFieldEntity newFieldEntity = new CustomUserFieldEntity();
    newFieldEntity.setKey("NEWKEYUPPERCASE");
    newFieldEntity.setLabel("New Field Label");
    newFieldEntity.setRequired(true);
    if (withValues) {
        newFieldEntity.setValues(Arrays.asList("test"));
    }
    when(customUserFieldsRepository.findById(anyString(), anyString(), any())).thenReturn(Optional.empty());
    when(customUserFieldsRepository.create(any())).thenAnswer(i -> i.getArguments()[0]);
    ArgumentCaptor<CustomUserField> fieldCaptor = ArgumentCaptor.forClass(CustomUserField.class);
    CustomUserFieldEntity createdEntity = service.create(newFieldEntity);
    verify(customUserFieldsRepository).create(fieldCaptor.capture());
    verify(auditService).createOrganizationAuditLog(anyMap(), any(), any(), any(), any());
    assertEquals("CustomUserField.key", newFieldEntity.getKey().toLowerCase(), createdEntity.getKey());
    assertEquals("CustomUserField.label", newFieldEntity.getLabel(), createdEntity.getLabel());
    assertEquals("CustomUserField.required", newFieldEntity.isRequired(), createdEntity.isRequired());
    if (newFieldEntity.getValues() != null) {
        assertNotNull("CustomUserField.values", createdEntity.getValues());
        assertEquals("CustomUserField.values.size", 1, createdEntity.getValues().size());
        assertEquals("CustomUserField.values.get(0)", newFieldEntity.getValues().get(0), createdEntity.getValues().get(0));
    }
    final CustomUserField customFieldRecord = fieldCaptor.getValue();
    assertNotNull(customFieldRecord);
    assertNotNull("CustomUserField.createAt", customFieldRecord.getCreatedAt());
    assertNotNull("CustomUserField.updateAt", customFieldRecord.getUpdatedAt());
    assertEquals("CustomUserField.required", newFieldEntity.isRequired(), customFieldRecord.isRequired());
    assertEquals("CustomUserField.key", newFieldEntity.getKey().toLowerCase(), customFieldRecord.getKey());
    assertEquals("CustomUserField.label", newFieldEntity.getLabel(), customFieldRecord.getLabel());
    assertEquals("CustomUserField.organization", ORG_ID, customFieldRecord.getReferenceId());
    assertEquals("CustomUserField.refType", REF_TYPE, customFieldRecord.getReferenceType());
    assertEquals("CustomUserField.format", MetadataFormat.STRING, customFieldRecord.getFormat());
    if (newFieldEntity.getValues() != null) {
        assertTrue("CustomUserField.values", customFieldRecord.getValues() != null && customFieldRecord.getValues().contains("test"));
    }
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) CustomUserFieldEntity(io.gravitee.rest.api.model.CustomUserFieldEntity)

Example 4 with CustomUserField

use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceTest method shouldUpdateField.

@Test
public void shouldUpdateField() throws Exception {
    final CustomUserField existingField = new CustomUserField();
    final String KEY = "NEWKEYUPPERCASE";
    existingField.setKey(KEY.toLowerCase());
    existingField.setLabel("Field Label");
    existingField.setReferenceId(ORG_ID);
    existingField.setReferenceType(REF_TYPE);
    existingField.setRequired(true);
    existingField.setFormat(MetadataFormat.STRING);
    existingField.setValues(Arrays.asList("test"));
    Date createdAt = new Date(Instant.now().minus(1, ChronoUnit.HOURS).toEpochMilli());
    existingField.setCreatedAt(createdAt);
    existingField.setUpdatedAt(createdAt);
    final CustomUserFieldEntity toUpdateFieldEntity = new CustomUserFieldEntity();
    toUpdateFieldEntity.setKey(KEY);
    toUpdateFieldEntity.setLabel("Updated Field Label");
    toUpdateFieldEntity.setRequired(false);
    toUpdateFieldEntity.setValues(Arrays.asList("test2"));
    when(customUserFieldsRepository.findById(anyString(), anyString(), any())).thenReturn(Optional.of(existingField));
    when(customUserFieldsRepository.update(any())).thenAnswer(i -> i.getArguments()[0]);
    ArgumentCaptor<CustomUserField> fieldCaptor = ArgumentCaptor.forClass(CustomUserField.class);
    CustomUserFieldEntity updatedEntity = service.update(toUpdateFieldEntity);
    verify(customUserFieldsRepository).update(fieldCaptor.capture());
    verify(auditService).createOrganizationAuditLog(anyMap(), any(), any(), any(), any());
    assertEquals("updatedCustomField.key", toUpdateFieldEntity.getKey().toLowerCase(), updatedEntity.getKey());
    assertEquals("updatedCustomField.label", toUpdateFieldEntity.getLabel(), updatedEntity.getLabel());
    assertEquals("updatedCustomField.required", toUpdateFieldEntity.isRequired(), updatedEntity.isRequired());
    assertNotNull("updatedCustomField.values", updatedEntity.getValues());
    assertEquals("updatedCustomField.values.size", 1, updatedEntity.getValues().size());
    assertEquals("updatedCustomField.values.get(0)", toUpdateFieldEntity.getValues().get(0), updatedEntity.getValues().get(0));
    final CustomUserField customFieldRecord = fieldCaptor.getValue();
    assertNotNull(customFieldRecord);
    assertEquals("update parameter createAt", existingField.getCreatedAt(), customFieldRecord.getCreatedAt());
    assertEquals("update parameter key", existingField.getKey(), customFieldRecord.getKey());
    assertEquals("update parameter refId", ORG_ID, customFieldRecord.getReferenceId());
    assertEquals("update parameter refType", REF_TYPE, customFieldRecord.getReferenceType());
    assertEquals("update parameter format", MetadataFormat.STRING, customFieldRecord.getFormat());
    assertNotEquals("update parameter updateAt", existingField.getUpdatedAt(), customFieldRecord.getUpdatedAt());
    assertNotEquals("update parameter required", existingField.isRequired(), customFieldRecord.isRequired());
    assertNotEquals("update parameter label", existingField.getLabel(), customFieldRecord.getLabel());
    assertNotEquals("update parameter values", existingField.getValues(), customFieldRecord.getValues());
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) CustomUserFieldEntity(io.gravitee.rest.api.model.CustomUserFieldEntity) Date(java.util.Date) Test(org.junit.Test)

Example 5 with CustomUserField

use of io.gravitee.repository.management.model.CustomUserField in project gravitee-management-rest-api by gravitee-io.

the class CustomUserFieldsServiceImpl method create.

@Override
public CustomUserFieldEntity create(CustomUserFieldEntity newFieldEntity) {
    try {
        final String refId = GraviteeContext.getCurrentOrganization();
        final CustomUserFieldReferenceType refType = ORGANIZATION;
        LOGGER.debug("Create custom user field [key={}, refId={}]", newFieldEntity.getKey(), refId);
        Optional<CustomUserField> existingRecord = this.customUserFieldsRepository.findById(formatKeyValue(newFieldEntity.getKey()), refId, refType);
        if (existingRecord.isPresent()) {
            throw new CustomUserFieldAlreadyExistException(newFieldEntity.getKey());
        } else {
            CustomUserField fieldToCreate = map(newFieldEntity);
            fieldToCreate.setReferenceId(refId);
            fieldToCreate.setReferenceType(refType);
            final Date now = new Date();
            fieldToCreate.setCreatedAt(now);
            fieldToCreate.setUpdatedAt(now);
            final CustomUserField recorded = customUserFieldsRepository.create(fieldToCreate);
            createAuditLog(CUSTOM_USER_FIELD_CREATED, now, null, recorded);
            return map(recorded);
        }
    } catch (TechnicalException e) {
        LOGGER.error("An error occurs while trying to create CustomUserField", e);
        throw new TechnicalManagementException("An error occurs while trying to create CustomUserField", e);
    }
}
Also used : CustomUserField(io.gravitee.repository.management.model.CustomUserField) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) CustomUserFieldReferenceType(io.gravitee.repository.management.model.CustomUserFieldReferenceType) CustomUserFieldAlreadyExistException(io.gravitee.rest.api.service.exceptions.CustomUserFieldAlreadyExistException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

CustomUserField (io.gravitee.repository.management.model.CustomUserField)9 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)4 CustomUserFieldReferenceType (io.gravitee.repository.management.model.CustomUserFieldReferenceType)4 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)4 Date (java.util.Date)4 CustomUserFieldEntity (io.gravitee.rest.api.model.CustomUserFieldEntity)3 Test (org.junit.Test)3 CustomUserFieldAlreadyExistException (io.gravitee.rest.api.service.exceptions.CustomUserFieldAlreadyExistException)1 CustomUserFieldNotFoundException (io.gravitee.rest.api.service.exceptions.CustomUserFieldNotFoundException)1