@Test public void return403IfAgreementNotInDBOrDoesNotBelongToDeploymentTest() throws Exception { String payload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<agreement xmlns=\"http: + "<id>33</id>" + "<price>222</price>" + "<sla-agreement-id>sla-agreement-id</sla-agreement-id>" + "<provider-id>provider-id</provider-id>" + "<accepted>true</accepted>" + "<link rel=\"deployment\" href=\"/applications/101/deployments/22\" type=\"application/xml\" />" + "<link rel=\"self\" href=\"/applications/101/deployments/22/agreements/44\" type=\"application/xml\" />" + "</agreement>"; Agreement agreement1 = new Agreement(); agreement1.setAccepted(false); agreement1.setId(1); agreement1.setPrice("111"); agreement1.setProviderId("provider-id1"); agreement1.setSlaAgreement("sssas1"); agreement1.setSlaAgreementId("sla-agreement-id1"); Agreement agreement2 = new Agreement(); agreement2.setAccepted(true); agreement2.setId(2); agreement2.setPrice("222"); agreement2.setProviderId("provider-id"); agreement2.setSlaAgreement("sssas"); agreement2.setSlaAgreementId("sla-agreement-id"); Deployment deployment = new Deployment(); deployment.addAgreement(agreement1); deployment.addAgreement(agreement2); DeploymentDAO deploymentDAO = mock(DeploymentDAO.class); when(deploymentDAO.getById(22)).thenReturn(deployment); AgreementRest agreementRest = new AgreementRest(); agreementRest.deploymentDAO = deploymentDAO; Response response = agreementRest.acceptSlaAgreement("101", "22", "44", payload); assertEquals(403, response.getStatus()); assertEquals("Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment.", (String) response.getEntity()); deployment = new Deployment(); deployment.setAgreements(null); response = agreementRest.acceptSlaAgreement("101", "22", "44", payload); assertEquals(403, response.getStatus()); assertEquals("Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment.", (String) response.getEntity()); deployment = new Deployment(); deployment.setAgreements(new ArrayList<Agreement>()); response = agreementRest.acceptSlaAgreement("101", "22", "44", payload); assertEquals(403, response.getStatus()); assertEquals("Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment.", (String) response.getEntity()); }
@PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response acceptSlaAgreement(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("agreement_id") String agreementId, String payload) throws Exception { Agreement agreement = ModelConverter.xmlAgreementToObject(payload); PriceModellerClient.getInstance().initializeApplication(applicationName, Integer.parseInt(deploymentId), 1); if(agreement.isAccepted()) { Deployment deployment = deploymentDAO.getById(Integer.parseInt(deploymentId)); List<Agreement> agreements = deployment.getAgreements(); Agreement agreementInDB = deploymentContainsAgreement(agreements, Integer.parseInt(agreementId)); if(agreementInDB != null) { if(deploymentDosNotContainsAcceptedAgreement(agreements)) { Timestamp now = new Timestamp(System.currentTimeMillis()); if(TimeStampComparator.isInTheFuture(now, agreementInDB.getValidUntil())) { SLASOITemplateParser parser = new SLASOITemplateParser(); SLATemplate slat = parser.parseTemplate(agreementInDB.getSlaAgreement()); SlaTranslator slaTranslator = new SlaTranslatorImplNoOsgi(); client.setSlaTranslator(slaTranslator); logger.debug("Sending create agreement SOAP request..."); SLA slaAgreement = client.createAgreement(Configuration.slamURL, slat, agreementInDB.getNegotiationId()); logger.debug("SLA:"); logger.debug(slaAgreement); agreementInDB.setAccepted(true); agreementInDB.setSlaAgreement(slaAgreement.getUuid()); agreementDAO.update(agreementInDB); deployment.setStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZATION); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setApplicationName(applicationName); deploymentEvent.setDeploymentId(deployment.getId()); deploymentEvent.setDeploymentStatus(deployment.getStatus()); deploymentDAO.update(deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); return buildResponse(Status.ACCEPTED, XMLBuilder.getAgreementXML(agreementInDB, applicationName, Integer.parseInt(deploymentId))); } else { return buildResponse(Status.FORBIDDEN, "The selected agreement has already expired. It is necessary to restart the deployment process."); } } else { return buildResponse(Status.FORBIDDEN, "This agreement or a different one from the deployment has been previously accepted."); } } else { return buildResponse(Status.FORBIDDEN, "Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment."); } } else { return buildResponse(Status.BAD_REQUEST, "The only valid change of state it is <accepted>true</accepted>"); } }
AgreementRest extends AbstractRest { @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response acceptSlaAgreement(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("agreement_id") String agreementId, String payload) throws Exception { Agreement agreement = ModelConverter.xmlAgreementToObject(payload); PriceModellerClient.getInstance().initializeApplication(applicationName, Integer.parseInt(deploymentId), 1); if(agreement.isAccepted()) { Deployment deployment = deploymentDAO.getById(Integer.parseInt(deploymentId)); List<Agreement> agreements = deployment.getAgreements(); Agreement agreementInDB = deploymentContainsAgreement(agreements, Integer.parseInt(agreementId)); if(agreementInDB != null) { if(deploymentDosNotContainsAcceptedAgreement(agreements)) { Timestamp now = new Timestamp(System.currentTimeMillis()); if(TimeStampComparator.isInTheFuture(now, agreementInDB.getValidUntil())) { SLASOITemplateParser parser = new SLASOITemplateParser(); SLATemplate slat = parser.parseTemplate(agreementInDB.getSlaAgreement()); SlaTranslator slaTranslator = new SlaTranslatorImplNoOsgi(); client.setSlaTranslator(slaTranslator); logger.debug("Sending create agreement SOAP request..."); SLA slaAgreement = client.createAgreement(Configuration.slamURL, slat, agreementInDB.getNegotiationId()); logger.debug("SLA:"); logger.debug(slaAgreement); agreementInDB.setAccepted(true); agreementInDB.setSlaAgreement(slaAgreement.getUuid()); agreementDAO.update(agreementInDB); deployment.setStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZATION); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setApplicationName(applicationName); deploymentEvent.setDeploymentId(deployment.getId()); deploymentEvent.setDeploymentStatus(deployment.getStatus()); deploymentDAO.update(deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); return buildResponse(Status.ACCEPTED, XMLBuilder.getAgreementXML(agreementInDB, applicationName, Integer.parseInt(deploymentId))); } else { return buildResponse(Status.FORBIDDEN, "The selected agreement has already expired. It is necessary to restart the deployment process."); } } else { return buildResponse(Status.FORBIDDEN, "This agreement or a different one from the deployment has been previously accepted."); } } else { return buildResponse(Status.FORBIDDEN, "Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment."); } } else { return buildResponse(Status.BAD_REQUEST, "The only valid change of state it is <accepted>true</accepted>"); } } }
AgreementRest extends AbstractRest { @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response acceptSlaAgreement(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("agreement_id") String agreementId, String payload) throws Exception { Agreement agreement = ModelConverter.xmlAgreementToObject(payload); PriceModellerClient.getInstance().initializeApplication(applicationName, Integer.parseInt(deploymentId), 1); if(agreement.isAccepted()) { Deployment deployment = deploymentDAO.getById(Integer.parseInt(deploymentId)); List<Agreement> agreements = deployment.getAgreements(); Agreement agreementInDB = deploymentContainsAgreement(agreements, Integer.parseInt(agreementId)); if(agreementInDB != null) { if(deploymentDosNotContainsAcceptedAgreement(agreements)) { Timestamp now = new Timestamp(System.currentTimeMillis()); if(TimeStampComparator.isInTheFuture(now, agreementInDB.getValidUntil())) { SLASOITemplateParser parser = new SLASOITemplateParser(); SLATemplate slat = parser.parseTemplate(agreementInDB.getSlaAgreement()); SlaTranslator slaTranslator = new SlaTranslatorImplNoOsgi(); client.setSlaTranslator(slaTranslator); logger.debug("Sending create agreement SOAP request..."); SLA slaAgreement = client.createAgreement(Configuration.slamURL, slat, agreementInDB.getNegotiationId()); logger.debug("SLA:"); logger.debug(slaAgreement); agreementInDB.setAccepted(true); agreementInDB.setSlaAgreement(slaAgreement.getUuid()); agreementDAO.update(agreementInDB); deployment.setStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZATION); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setApplicationName(applicationName); deploymentEvent.setDeploymentId(deployment.getId()); deploymentEvent.setDeploymentStatus(deployment.getStatus()); deploymentDAO.update(deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); return buildResponse(Status.ACCEPTED, XMLBuilder.getAgreementXML(agreementInDB, applicationName, Integer.parseInt(deploymentId))); } else { return buildResponse(Status.FORBIDDEN, "The selected agreement has already expired. It is necessary to restart the deployment process."); } } else { return buildResponse(Status.FORBIDDEN, "This agreement or a different one from the deployment has been previously accepted."); } } else { return buildResponse(Status.FORBIDDEN, "Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment."); } } else { return buildResponse(Status.BAD_REQUEST, "The only valid change of state it is <accepted>true</accepted>"); } }  }
AgreementRest extends AbstractRest { @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response acceptSlaAgreement(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("agreement_id") String agreementId, String payload) throws Exception { Agreement agreement = ModelConverter.xmlAgreementToObject(payload); PriceModellerClient.getInstance().initializeApplication(applicationName, Integer.parseInt(deploymentId), 1); if(agreement.isAccepted()) { Deployment deployment = deploymentDAO.getById(Integer.parseInt(deploymentId)); List<Agreement> agreements = deployment.getAgreements(); Agreement agreementInDB = deploymentContainsAgreement(agreements, Integer.parseInt(agreementId)); if(agreementInDB != null) { if(deploymentDosNotContainsAcceptedAgreement(agreements)) { Timestamp now = new Timestamp(System.currentTimeMillis()); if(TimeStampComparator.isInTheFuture(now, agreementInDB.getValidUntil())) { SLASOITemplateParser parser = new SLASOITemplateParser(); SLATemplate slat = parser.parseTemplate(agreementInDB.getSlaAgreement()); SlaTranslator slaTranslator = new SlaTranslatorImplNoOsgi(); client.setSlaTranslator(slaTranslator); logger.debug("Sending create agreement SOAP request..."); SLA slaAgreement = client.createAgreement(Configuration.slamURL, slat, agreementInDB.getNegotiationId()); logger.debug("SLA:"); logger.debug(slaAgreement); agreementInDB.setAccepted(true); agreementInDB.setSlaAgreement(slaAgreement.getUuid()); agreementDAO.update(agreementInDB); deployment.setStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZATION); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setApplicationName(applicationName); deploymentEvent.setDeploymentId(deployment.getId()); deploymentEvent.setDeploymentStatus(deployment.getStatus()); deploymentDAO.update(deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); return buildResponse(Status.ACCEPTED, XMLBuilder.getAgreementXML(agreementInDB, applicationName, Integer.parseInt(deploymentId))); } else { return buildResponse(Status.FORBIDDEN, "The selected agreement has already expired. It is necessary to restart the deployment process."); } } else { return buildResponse(Status.FORBIDDEN, "This agreement or a different one from the deployment has been previously accepted."); } } else { return buildResponse(Status.FORBIDDEN, "Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment."); } } else { return buildResponse(Status.BAD_REQUEST, "The only valid change of state it is <accepted>true</accepted>"); } }  @GET @Produces(MediaType.APPLICATION_XML) Response getAgreements(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId); @GET @Path("{agreement_id}") @Produces(MediaType.APPLICATION_XML) Response getAgreement(@PathParam("application_name") String applicationName, 
			                     @PathParam("deployment_id") String deploymentId,
			                     @PathParam("agreement_id") String agreementId); @GET @Path("{agreement_id}/sla") @Produces(MediaType.APPLICATION_XML) Response getSlaAgreement(@PathParam("application_name") String applicationName, 
			                     @PathParam("deployment_id") String deploymentId,
			                     @PathParam("agreement_id") String agreementId); @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) Response acceptSlaAgreement(@PathParam("application_name") String applicationName, 
									   @PathParam("deployment_id") String deploymentId,
									   @PathParam("agreement_id") String agreementId,
									   String payload); }
AgreementRest extends AbstractRest { @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) public Response acceptSlaAgreement(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId, @PathParam("agreement_id") String agreementId, String payload) throws Exception { Agreement agreement = ModelConverter.xmlAgreementToObject(payload); PriceModellerClient.getInstance().initializeApplication(applicationName, Integer.parseInt(deploymentId), 1); if(agreement.isAccepted()) { Deployment deployment = deploymentDAO.getById(Integer.parseInt(deploymentId)); List<Agreement> agreements = deployment.getAgreements(); Agreement agreementInDB = deploymentContainsAgreement(agreements, Integer.parseInt(agreementId)); if(agreementInDB != null) { if(deploymentDosNotContainsAcceptedAgreement(agreements)) { Timestamp now = new Timestamp(System.currentTimeMillis()); if(TimeStampComparator.isInTheFuture(now, agreementInDB.getValidUntil())) { SLASOITemplateParser parser = new SLASOITemplateParser(); SLATemplate slat = parser.parseTemplate(agreementInDB.getSlaAgreement()); SlaTranslator slaTranslator = new SlaTranslatorImplNoOsgi(); client.setSlaTranslator(slaTranslator); logger.debug("Sending create agreement SOAP request..."); SLA slaAgreement = client.createAgreement(Configuration.slamURL, slat, agreementInDB.getNegotiationId()); logger.debug("SLA:"); logger.debug(slaAgreement); agreementInDB.setAccepted(true); agreementInDB.setSlaAgreement(slaAgreement.getUuid()); agreementDAO.update(agreementInDB); deployment.setStatus(Dictionary.APPLICATION_STATUS_CONTEXTUALIZATION); DeploymentEvent deploymentEvent = new DeploymentEvent(); deploymentEvent.setApplicationName(applicationName); deploymentEvent.setDeploymentId(deployment.getId()); deploymentEvent.setDeploymentStatus(deployment.getStatus()); deploymentDAO.update(deployment); deploymentEventService.fireDeploymentEvent(deploymentEvent); return buildResponse(Status.ACCEPTED, XMLBuilder.getAgreementXML(agreementInDB, applicationName, Integer.parseInt(deploymentId))); } else { return buildResponse(Status.FORBIDDEN, "The selected agreement has already expired. It is necessary to restart the deployment process."); } } else { return buildResponse(Status.FORBIDDEN, "This agreement or a different one from the deployment has been previously accepted."); } } else { return buildResponse(Status.FORBIDDEN, "Or agreement it is not registered in the Application Manager DB or it does not belong to the requested deployment."); } } else { return buildResponse(Status.BAD_REQUEST, "The only valid change of state it is <accepted>true</accepted>"); } }  @GET @Produces(MediaType.APPLICATION_XML) Response getAgreements(@PathParam("application_name") String applicationName, @PathParam("deployment_id") String deploymentId); @GET @Path("{agreement_id}") @Produces(MediaType.APPLICATION_XML) Response getAgreement(@PathParam("application_name") String applicationName, 
			                     @PathParam("deployment_id") String deploymentId,
			                     @PathParam("agreement_id") String agreementId); @GET @Path("{agreement_id}/sla") @Produces(MediaType.APPLICATION_XML) Response getSlaAgreement(@PathParam("application_name") String applicationName, 
			                     @PathParam("deployment_id") String deploymentId,
			                     @PathParam("agreement_id") String agreementId); @PUT @Path("{agreement_id}") @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) Response acceptSlaAgreement(@PathParam("application_name") String applicationName, 
									   @PathParam("deployment_id") String deploymentId,
									   @PathParam("agreement_id") String agreementId,
									   String payload);  }