Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM2-136 : Appointment resource initial implementation #136

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api;

import javax.validation.constraints.NotNull;

import org.hl7.fhir.r4.model.Appointment;

public interface FhirAppointmentService {

Appointment getAppointmentByUuid(@NotNull String uuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.dao;

import javax.validation.constraints.NotNull;

public interface FhirAppointmentDao<T> {

T getAppointmentByUuid(@NotNull String uuid);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.impl;

import org.apache.commons.lang3.NotImplementedException;
import org.hl7.fhir.r4.model.Appointment;
import org.openmrs.module.fhir2.api.FhirAppointmentService;
import org.springframework.stereotype.Component;

@Component
public class FhirAppointmentServiceImpl implements FhirAppointmentService {

@Override
public Appointment getAppointmentByUuid(String uuid) {
throw new NotImplementedException("NotImplementedException");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.translators;

import org.hl7.fhir.r4.model.Appointment;

public interface AppointmentTranslator<T> extends OpenmrsFhirUpdatableTranslator<T, Appointment> {

/**
* Maps OpenMRS appointment to {@link org.hl7.fhir.r4.model.Appointment}
*
* @param appointment the OpenMRS data element to translate
* @return the corresponding FHIR Appointment resource
*/
@Override
Appointment toFhirResource(T appointment);

/**
* Maps {@link org.hl7.fhir.r4.model.Appointment} to OpenMRS appointment object
*
* @param existingAppointment
* @param appointment the resource to map
* @return the updated OpenMRS appointment object
*/
@Override
T toOpenmrsType(T existingAppointment, Appointment appointment);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.api.impl;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.nullValue;

import org.apache.commons.lang3.NotImplementedException;
import org.hl7.fhir.r4.model.Appointment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class FhirAppointmentServiceImplTest {

private static final String APPOINTMENT_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private FhirAppointmentServiceImpl appointmentService;

private Appointment appointment;

@Before
public void setup() {
appointmentService = new FhirAppointmentServiceImpl();

appointment = new Appointment();
appointment.setId(APPOINTMENT_UUID);
}

@Test(expected = NotImplementedException.class)
public void getAppointmentByUuid_shouldThrowNotImplementedException() {
assertThat(appointmentService.getAppointmentByUuid(APPOINTMENT_UUID), nullValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.providers;

import javax.validation.constraints.NotNull;

import ca.uhn.fhir.rest.annotation.IdParam;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.server.IResourceProvider;
import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import lombok.AccessLevel;
import lombok.Setter;
import org.hl7.fhir.instance.model.api.IBaseResource;
import org.hl7.fhir.r4.model.Appointment;
import org.hl7.fhir.r4.model.IdType;
import org.openmrs.module.fhir2.api.FhirAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
@Qualifier("fhirResources")
@Setter(AccessLevel.PACKAGE)
public class AppointmentFhirResourceProvider implements IResourceProvider {

@Autowired
private FhirAppointmentService appointmentService;

@Override
public Class<? extends IBaseResource> getResourceType() {
return Appointment.class;
}

@Read
@SuppressWarnings("unused")
public Appointment getAppointmentByUuid(@IdParam @NotNull IdType id) {
Appointment appointment = appointmentService.getAppointmentByUuid(id.getIdPart());
if (appointment == null) {
throw new ResourceNotFoundException("Could not find Appointment with Id " + id.getIdPart());
}
return appointment;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.providers;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.when;

import ca.uhn.fhir.rest.server.exceptions.ResourceNotFoundException;
import org.hl7.fhir.r4.model.Appointment;
import org.hl7.fhir.r4.model.IdType;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirAppointmentService;
import org.openmrs.module.fhir2.web.servlet.BaseFhirProvenanceResourceTest;

@RunWith(MockitoJUnitRunner.class)
public class AppointmentFhirResourceProviderTest extends BaseFhirProvenanceResourceTest<Appointment> {

private static final String APPOINTMENT_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private static final String WRONG_APPOINTMENT_UUID = "1010AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

@Mock
private FhirAppointmentService appointmentService;

private Appointment appointment;

private AppointmentFhirResourceProvider resourceProvider;

@Before
public void setUp() {
resourceProvider = new AppointmentFhirResourceProvider();
resourceProvider.setAppointmentService(appointmentService);
}

@Before
public void initAppointment() {
appointment = new Appointment();
appointment.setId(APPOINTMENT_UUID);
}

@Test
public void getResourceType_shouldReturnResourceType() {
assertThat(resourceProvider.getResourceType(), equalTo(Appointment.class));
assertThat(resourceProvider.getResourceType().getName(), equalTo(Appointment.class.getName()));
}

@Test
public void getAppointmentByUuid_shouldReturnMatchingAppointment() {
when(appointmentService.getAppointmentByUuid(APPOINTMENT_UUID)).thenReturn(appointment);
IdType id = new IdType();
id.setValue(APPOINTMENT_UUID);
Appointment appointment = resourceProvider.getAppointmentByUuid(id);
assertThat(appointment, notNullValue());
assertThat(appointment.getId(), notNullValue());
assertThat(appointment.getId(), equalTo(APPOINTMENT_UUID));
}

@Test(expected = ResourceNotFoundException.class)
public void getAppointmentWithWrongUuid_shouldThrowResourceNotFoundException() {
IdType id = new IdType();
id.setValue(WRONG_APPOINTMENT_UUID);
Appointment result = resourceProvider.getAppointmentByUuid(id);
assertThat(result, nullValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.fhir2.providers;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Mockito.when;

import lombok.AccessLevel;
import lombok.Getter;
import org.hl7.fhir.r4.model.Appointment;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.openmrs.module.fhir2.api.FhirAppointmentService;
import org.openmrs.module.fhir2.web.servlet.BaseFhirResourceProviderTest;
import org.springframework.mock.web.MockHttpServletResponse;

@RunWith(MockitoJUnitRunner.class)
public class AppointmentFhirResourceProviderWebTest extends BaseFhirResourceProviderTest<AppointmentFhirResourceProvider, Appointment> {

private static final String APPOINTMENT_UUID = "1085AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

private static final String WRONG_APPOINTMENT_UUID = "1010AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";

@Mock
private FhirAppointmentService appointmentService;

@Getter(AccessLevel.PUBLIC)
private AppointmentFhirResourceProvider resourceProvider;

@Before
@Override
public void setup() throws Exception {
resourceProvider = new AppointmentFhirResourceProvider();
resourceProvider.setAppointmentService(appointmentService);
super.setup();
}

@Test
public void shouldReturnAppointmentByUuid() throws Exception {
Appointment appointment = new Appointment();
appointment.setId(APPOINTMENT_UUID);
when(appointmentService.getAppointmentByUuid(APPOINTMENT_UUID)).thenReturn(appointment);

MockHttpServletResponse response = get("/Appointment/" + APPOINTMENT_UUID).accept(FhirMediaTypes.JSON).go();

assertThat(response, isOk());
assertThat(response.getContentType(), equalTo(FhirMediaTypes.JSON.toString()));

Appointment result = readResponse(response);
assertThat(result.getIdElement().getIdPart(), equalTo(APPOINTMENT_UUID));
}

@Test
public void shouldReturn404IfAppointmentNotFound() throws Exception {
when(appointmentService.getAppointmentByUuid(WRONG_APPOINTMENT_UUID)).thenReturn(null);

MockHttpServletResponse response = get("/Appointment/" + WRONG_APPOINTMENT_UUID).accept(FhirMediaTypes.JSON).go();

assertThat(response, isNotFound());
}
}