Exemplo n.º 1
0
  @Test(expected = SecurityException.class)
  public void shouldThrowExceptionWhileReadingInstanceWithoutAnyPermission() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(SecurityMode.NO_ACCESS);
    entityDto.setSecurityMode(SecurityMode.NO_ACCESS);

    EntityRecord entityRecord =
        new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<FieldRecord>());

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);

    instanceService.getEntityRecords(entityRecord.getId());
  }
Exemplo n.º 2
0
  @Test
  public void shouldAcceptUserWithNoPermissionWhileReadingInstanceWithNoSecurityMode() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<>());

    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId());

    verify(entityService).getEntityFieldsForUI(ANOTHER_ENTITY_ID);
  }
Exemplo n.º 3
0
  @Test
  public void shouldNotUpdateGridSizeWhenUsernameIsBlank() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<FieldRecord>());

    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));

    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt());
  }
Exemplo n.º 4
0
  @Test
  public void shouldAutoPopulateOwnerAndCreator() {
    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(
            asList(
                FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null),
                FieldTestHelper.fieldDto(
                    1L, "creator", String.class.getName(), "String field", null)));
    mockEntity();
    setUpSecurityContext();

    EntityRecord record = instanceService.newInstance(ENTITY_ID);

    List<FieldRecord> fieldRecords = record.getFields();
    assertEquals(
        asList("motech", "motech"), extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
Exemplo n.º 5
0
  @Test
  public void shouldReturnNewInstances() {
    mockSampleFields();
    mockEntity();

    EntityRecord record = instanceService.newInstance(ENTITY_ID);

    assertNotNull(record);
    assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId());
    assertNull(record.getId());

    List<FieldRecord> fieldRecords = record.getFields();
    assertCommonFieldRecordFields(fieldRecords);
    assertEquals(
        asList("Default", 7, null, null, null),
        extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
Exemplo n.º 6
0
  private HistoryRecord convertToHistoryRecord(
      Object object, EntityDto entity, Long instanceId, MotechDataService service) {
    Long entityId = entity.getId();

    EntityRecord entityRecord =
        instanceToRecord(object, entity, entityService.getEntityFields(entityId), service);
    Long historyInstanceSchemaVersion =
        (Long)
            PropertyUtil.safeGetProperty(
                object, HistoryTrashClassHelper.schemaVersion(object.getClass()));
    Long currentSchemaVersion = entityService.getCurrentSchemaVersion(entity.getClassName());

    return new HistoryRecord(
        entityRecord.getId(),
        instanceId,
        historyInstanceSchemaVersion.equals(currentSchemaVersion),
        entityRecord.getFields());
  }
Exemplo n.º 7
0
  @Test
  public void shouldUpdateGridSize() {
    setUpSecurityContext();

    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());

    EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<>());

    when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto);

    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100));

    verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1);
    verify(userPreferencesService).updateGridSize(ENTITY_ID + 1, "motech", 100);
  }
Exemplo n.º 8
0
  @Test
  public void shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields() {
    FieldDto ownerField =
        FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null);
    ownerField.setNonEditable(true);
    FieldDto creatorField =
        FieldTestHelper.fieldDto(1L, "creator", String.class.getName(), "String field", null);
    creatorField.setNonEditable(true);

    when(entityService.getEntityFieldsForUI(ENTITY_ID))
        .thenReturn(asList(ownerField, creatorField));
    mockEntity();
    setUpSecurityContext();

    EntityRecord record = instanceService.newInstance(ENTITY_ID);

    List<FieldRecord> fieldRecords = record.getFields();
    assertEquals(asList(null, null), extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
Exemplo n.º 9
0
  @Test
  public void shouldReturnEntityInstance() {
    mockDataService();
    mockSampleFields();
    mockEntity();
    when(motechDataService.retrieve("id", INSTANCE_ID))
        .thenReturn(new TestSample("Hello world", 99));

    EntityRecord record = instanceService.getEntityInstance(ENTITY_ID, INSTANCE_ID);

    assertNotNull(record);
    assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId());
    assertEquals(Long.valueOf(INSTANCE_ID), record.getId());

    List<FieldRecord> fieldRecords = record.getFields();
    assertCommonFieldRecordFields(fieldRecords);
    assertEquals(
        asList("Hello world", 99, null, null, null),
        extract(fieldRecords, on(FieldRecord.class).getValue()));
  }
Exemplo n.º 10
0
  @Override
  @Transactional
  public Object saveInstance(EntityRecord entityRecord, Long deleteValueFieldId) {
    EntityDto entity = getEntity(entityRecord.getEntitySchemaId());
    validateCredentials(entity);
    validateNonEditableProperty(entity);

    List<FieldDto> entityFields = getEntityFields(entityRecord.getEntitySchemaId());

    try {
      MotechDataService service = getServiceForEntity(entity);
      Class<?> entityClass = getEntityClass(entity);

      boolean newObject = entityRecord.getId() == null;

      Object instance;
      if (newObject) {
        instance = entityClass.newInstance();

        for (FieldDto entityField : entityFields) {
          if (entityField.getType().isMap() && entityField.getBasic().getDefaultValue() != null) {
            setInstanceFieldMap(instance, entityField);
          }
        }

      } else {
        instance = service.retrieve(ID_FIELD_NAME, entityRecord.getId());
        if (instance == null) {
          throw new ObjectNotFoundException(entity.getName(), entityRecord.getId());
        }
      }

      updateFields(instance, entityRecord.getFields(), service, deleteValueFieldId, !newObject);

      if (newObject) {
        return service.create(instance);
      } else {
        return service.update(instance);
      }
    } catch (Exception e) {
      if (entityRecord.getId() == null) {
        throw new ObjectCreateException(entity.getName(), e);
      } else {
        throw new ObjectUpdateException(entity.getName(), entityRecord.getId(), e);
      }
    }
  }