Skip to content

Commit

Permalink
BAH-966 | Api authentication across tabs (Bahmni#25)
Browse files Browse the repository at this point in the history
* Raju, Sowmika | [Authentication Fix] | Handle authentication in controllers

* Sowmika | Handle authentication in recurring appointment controller

Co-authored-by: Raju R <[email protected]>
  • Loading branch information
2 people authored and bsneha90TW committed Jan 10, 2020
1 parent cded3a3 commit 2a689cb
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.openmrs.module.appointments.web.mapper.AppointmentServiceMapper;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -31,7 +32,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/appointment")
public class AppointmentController {
public class AppointmentController extends BaseRestController {

private Log log = LogFactory.getLog(this.getClass());

Expand All @@ -47,7 +48,6 @@ public class AppointmentController {
@Autowired
private AppointmentServiceMapper appointmentServiceMapper;


@RequestMapping(method = RequestMethod.GET, value = "all")
@ResponseBody
public List<AppointmentDefaultResponse> getAllAppointments(@RequestParam(value = "forDate", required = false) String forDate) throws ParseException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.openmrs.module.appointments.web.contract.AppointmentServiceFullResponse;
import org.openmrs.module.appointments.web.mapper.AppointmentServiceMapper;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -21,7 +22,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/appointmentService")
public class AppointmentServiceController {
public class AppointmentServiceController extends BaseRestController {

@Autowired
private AppointmentServiceDefinitionService appointmentServiceDefinitionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.openmrs.module.appointments.web.contract.AppointmentServiceFullResponse;
import org.openmrs.module.appointments.web.mapper.AppointmentServiceMapper;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -14,7 +15,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/appointment-services")
public class AppointmentServicesController {
public class AppointmentServicesController extends BaseRestController {

@Autowired
private AppointmentServiceDefinitionService appointmentServiceDefinitionService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.openmrs.module.appointments.web.validators.AppointmentSearchValidator;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -37,7 +38,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/appointments")
public class AppointmentsController {
public class AppointmentsController extends BaseRestController {
@Autowired
private AppointmentsService appointmentsService;
@Autowired
Expand Down Expand Up @@ -94,18 +95,14 @@ public List<AppointmentDefaultResponse> search(@Valid @RequestBody AppointmentSe
@RequestMapping(method = RequestMethod.POST, value = "/{appointmentUuid}/status-change")
@ResponseBody
public ResponseEntity<Object> transitionAppointment(@PathVariable("appointmentUuid") String appointmentUuid, @RequestBody Map<String, String> statusDetails) throws ParseException {
try {
String toStatus = statusDetails.get("toStatus");
Date onDate = DateUtil.convertToLocalDateFromUTC(statusDetails.get("onDate"));
Appointment appointment = appointmentsService.getAppointmentByUuid(appointmentUuid);
if (appointment != null) {
appointmentsService.changeStatus(appointment, toStatus, onDate);
return new ResponseEntity<>(appointmentMapper.constructResponse(appointment), HttpStatus.OK);
} else
throw new RuntimeException("Appointment does not exist");
} catch (RuntimeException e) {
return new ResponseEntity<>(RestUtil.wrapErrorResponse(e, e.getMessage()), HttpStatus.BAD_REQUEST);
}
String toStatus = statusDetails.get("toStatus");
Date onDate = DateUtil.convertToLocalDateFromUTC(statusDetails.get("onDate"));
Appointment appointment = appointmentsService.getAppointmentByUuid(appointmentUuid);
if (appointment != null) {
appointmentsService.changeStatus(appointment, toStatus, onDate);
return new ResponseEntity<>(appointmentMapper.constructResponse(appointment), HttpStatus.OK);
} else
throw new RuntimeException("Appointment does not exist");
}

@RequestMapping(method = RequestMethod.POST, value = "/conflicts")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.openmrs.module.appointments.web.validators.TimeZoneValidator;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.RestUtil;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -44,7 +45,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/recurring-appointments")
public class RecurringAppointmentsController {
public class RecurringAppointmentsController extends BaseRestController {

private Log log = LogFactory.getLog(this.getClass());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.openmrs.module.appointments.model.Speciality;
import org.openmrs.module.appointments.service.SpecialityService;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -15,7 +16,7 @@

@Controller
@RequestMapping(value = "/rest/" + RestConstants.VERSION_1 + "/speciality")
public class SpecialityController {
public class SpecialityController extends BaseRestController {

@Autowired
private SpecialityService specialityService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import org.codehaus.jackson.type.TypeReference;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.api.APIException;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.dao.AppointmentAuditDao;
import org.openmrs.module.appointments.model.Appointment;
Expand Down Expand Up @@ -38,6 +41,9 @@ public class AppointmentControllerIT extends BaseIntegrationTest {
@Autowired
AppointmentAuditDao appointmentAuditDao;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() throws Exception {
executeDataSet("appointmentTestData.xml");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,29 +87,29 @@ public void shouldGetAllFutureAppointmentsForGivenServiceType() throws Exception
assertEquals(1, appointmentDefaultResponses.size());
assertEquals(appointment.getUuid(), appointmentDefaultResponses.get(0).getUuid());
}

@Test
public void shouldGetAllAppointments() throws Exception {
Appointment appointment = new Appointment();
List<Appointment> appointmentList = new ArrayList<>();
appointmentList.add(appointment);
when(appointmentsService.getAllAppointments(null)).thenReturn(appointmentList);

appointmentController.getAllAppointments(null);
verify(appointmentsService, times(1)).getAllAppointments(null);
verify(appointmentMapper, times(1)).constructResponse(appointmentList);
}

@Test
public void shouldGetAllAppointmentsForDate() throws Exception {
Appointment appointment = new Appointment();
List<Appointment> appointmentList = new ArrayList<>();
appointmentList.add(appointment);
String dateString = "2017-08-15T00:00:00.0Z";
Date forDate = DateUtil.convertToLocalDateFromUTC(dateString);

when(appointmentsService.getAllAppointments(forDate)).thenReturn(appointmentList);

appointmentController.getAllAppointments(dateString);
verify(appointmentsService, times(1)).getAllAppointments(forDate);
verify(appointmentMapper, times(1)).constructResponse(appointmentList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.openmrs.api.context.Context;
import org.openmrs.module.appointments.dao.AppointmentAuditDao;
import org.openmrs.module.appointments.model.Appointment;
Expand Down Expand Up @@ -34,6 +37,9 @@ public class AppointmentsControllerIT extends BaseIntegrationTest {
@Autowired
AppointmentAuditDao appointmentAuditDao;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() throws Exception {
executeDataSet("appointmentTestData.xml");
Expand Down Expand Up @@ -168,22 +174,22 @@ public void should_changeAppointmentStatusWithoutDate() throws Exception {

@Test
public void should_throwExceptionForInvalidStatusChange() throws Exception {
String content = "{ \"toStatus\": \"Missed\"}";
String content = "{ \"toStatus\": \"Completed\"}";

MockHttpServletResponse response = handle(newPostRequest("/rest/v1/appointments/75504r42-3ca8-11e3-bf2b-0800271c13555/status-change", content));
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Appointment status can not be changed from Missed to Completed");

assertNotNull(response);
assertEquals(400, response.getStatus());
MockHttpServletResponse response = handle(newPostRequest("/rest/v1/appointments/75504r42-3ca8-11e3-bf2b-0800271c13555/status-change", content));
}

@Test
public void should_throwExceptionForInvalidAppointment() throws Exception {
String content = "{ \"toStatus\": \"Scheduled\"}";

MockHttpServletResponse response = handle(newPostRequest("/rest/v1/appointments/c36006e5-9fbb-4f20-866b-0ece245615a8/status-change", content));
expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Appointment does not exist");

assertNotNull(response);
assertEquals(400, response.getStatus());
MockHttpServletResponse response = handle(newPostRequest("/rest/v1/appointments/c36006e5-9fbb-4f20-866b-0ece245615a8/status-change", content));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@


import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
Expand Down Expand Up @@ -59,6 +61,9 @@ public class AppointmentsControllerTest {
@InjectMocks
private AppointmentsController appointmentsController;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Before
public void setUp() throws Exception {
initMocks(this);
Expand Down Expand Up @@ -208,6 +213,9 @@ public void shouldReturnErrorResponseWhenAppointmentDoesNotExist() throws Except
statusDetails.put("toStatus", "Completed");
when(appointmentsService.getAppointmentByUuid(anyString())).thenReturn(null);

expectedException.expect(RuntimeException.class);
expectedException.expectMessage("Appointment does not exist");

appointmentsController.transitionAppointment("appointmentUuid", statusDetails);

verify(appointmentsService, times(1)).getAppointmentByUuid("appointmentUuid");
Expand Down

0 comments on commit 2a689cb

Please sign in to comment.