/** @throws Exception */
  @Test
  public void addConsumedMedication() throws Exception {

    int oldSize = patientMedicationDao.findAll().size();

    Patient patient = (Patient) userService.findUserByLogin("sysuser1");
    assertNotNull(patient);
    Medication medication = medicationService.findByName("Kort 2", Locale.GERMANY).get(0);
    assertNotNull(medication);

    Disease disease = diseaseService.findByCode("M79.0");
    assertNotNull(disease);

    patientMedicationService.addConsumedMedication(
        patient.getId(), disease.getId(), medication.getId(), 2.0d, new Date(), null);
    assertEquals(oldSize + 1, patientMedicationDao.findAll().size());

    patientMedicationService.addConsumedMedication(
        patient.getId(), disease.getId(), medication.getId(), 2.0d, new Date(), "test");
    assertEquals(oldSize + 2, patientMedicationDao.findAll().size());

    List<Medication> result =
        patientMedicationService.findAllConsumedMedications(patient.getId(), disease.getId());
    assertEquals(4, result.size());
    assertFalse(result.get(0).equals(result.get(1)));

    result = patientMedicationService.findAllConsumedMedications(patient.getId(), 0L);
    assertEquals(0, result.size());
  }
  /** @throws Exception */
  @Test
  public void addConsumedMedicationWeekly() throws Exception {

    int oldSize = patientMedicationDao.findAll().size();

    Patient patient = (Patient) userService.findUserByLogin("sysuser1");
    assertNotNull(patient);
    Medication medication = medicationService.findByName("Kort 2", Locale.GERMANY).get(0);

    Disease disease = diseaseService.findByCode(DiseaseCodes.RHEUMA_CODE);
    assertNotNull(disease);

    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    Date startDate = dateFormat.parse("1.11.2010");
    Date endDate = dateFormat.parse("28.11.2010");

    patientMedicationService.addConsumedMedication(
        patient.getId(),
        disease.getId(),
        medication.getId(),
        2.0d,
        MedicationFrequency.WEEKLY,
        startDate,
        endDate,
        null);
    assertEquals(oldSize + 4, patientMedicationDao.findAll().size());
  }
  /** @throws Exception */
  @Test
  public void findAllForDisease() throws Exception {
    Patient patient = (Patient) userService.findUserByLogin("sysuser1");
    assertNotNull(patient);

    Disease disease = diseaseService.findByCode("M79.0");
    assertNotNull(disease);

    List<PatientMedication> result =
        patientMedicationService.findAllForDisease(patient.getId(), disease.getId());
    assertEquals(6, result.size());
    PatientMedication medication = result.get(0);
    assertNotNull(medication);
  }
  /**
   * {@link PatientMedicationService#findConsumedMedicationsDiary(Long, java.util.Date,
   * java.util.Date)}
   *
   * @throws Exception
   */
  @Test
  public void findPatientConsumedMedicationsDiary2() throws Exception {
    Patient patient = (Patient) userService.findUserByLogin("sysuser1");
    assertNotNull(patient);

    Disease disease = diseaseService.findByCode("M79.0");
    assertNotNull(disease);

    DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");
    Date startDate = dateFormat.parse("1.11.2010");
    Date endDate = dateFormat.parse("3.11.2010");
    List<Object[]> result =
        patientMedicationService.findConsumedMedicationsDiary(patient.getId(), startDate, endDate);

    assertNotNull(result);
    assertEquals(5, result.size());
    assertEquals(2, Number.class.cast(result.get(2)[2]).intValue());
    assertEquals(2, Number.class.cast(result.get(3)[2]).intValue());

    for (Object[] row : result) {
      assertTrue(Medication.class.isInstance(row[1]));
    }
  }