@Test
  public void shouldSubmitConsultNoteWithOptionalAdditionalObservationsWithoutValur()
      throws Exception {
    int primaryConceptNameId = 2460;

    String diagnosisJson =
        "{ \"certainty\": \"PRESUMED\", \"diagnosisOrder\": \"PRIMARY\", \"diagnosis\": \""
            + CodedOrFreeTextAnswer.CONCEPT_NAME_PREFIX
            + primaryConceptNameId
            + "\" }";

    String additionalObsJson = "";

    Concept conceptFor2460 = new Concept();
    final ConceptName conceptName2460 = new ConceptName();
    conceptName2460.setConcept(conceptFor2460);

    when(conceptService.getConceptName(primaryConceptNameId)).thenReturn(conceptName2460);

    Patient patient = new Patient();
    patient.addName(new PersonName("Jean", "Paul", "Marie"));
    final Location sessionLocation = new Location();
    final Provider currentProvider = new Provider();

    DispositionFactory dispositionFactory = mock(DispositionFactory.class);

    EmrContext emrContext = new EmrContext();
    emrContext.setSessionLocation(sessionLocation);
    emrContext.setCurrentProvider(currentProvider);

    MockHttpSession httpSession = new MockHttpSession();
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    ConsultPageController controller = new ConsultPageController();
    String result =
        controller.post(
            patient,
            asList(diagnosisJson),
            "",
            asList(additionalObsJson),
            "",
            httpSession,
            httpServletRequest,
            consultService,
            conceptService,
            dispositionFactory,
            emrContext,
            new TestUiUtils());

    verify(consultService)
        .saveConsultNote(
            argThat(
                new ArgumentMatcher<ConsultNote>() {
                  @Override
                  public boolean matches(Object o) {
                    ConsultNote actual = (ConsultNote) o;
                    return containsInAnyOrder(
                                new Diagnosis(
                                    new CodedOrFreeTextAnswer(conceptName2460),
                                    Diagnosis.Order.PRIMARY))
                            .matches(actual.getDiagnoses())
                        && actual.getAdditionalObs().size() == 0;
                  }
                }));
  }
  @Test
  public void testSubmit() throws Exception {
    int primaryConceptNameId = 2460;
    int secondaryConceptId = 3;
    final String secondaryText = "Fatigue from too much testing";
    final String freeTextComments = "30 year old male, presenting with...";

    String diagnosisJson1 =
        "{ \"certainty\": \"PRESUMED\", \"diagnosisOrder\": \"PRIMARY\", \"diagnosis\": \""
            + CodedOrFreeTextAnswer.CONCEPT_NAME_PREFIX
            + primaryConceptNameId
            + "\" }";
    String diagnosisJson2 =
        "{ \"certainty\": \"PRESUMED\", \"diagnosisOrder\": \"SECONDARY\", \"diagnosis\": \""
            + CodedOrFreeTextAnswer.CONCEPT_PREFIX
            + secondaryConceptId
            + "\" }";
    String diagnosisJson3 =
        "{ \"certainty\": \"PRESUMED\", \"diagnosisOrder\": \"SECONDARY\", \"diagnosis\": \""
            + CodedOrFreeTextAnswer.NON_CODED_PREFIX
            + secondaryText
            + "\" }";

    Concept conceptFor2460 = new Concept();
    final ConceptName conceptName2460 = new ConceptName();
    conceptName2460.setConcept(conceptFor2460);

    final Concept concept3 = new Concept();

    when(conceptService.getConceptName(primaryConceptNameId)).thenReturn(conceptName2460);
    when(conceptService.getConcept(secondaryConceptId)).thenReturn(concept3);

    Patient patient = new Patient();
    patient.addName(new PersonName("Jean", "Paul", "Marie"));
    final Location sessionLocation = new Location();
    final Provider currentProvider = new Provider();

    EmrContext emrContext = new EmrContext();
    emrContext.setSessionLocation(sessionLocation);
    emrContext.setCurrentProvider(currentProvider);

    DispositionFactory dispositionFactory = mock(DispositionFactory.class);

    MockHttpSession httpSession = new MockHttpSession();
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    ConsultPageController controller = new ConsultPageController();
    String result =
        controller.post(
            patient,
            asList(diagnosisJson1, diagnosisJson2, diagnosisJson3),
            "", // no disposition
            null,
            freeTextComments,
            httpSession,
            httpServletRequest,
            consultService,
            conceptService,
            dispositionFactory,
            emrContext,
            new TestUiUtils());

    assertThat(result, startsWith("redirect:"));
    assertThat(
        httpSession.getAttribute(EmrConstants.SESSION_ATTRIBUTE_INFO_MESSAGE), notNullValue());

    verify(consultService)
        .saveConsultNote(
            argThat(
                new ArgumentMatcher<ConsultNote>() {
                  @Override
                  public boolean matches(Object o) {
                    ConsultNote actual = (ConsultNote) o;
                    return containsInAnyOrder(
                                new Diagnosis(
                                    new CodedOrFreeTextAnswer(conceptName2460),
                                    Diagnosis.Order.PRIMARY),
                                new Diagnosis(
                                    new CodedOrFreeTextAnswer(concept3), Diagnosis.Order.SECONDARY),
                                new Diagnosis(
                                    new CodedOrFreeTextAnswer(secondaryText),
                                    Diagnosis.Order.SECONDARY))
                            .matches(actual.getDiagnoses())
                        && actual.getComments().equals(freeTextComments)
                        && actual.getEncounterLocation().equals(sessionLocation)
                        && actual.getClinician().equals(currentProvider);
                  }
                }));
  }
  @Test
  public void shouldSubmitEDConsultNoteWithAdditionalObservationsOfTypeDate() throws Exception {
    int primaryConceptNameId = 2460;

    String diagnosisJson =
        "{ \"certainty\": \"PRESUMED\", \"diagnosisOrder\": \"PRIMARY\", \"diagnosis\": \""
            + CodedOrFreeTextAnswer.CONCEPT_NAME_PREFIX
            + primaryConceptNameId
            + "\" }";

    String additionalObsJson =
        "{ \"concept\": \"uuid-123\", \"value\": \"2013-05-21 17:23:47\", \"datatype\": \"Date\"}";

    Concept conceptFor2460 = new Concept();
    final ConceptName conceptName2460 = new ConceptName();
    conceptName2460.setConcept(conceptFor2460);

    when(conceptService.getConceptName(primaryConceptNameId)).thenReturn(conceptName2460);

    final Concept conceptForAdditionalObs = new Concept();
    conceptForAdditionalObs.setUuid("uuid-123");
    Calendar calendar = new GregorianCalendar(2013, 04, 21, 17, 23, 47);
    final Date dateForAdditionalObs = calendar.getTime();

    when(conceptService.getConceptByUuid("uuid-123")).thenReturn(conceptForAdditionalObs);

    Patient patient = new Patient();
    patient.addName(new PersonName("Jean", "Paul", "Marie"));
    final Location sessionLocation = new Location();
    final Provider currentProvider = new Provider();

    DispositionFactory dispositionFactory = mock(DispositionFactory.class);

    EmrContext emrContext = new EmrContext();
    emrContext.setSessionLocation(sessionLocation);
    emrContext.setCurrentProvider(currentProvider);

    MockHttpSession httpSession = new MockHttpSession();
    MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
    ConsultPageController controller = new ConsultPageController();
    String result =
        controller.post(
            patient,
            asList(diagnosisJson),
            "",
            asList(additionalObsJson),
            "",
            httpSession,
            httpServletRequest,
            consultService,
            conceptService,
            dispositionFactory,
            emrContext,
            new TestUiUtils());

    final Obs traumaObs = new Obs();
    traumaObs.setConcept(conceptForAdditionalObs);
    traumaObs.setValueDate(dateForAdditionalObs);

    verify(consultService)
        .saveConsultNote(
            argThat(
                new ArgumentMatcher<ConsultNote>() {
                  @Override
                  public boolean matches(Object o) {
                    ConsultNote actual = (ConsultNote) o;
                    Obs actualObs = actual.getAdditionalObs().get(0);
                    return containsInAnyOrder(
                                new Diagnosis(
                                    new CodedOrFreeTextAnswer(conceptName2460),
                                    Diagnosis.Order.PRIMARY))
                            .matches(actual.getDiagnoses())
                        && actual.getAdditionalObs().size() == 1
                        && actualObs.getConcept() == conceptForAdditionalObs
                        && actualObs.getValueDate().equals(dateForAdditionalObs);
                  }
                }));
  }