From 9d83361d133b418c0905fb92009e4bf6954591a7 Mon Sep 17 00:00:00 2001 From: Satish Date: Wed, 21 Dec 2016 17:49:01 +0530 Subject: [PATCH] Integration test cases for SMS Campaign --- .../common/organisation/CampaignsHelper.java | 198 +++++++++++++++++ .../common/organisation/CampaignsTest.java | 207 ++++++++++++++++++ .../common/report/ReportData.java | 75 +++++++ 3 files changed, 480 insertions(+) create mode 100644 fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsHelper.java create mode 100644 fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsTest.java create mode 100644 fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/report/ReportData.java diff --git a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsHelper.java b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsHelper.java new file mode 100644 index 000000000..c45077ad6 --- /dev/null +++ b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsHelper.java @@ -0,0 +1,198 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.integrationtests.common.organisation; + +import static com.jayway.restassured.RestAssured.given; +import static com.jayway.restassured.path.json.JsonPath.from; +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import org.apache.fineract.integrationtests.common.Utils; +import org.apache.fineract.integrationtests.common.report.ReportData; +import org.joda.time.LocalDateTime; +import org.springframework.util.Assert; + +import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +public class CampaignsHelper { + + private final RequestSpecification requestSpec; + private final ResponseSpecification responseSpec; + + private static final String SMS_CAMPAIGNS_URL = "/fineract-provider/api/v1/smscampaigns"; + public static final String DATE_FORMAT = "dd MMMM yyyy"; + public static final String DATE_TIME_FORMAT = "dd MMMM yyyy HH:mm:ss"; + + private static final String BUSINESS_RULE_OPTIONS = "businessRulesOptions"; + + public CampaignsHelper(final RequestSpecification requestSpec, final ResponseSpecification responseSpec) { + this.requestSpec = requestSpec; + this.responseSpec = responseSpec; + } + + public Integer createCampaign(String reportName, Integer triggerType) { + System.out.println("---------------------------------CREATING A CAMPAIGN---------------------------------------------"); + final String CREATE_SMS_CAMPAIGNS_URL = SMS_CAMPAIGNS_URL + "?" + Utils.TENANT_IDENTIFIER; + return Utils.performServerPost(requestSpec, responseSpec, CREATE_SMS_CAMPAIGNS_URL, getCreateCampaignJSON(reportName, triggerType), + "resourceId"); + } + + public void verifyCampaignCreatedOnServer(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer generatedCampaignId) { + System.out.println("------------------------------CHECK CAMPAIGN DETAILS------------------------------------\n"); + final String RETRIEVE_SMS_CAMPAIGNS_URL = SMS_CAMPAIGNS_URL + "/" + generatedCampaignId + "?" + Utils.TENANT_IDENTIFIER; + final Integer responseCampaignId = Utils.performServerGet(requestSpec, responseSpec, RETRIEVE_SMS_CAMPAIGNS_URL, "id"); + assertEquals("ERROR IN CREATING THE CAMPAIGN", generatedCampaignId, responseCampaignId); + } + + public Integer updateCampaign(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer generatedCampaignId, String reportName, Integer triggerType) { + System.out.println("------------------------------UPDATE CAMPAIGN DETAILS------------------------------------\n"); + final String UPDATE_SMS_CAMPAIGNS_URL = SMS_CAMPAIGNS_URL + "/" + generatedCampaignId + "?" + Utils.TENANT_IDENTIFIER; + return Utils.performServerPut(requestSpec, responseSpec, UPDATE_SMS_CAMPAIGNS_URL, getUpdateCampaignJSON(reportName, triggerType), + "resourceId"); + } + + public Integer deleteCampaign(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer generatedCampaignId) { + System.out.println("------------------------------DELETE CAMPAIGN DETAILS------------------------------------\n"); + final String DELETE_SMS_CAMPAIGNS_URL = SMS_CAMPAIGNS_URL + "/" + generatedCampaignId + "?" + Utils.TENANT_IDENTIFIER; + return Utils.performServerDelete(requestSpec, responseSpec, DELETE_SMS_CAMPAIGNS_URL, "resourceId"); + } + + public Integer performActionsOnCampaign(final RequestSpecification requestSpec, final ResponseSpecification responseSpec, + final Integer generatedCampaignId, String command) { + System.out.println("------------------------------PERFORM ACTION ON CAMPAIGN DETAILS------------------------------------\n"); + final String SMS_CAMPAIGNS_ACTION_URL = SMS_CAMPAIGNS_URL + "/" + generatedCampaignId + "?command=" + command + "&" + + Utils.TENANT_IDENTIFIER; + String actionDate = Utils.getLocalDateOfTenant().toString(DATE_FORMAT); + return Utils + .performServerPost(requestSpec, responseSpec, SMS_CAMPAIGNS_ACTION_URL, getJSONForCampaignAction(command, actionDate), "resourceId"); + } + + public Object performActionsOnCampaignWithFailure(final Integer generatedCampaignId, String command, String actionDate, String responseJsonAttribute) { + System.out.println("--------------------------PERFORM ACTION ON CAMPAIGN DETAILS WITH FAILURE-------------------------------\n"); + final String SMS_CAMPAIGNS_ACTION_URL = SMS_CAMPAIGNS_URL + "/" + generatedCampaignId + "?command=" + command + "&" + + Utils.TENANT_IDENTIFIER; + return Utils.performServerPost(this.requestSpec, this.responseSpec, SMS_CAMPAIGNS_ACTION_URL, getJSONForCampaignAction(command, actionDate), + responseJsonAttribute); + } + + public String getCreateCampaignJSON(String reportName, Integer triggerType) { + final HashMap map = new HashMap<>(); + final HashMap paramValueMap = new HashMap<>(); + Long reportId = getSelectedReportId(reportName); + map.put("providerId", 1); + map.put("triggerType", triggerType); + if (2 == triggerType) { + map.put("recurrenceStartDate", LocalDateTime.now().toString(DATE_TIME_FORMAT)); + map.put("frequency", 1); + map.put("interval", "1"); + } + map.put("campaignName", Utils.randomNameGenerator("Campaign_Name_", 5)); + map.put("campaignType", 1); + map.put("message", "Hi, this is from integtration tests runner"); + map.put("locale", "en"); + map.put("dateFormat", DATE_FORMAT); + map.put("dateTimeFormat", DATE_TIME_FORMAT); + map.put("runReportId", reportId); + paramValueMap.put("officeId", "1"); + paramValueMap.put("loanOfficerId", "1"); + paramValueMap.put("reportName", reportName); + map.put("paramValue", paramValueMap); + String json = new Gson().toJson(map); + System.out.println(json); + return json; + } + + public String getUpdateCampaignJSON(String reportName, Integer triggerType) { + final HashMap map = new HashMap<>(); + final HashMap paramValueMap = new HashMap<>(); + Long reportId = getSelectedReportId(reportName); + map.put("providerId", 1); + map.put("triggerType", triggerType); + if (2 == triggerType) { + map.put("recurrenceStartDate", LocalDateTime.now().toString(DATE_TIME_FORMAT)); + } + map.put("campaignName", Utils.randomNameGenerator("Campaign_Name_", 5)); + map.put("campaignType", 1); + map.put("message", "Hi, this is from integtration tests runner"); + map.put("locale", "en"); + map.put("dateFormat", DATE_FORMAT); + map.put("dateTimeFormat", DATE_TIME_FORMAT); + map.put("runReportId", reportId); + paramValueMap.put("officeId", "1"); + paramValueMap.put("loanOfficerId", "1"); + paramValueMap.put("reportName", reportName); + map.put("paramValue", paramValueMap); + String json = new Gson().toJson(map); + System.out.println(json); + return json; + } + + public String getJSONForCampaignAction(String command, String actionDate) { + final HashMap map = new HashMap<>(); + String dateString = ("close".equalsIgnoreCase(command)) ? "closureDate" : "activationDate"; + map.put(dateString, actionDate); + map.put("locale", "en"); + map.put("dateFormat", DATE_FORMAT); + String json = new Gson().toJson(map); + System.out.println(json); + return json; + } + + public ArrayList getReports() { + return getReports(BUSINESS_RULE_OPTIONS); + } + + private ArrayList getReports(String jsonAttributeToGetBack) { + System.out.println("--------------------------------- GET REPORTS OPTIONS -------------------------------"); + Assert.notNull(jsonAttributeToGetBack); + final String templateUrl = SMS_CAMPAIGNS_URL + "/template?" + Utils.TENANT_IDENTIFIER; + final String json = given().spec(requestSpec).expect().spec(responseSpec).log().ifError().when().get(templateUrl).andReturn() + .asString(); + Assert.notNull(json); + ArrayList reportsList = new ArrayList<>(); + String reportsString = new Gson().toJson(from(json).get(jsonAttributeToGetBack)); + Assert.notNull(reportsString); + final Gson gson = new Gson(); + final Type typeOfHashMap = new TypeToken>() {}.getType(); + reportsList = gson.fromJson(reportsString, typeOfHashMap); + return reportsList; + } + + private Long getSelectedReportId(final String reportName) { + ArrayList reports = getReports(); + + if (reports != null && !reports.isEmpty()) { + for (ReportData reportData : reports) { + if (reportName.equals(reportData.getReportName())) { return reportData.getReportId(); } + } + } + Assert.notNull(null); + return null; + } +} diff --git a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsTest.java b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsTest.java new file mode 100644 index 000000000..b8591caa2 --- /dev/null +++ b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/organisation/CampaignsTest.java @@ -0,0 +1,207 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.integrationtests.common.organisation; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.HashMap; + +import org.apache.fineract.integrationtests.common.CommonConstants; +import org.apache.fineract.integrationtests.common.Utils; +import org.junit.Before; +import org.junit.Test; + +import com.jayway.restassured.builder.RequestSpecBuilder; +import com.jayway.restassured.builder.ResponseSpecBuilder; +import com.jayway.restassured.http.ContentType; +import com.jayway.restassured.specification.RequestSpecification; +import com.jayway.restassured.specification.ResponseSpecification; + +public class CampaignsTest { + + private RequestSpecification requestSpec; + private ResponseSpecification responseSpec; + private CampaignsHelper campaignsHelper; + + private static final String NON_TRIGGERED_REPORT_NAME = "Prospective Clients"; + private static final String TRIGGERED_REPORT_NAME = "Client Activated"; + + private static final Integer DIRECT_TRIGGER_TYPE = 1; + private static final Integer SCHEDULED_TRIGGER_TYPE = 2; + private static final Integer TRIGGERED_TRIGGER_TYPE = 3; + + private static final String ACTIVATE_COMMAND = "activate"; + private static final String CLOSE_COMMAND = "close"; + private static final String REACTIVATE_COMMAND = "reactivate"; + + public static final String DATE_FORMAT = "dd MMMM yyyy"; + + @Before + public void setup() { + Utils.initializeRESTAssured(); + this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); + this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); + this.campaignsHelper = new CampaignsHelper(this.requestSpec, this.responseSpec); + } + + @Test + public void testSupportedActionsForCampaignWithTriggerTypeAsDirect() { + // creating new campaign + Integer campaignId = this.campaignsHelper.createCampaign(NON_TRIGGERED_REPORT_NAME, DIRECT_TRIGGER_TYPE); + this.campaignsHelper.verifyCampaignCreatedOnServer(this.requestSpec, this.responseSpec, campaignId); + + // updating campaign + Integer updatedCampaignId = this.campaignsHelper.updateCampaign(this.requestSpec, this.responseSpec, campaignId, + NON_TRIGGERED_REPORT_NAME, DIRECT_TRIGGER_TYPE); + assertEquals(campaignId, updatedCampaignId); + + // activating campaign + Integer activatedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + ACTIVATE_COMMAND); + assertEquals(activatedCampaignId, campaignId); + + // closing campaign + Integer closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // reactivating campaign + Integer reactivateCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + REACTIVATE_COMMAND); + assertEquals(reactivateCampaignId, campaignId); + + // closing campaign again for deletion + closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // deleting campaign + Integer deletedCampaignId = this.campaignsHelper.deleteCampaign(this.requestSpec, this.responseSpec, campaignId); + assertEquals(deletedCampaignId, campaignId); + } + + @Test + public void testSupportedActionsForCampaignWithTriggerTypeAsScheduled() { + // creating new campaign + Integer campaignId = this.campaignsHelper.createCampaign(NON_TRIGGERED_REPORT_NAME, SCHEDULED_TRIGGER_TYPE); + this.campaignsHelper.verifyCampaignCreatedOnServer(this.requestSpec, this.responseSpec, campaignId); + + // updating campaign + Integer updatedCampaignId = this.campaignsHelper.updateCampaign(this.requestSpec, this.responseSpec, campaignId, + NON_TRIGGERED_REPORT_NAME, SCHEDULED_TRIGGER_TYPE); + assertEquals(campaignId, updatedCampaignId); + + // activating campaign + Integer activatedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + ACTIVATE_COMMAND); + assertEquals(activatedCampaignId, campaignId); + + // closing campaign + Integer closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // reactivating campaign + Integer reactivateCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + REACTIVATE_COMMAND); + assertEquals(reactivateCampaignId, campaignId); + + // closing campaign again for deletion + closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // deleting campaign + Integer deletedCampaignId = this.campaignsHelper.deleteCampaign(this.requestSpec, this.responseSpec, campaignId); + assertEquals(deletedCampaignId, campaignId); + } + + @Test + public void testSupportedActionsForCampaignWithTriggerTypeAsTriggered() { + // creating new campaign + Integer campaignId = this.campaignsHelper.createCampaign(TRIGGERED_REPORT_NAME, TRIGGERED_TRIGGER_TYPE); + this.campaignsHelper.verifyCampaignCreatedOnServer(this.requestSpec, this.responseSpec, campaignId); + + // updating campaign + Integer updatedCampaignId = this.campaignsHelper.updateCampaign(this.requestSpec, this.responseSpec, campaignId, + TRIGGERED_REPORT_NAME, TRIGGERED_TRIGGER_TYPE); + assertEquals(campaignId, updatedCampaignId); + + // activating campaign + Integer activatedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + ACTIVATE_COMMAND); + assertEquals(activatedCampaignId, campaignId); + + // closing campaign + Integer closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // reactivating campaign + Integer reactivateCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + REACTIVATE_COMMAND); + assertEquals(reactivateCampaignId, campaignId); + + // closing campaign again for deletion + closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // deleting campaign + Integer deletedCampaignId = this.campaignsHelper.deleteCampaign(this.requestSpec, this.responseSpec, campaignId); + assertEquals(deletedCampaignId, campaignId); + } + + @SuppressWarnings("unchecked") + @Test + public void testSupportedActionsForCampaignWithError() { + final ResponseSpecification responseSpecWithError = new ResponseSpecBuilder().expectStatusCode(400).build(); + CampaignsHelper campaignsHelperWithError = new CampaignsHelper(this.requestSpec, responseSpecWithError); + // creating new campaign + Integer campaignId = this.campaignsHelper.createCampaign(NON_TRIGGERED_REPORT_NAME, DIRECT_TRIGGER_TYPE); + this.campaignsHelper.verifyCampaignCreatedOnServer(this.requestSpec, this.responseSpec, campaignId); + + // activating campaign with failure + ArrayList campaignDateValidationData = (ArrayList) campaignsHelperWithError.performActionsOnCampaignWithFailure( + campaignId, ACTIVATE_COMMAND, Utils.getLocalDateOfTenant().plusDays(1).toString(DATE_FORMAT), + CommonConstants.RESPONSE_ERROR); + assertEquals("error.msg.campaign.activationDate.in.the.future", + ((HashMap) campaignDateValidationData.get(0)).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE)); + + // activating campaign + Integer activatedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + ACTIVATE_COMMAND); + assertEquals(activatedCampaignId, campaignId); + + // activating campaign with failure + ArrayList campaignErrorData = (ArrayList) campaignsHelperWithError.performActionsOnCampaignWithFailure( + activatedCampaignId, ACTIVATE_COMMAND, Utils.getLocalDateOfTenant().toString(DATE_FORMAT), CommonConstants.RESPONSE_ERROR); + assertEquals("error.msg.campaign.already.active", + ((HashMap) campaignErrorData.get(0)).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE)); + + // closing campaign again for deletion + Integer closedCampaignId = this.campaignsHelper.performActionsOnCampaign(this.requestSpec, this.responseSpec, campaignId, + CLOSE_COMMAND); + assertEquals(closedCampaignId, campaignId); + + // deleting campaign + Integer deletedCampaignId = this.campaignsHelper.deleteCampaign(this.requestSpec, this.responseSpec, campaignId); + assertEquals(deletedCampaignId, campaignId); + + } +} diff --git a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/report/ReportData.java b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/report/ReportData.java new file mode 100644 index 000000000..d64e39388 --- /dev/null +++ b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/report/ReportData.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.integrationtests.common.report; + +import java.util.Map; + +public class ReportData { + + private final Long reportId; + + private final String reportName; + + private final String reportType; + + private final String reportSubType; + + private final String reportDescription; + + private final Map reportParamName; + + public ReportData(final Long reportId, final String reportName, final String reportType, final String reportSubType, + final Map reportParamName, final String reportDescription) { + this.reportId = reportId; + this.reportName = reportName; + this.reportType = reportType; + this.reportSubType = reportSubType; + this.reportParamName = reportParamName; + this.reportDescription = reportDescription; + } + + public static ReportData instance(final Long reportId, final String reportName, final String reportType, + final String reportSubType, final Map reportParamName, final String reportDescription) { + return new ReportData(reportId, reportName, reportType, reportSubType, reportParamName, reportDescription); + } + + public Map getReportParamName() { + return reportParamName; + } + + public String getReportType() { + return reportType; + } + + public String getReportSubType() { + return this.reportSubType; + } + + public String getReportName() { + return reportName; + } + + public Long getReportId() { + return reportId; + } + + public String getReportDescription() { + return reportDescription; + } +}