|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.fineract.integrationtests; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | + |
| 24 | +import io.restassured.builder.RequestSpecBuilder; |
| 25 | +import io.restassured.builder.ResponseSpecBuilder; |
| 26 | +import io.restassured.http.ContentType; |
| 27 | +import io.restassured.specification.RequestSpecification; |
| 28 | +import io.restassured.specification.ResponseSpecification; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.List; |
| 31 | +import org.apache.fineract.integrationtests.common.CommonConstants; |
| 32 | +import org.apache.fineract.integrationtests.common.Utils; |
| 33 | +import org.apache.fineract.integrationtests.common.organisation.CampaignsHelper; |
| 34 | +import org.junit.jupiter.api.BeforeEach; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 37 | +import org.mockserver.integration.ClientAndServer; |
| 38 | +import org.mockserver.junit.jupiter.MockServerExtension; |
| 39 | +import org.mockserver.junit.jupiter.MockServerSettings; |
| 40 | +import org.mockserver.model.HttpRequest; |
| 41 | +import org.mockserver.model.HttpResponse; |
| 42 | +import org.mockserver.model.MediaType; |
| 43 | + |
| 44 | +/** |
| 45 | + * Integration tests for SMS Campaign duplicate name validation. |
| 46 | + */ |
| 47 | +@ExtendWith(MockServerExtension.class) |
| 48 | +@MockServerSettings(ports = { 9191 }) |
| 49 | +public class SmsCampaignIntegrationTest { |
| 50 | + |
| 51 | + private RequestSpecification requestSpec; |
| 52 | + private ResponseSpecification responseSpec; |
| 53 | + private ResponseSpecification errorResponseSpec; |
| 54 | + private CampaignsHelper campaignsHelper; |
| 55 | + private final ClientAndServer client; |
| 56 | + |
| 57 | + public SmsCampaignIntegrationTest(ClientAndServer client) { |
| 58 | + this.client = client; |
| 59 | + this.client.when(HttpRequest.request().withMethod("GET").withPath("/smsbridges")) |
| 60 | + .respond(HttpResponse.response().withContentType(MediaType.APPLICATION_JSON).withBody( |
| 61 | + "[{\"id\":1,\"tenantId\":1,\"phoneNo\":\"+1234567890\",\"providerName\":\"Dummy SMS Provider - Testing\",\"providerDescription\":\"Dummy, just for testing\"}]")); |
| 62 | + } |
| 63 | + |
| 64 | + @BeforeEach |
| 65 | + public void setup() { |
| 66 | + Utils.initializeRESTAssured(); |
| 67 | + this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); |
| 68 | + this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); |
| 69 | + this.requestSpec.header("Fineract-Platform-TenantId", "default"); |
| 70 | + this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); |
| 71 | + this.errorResponseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); |
| 72 | + this.campaignsHelper = new CampaignsHelper(this.requestSpec, this.responseSpec); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testCreateCampaignWithDuplicateNameShouldFail() { |
| 77 | + String reportName = "Prospective Clients"; |
| 78 | + int triggerType = 1; |
| 79 | + String campaignName = "Duplicate_Test_Campaign_" + System.currentTimeMillis(); |
| 80 | + |
| 81 | + // Create first campaign with specific name |
| 82 | + Integer firstCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName); |
| 83 | + assertNotNull(firstCampaignId, "First campaign should be created successfully"); |
| 84 | + campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, firstCampaignId); |
| 85 | + |
| 86 | + // Attempt to create second campaign with the same name - should fail |
| 87 | + List<HashMap> errors = campaignsHelper.createCampaignWithNameExpectingError(errorResponseSpec, reportName, triggerType, |
| 88 | + campaignName); |
| 89 | + |
| 90 | + assertNotNull(errors, "Error response should not be null"); |
| 91 | + assertEquals(1, errors.size(), "Should have exactly one error"); |
| 92 | + assertEquals("error.msg.sms.campaign.duplicate.name", errors.get(0).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE), |
| 93 | + "Error code should indicate duplicate campaign name"); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testCreateCampaignWithUniqueNameShouldSucceed() { |
| 98 | + String reportName = "Prospective Clients"; |
| 99 | + int triggerType = 1; |
| 100 | + String campaignName1 = "Unique_Campaign_1_" + System.currentTimeMillis(); |
| 101 | + String campaignName2 = "Unique_Campaign_2_" + System.currentTimeMillis(); |
| 102 | + |
| 103 | + // Create first campaign |
| 104 | + Integer firstCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName1); |
| 105 | + assertNotNull(firstCampaignId, "First campaign should be created successfully"); |
| 106 | + |
| 107 | + // Create second campaign with different name - should succeed |
| 108 | + Integer secondCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName2); |
| 109 | + assertNotNull(secondCampaignId, "Second campaign with different name should be created successfully"); |
| 110 | + |
| 111 | + // Verify both campaigns exist |
| 112 | + campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, firstCampaignId); |
| 113 | + campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, secondCampaignId); |
| 114 | + } |
| 115 | +} |
0 commit comments