/** @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 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 save() throws Exception {
    List<QuestionType> questionTypesOld = questionTypeService.findAll();
    assertFalse(questionTypesOld.isEmpty());

    User patient1 = userService.findUserByLogin("sysuser1");
    assertNotNull(patient1);

    QuestionType questionType = new QuestionType();
    questionType.setAnswerDataType(AnswerDataType.BOOLEAN);
    questionType.setAnswers(new ArrayList<Answer>());
    questionType.setType(Type.SINGLE_CHOICE_LIST);
    NoAnswer answer1 = new NoAnswer();
    answer1.setActive(false);
    answer1.setAnswer("test");
    answer1.setQuestionType(questionType);
    answer1.setSortOrder(0);
    questionType.getAnswers().add(answer1);

    Answer answer2 = new Answer();
    answer2.setActive(true);
    answer2.setAnswer("test");
    answer2.setQuestionType(questionType);
    answer2.setSortOrder(1);
    questionType.getAnswers().add(answer2);

    questionTypeService.save(patient1.getId(), questionType);

    List<QuestionType> questionTypes = questionTypeService.findAll();
    assertEquals(questionTypesOld.size() + 1, questionTypes.size());

    assertEquals(
        questionType.getUser().getId(),
        questionTypes.get(questionTypes.size() - 1).getUser().getId());
  }
  /** @throws Exception */
  @Test
  public void findForUser() throws Exception {
    User patient1 = userService.findUserByLogin("sysuser1");
    assertNotNull(patient1);

    QuestionType qt =
        questionTypeService.find(
            patient1.getId(), QuestionType.Type.SINGLE_CHOICE_LIST, AnswerDataType.BOOLEAN);
    assertNotNull(qt);
  }
  /** @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]));
    }
  }