### Question:
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);  }### Answer:
@Test public void postSlot404Test() throws Exception { ProviderDAO providerDAO = mock(ProviderDAO.class); when(providerDAO.getById(1)).thenReturn(null); ProviderSlot providerSlot = new ProviderSlot(); providerSlot.providerDAO = providerDAO; Response response = providerSlot.postSlots(1, ""); assertEquals(404, response.getStatus()); String providerXML = (String) response.getEntity(); assertEquals("No provider by that id found in the databae.", providerXML); }
@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); }
@Test public void postSlot() 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; String clientPayload = "{" + "\"cpus\": 2," + "\"ramMb\": 512, " + "\"diskGb\": 333, " + "\"swapMb\": 0" + "}"; mServer.addPath("/slots", payload); Response response = providerSlot.postSlots(1, clientPayload); String payloadToVMM = mServer.getRequestBody(); Gson gson = new Gson(); VmRequirements vmRequirements = gson.fromJson(payloadToVMM, VmRequirements.class); assertEquals(2, vmRequirements.getCpus()); assertEquals(512, vmRequirements.getRamMb()); assertEquals(333, vmRequirements.getDiskGb()); assertEquals(0, vmRequirements.getSwapMb()); assertEquals(200, response.getStatus()); String responseBody = (String) response.getEntity(); Type collectionType = new TypeToken<List<Slot>>(){}.getType(); List<Slot> slots = gson.fromJson(responseBody, collectionType); assertEquals("wally152", slots.get(0).getHostname()); assertEquals(2.0, slots.get(0).getFreeCpus(), 0.001); }