Search in sources :

Example 6 with CustomUserFieldEntity

use of io.gravitee.rest.api.model.CustomUserFieldEntity 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 7 with CustomUserFieldEntity

use of io.gravitee.rest.api.model.CustomUserFieldEntity 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 8 with CustomUserFieldEntity

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

the class CustomUserFieldsResourceAdminTest method shouldNotUpdate_KeyMismatch.

@Test
public void shouldNotUpdate_KeyMismatch() {
    reset(customUserFieldService);
    final CustomUserFieldEntity field = new CustomUserFieldEntity();
    field.setKey("test-update");
    field.setLabel("Test");
    ArgumentCaptor<CustomUserFieldEntity> argument = ArgumentCaptor.forClass(CustomUserFieldEntity.class);
    when(customUserFieldService.update(any())).thenReturn(field);
    final Response response = orgTarget("/invalid-key").request().put(Entity.json(field));
    assertEquals(BAD_REQUEST_400, response.getStatus());
    verify(customUserFieldService, never()).update(any());
}
Also used : Response(javax.ws.rs.core.Response) CustomUserFieldEntity(io.gravitee.rest.api.model.CustomUserFieldEntity) Test(org.junit.Test)

Example 9 with CustomUserFieldEntity

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

the class CustomUserFieldsResourceNotAdminTest method shouldNotCreateField.

@Test
public void shouldNotCreateField() {
    reset(customUserFieldService);
    final CustomUserFieldEntity field = new CustomUserFieldEntity();
    field.setKey("TestResCreate");
    field.setLabel("TestResCreate");
    final Response response = orgTarget().request().post(Entity.json(field));
    assertEquals(FORBIDDEN_403, response.getStatus());
    verify(customUserFieldService, never()).create(any());
}
Also used : Response(javax.ws.rs.core.Response) CustomUserFieldEntity(io.gravitee.rest.api.model.CustomUserFieldEntity) Test(org.junit.Test)

Example 10 with CustomUserFieldEntity

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

the class CustomUserFieldsServiceImpl method map.

private CustomUserFieldEntity map(CustomUserField record) {
    CustomUserFieldEntity result = new CustomUserFieldEntity();
    result.setKey(formatKeyValue(record.getKey()));
    result.setLabel(record.getLabel());
    result.setRequired(record.isRequired());
    if (record.getValues() != null) {
        switch(record.getFormat()) {
            case STRING:
                result.setValues(record.getValues());
                break;
            default:
                throw new TechnicalManagementException("Unable to read values of CustomUserField, format not supported");
        }
    }
    return result;
}
Also used : CustomUserFieldEntity(io.gravitee.rest.api.model.CustomUserFieldEntity) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Aggregations

CustomUserFieldEntity (io.gravitee.rest.api.model.CustomUserFieldEntity)13 Test (org.junit.Test)11 Response (javax.ws.rs.core.Response)6 CustomUserField (io.gravitee.repository.management.model.CustomUserField)3 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)1 Date (java.util.Date)1 List (java.util.List)1