@Test public void postSlotInvalidPayload() throws Exception { Provider provider = new Provider(); provider.setVmmUrl(mBaseURL); ProviderDAO providerDAO = mock(ProviderDAO.class); when(providerDAO.getById(1)).thenReturn(provider); ProviderSlot providerSlot = new ProviderSlot(); providerSlot.providerDAO = providerDAO; Response response = providerSlot.postSlots(1, "invalid"); assertEquals(400, response.getStatus()); String responseBody = (String) response.getEntity(); assertEquals("Invalid payload: invalid", responseBody); }
@POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postSlots(@PathParam("id") int id, String payload) { logger.info("POST /" + id + "/slots" + " | with payload " + payload); Provider provider = providerDAO.getById(id); if(provider != null) { VmRequirements vmRequirements = null; Gson gson = new Gson(); try { vmRequirements = gson.fromJson(payload, VmRequirements.class); } catch(Exception ex) { logger.error("Trying to passe the following string to VmRequirements: " + payload); logger.error(ex.getMessage()); return buildResponse(Status.BAD_REQUEST, "Invalid payload: " + payload); } VmManagerClient vmm = new VmManagerClient(provider.getVmmUrl()); List<Slot> slots = vmm.getSlots(vmRequirements); String slotsPayload = gson.toJson(slots); return buildResponse(Status.OK, slotsPayload); } else { logger.debug("RESPONSE: 404"); return buildResponse(Status.NOT_FOUND, "No provider by that id found in the databae."); } }
ProviderSlot extends RestAbstract { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postSlots(@PathParam("id") int id, String payload) { logger.info("POST /" + id + "/slots" + " | with payload " + payload); Provider provider = providerDAO.getById(id); if(provider != null) { VmRequirements vmRequirements = null; Gson gson = new Gson(); try { vmRequirements = gson.fromJson(payload, VmRequirements.class); } catch(Exception ex) { logger.error("Trying to passe the following string to VmRequirements: " + payload); logger.error(ex.getMessage()); return buildResponse(Status.BAD_REQUEST, "Invalid payload: " + payload); } VmManagerClient vmm = new VmManagerClient(provider.getVmmUrl()); List<Slot> slots = vmm.getSlots(vmRequirements); String slotsPayload = gson.toJson(slots); return buildResponse(Status.OK, slotsPayload); } else { logger.debug("RESPONSE: 404"); return buildResponse(Status.NOT_FOUND, "No provider by that id found in the databae."); } } }
ProviderSlot extends RestAbstract { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postSlots(@PathParam("id") int id, String payload) { logger.info("POST /" + id + "/slots" + " | with payload " + payload); Provider provider = providerDAO.getById(id); if(provider != null) { VmRequirements vmRequirements = null; Gson gson = new Gson(); try { vmRequirements = gson.fromJson(payload, VmRequirements.class); } catch(Exception ex) { logger.error("Trying to passe the following string to VmRequirements: " + payload); logger.error(ex.getMessage()); return buildResponse(Status.BAD_REQUEST, "Invalid payload: " + payload); } VmManagerClient vmm = new VmManagerClient(provider.getVmmUrl()); List<Slot> slots = vmm.getSlots(vmRequirements); String slotsPayload = gson.toJson(slots); return buildResponse(Status.OK, slotsPayload); } else { logger.debug("RESPONSE: 404"); return buildResponse(Status.NOT_FOUND, "No provider by that id found in the databae."); } }  }
ProviderSlot extends RestAbstract { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postSlots(@PathParam("id") int id, String payload) { logger.info("POST /" + id + "/slots" + " | with payload " + payload); Provider provider = providerDAO.getById(id); if(provider != null) { VmRequirements vmRequirements = null; Gson gson = new Gson(); try { vmRequirements = gson.fromJson(payload, VmRequirements.class); } catch(Exception ex) { logger.error("Trying to passe the following string to VmRequirements: " + payload); logger.error(ex.getMessage()); return buildResponse(Status.BAD_REQUEST, "Invalid payload: " + payload); } VmManagerClient vmm = new VmManagerClient(provider.getVmmUrl()); List<Slot> slots = vmm.getSlots(vmRequirements); String slotsPayload = gson.toJson(slots); return buildResponse(Status.OK, slotsPayload); } else { logger.debug("RESPONSE: 404"); return buildResponse(Status.NOT_FOUND, "No provider by that id found in the databae."); } }  @GET Response getSlot(@PathParam("id") int id); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response postSlots(@PathParam("id") int id, String payload); }
ProviderSlot extends RestAbstract { @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public Response postSlots(@PathParam("id") int id, String payload) { logger.info("POST /" + id + "/slots" + " | with payload " + payload); Provider provider = providerDAO.getById(id); if(provider != null) { VmRequirements vmRequirements = null; Gson gson = new Gson(); try { vmRequirements = gson.fromJson(payload, VmRequirements.class); } catch(Exception ex) { logger.error("Trying to passe the following string to VmRequirements: " + payload); logger.error(ex.getMessage()); return buildResponse(Status.BAD_REQUEST, "Invalid payload: " + payload); } VmManagerClient vmm = new VmManagerClient(provider.getVmmUrl()); List<Slot> slots = vmm.getSlots(vmRequirements); String slotsPayload = gson.toJson(slots); return buildResponse(Status.OK, slotsPayload); } else { logger.debug("RESPONSE: 404"); return buildResponse(Status.NOT_FOUND, "No provider by that id found in the databae."); } }  @GET Response getSlot(@PathParam("id") int id); @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) Response postSlots(@PathParam("id") int id, String payload);  }