-
Notifications
You must be signed in to change notification settings - Fork 2.3k
FINERACT-248: Prevent duplicate SMS campaign names #5439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nickus
wants to merge
1
commit into
apache:develop
Choose a base branch
from
nickus:FINERACT-248-prevent-duplicate-sms-campaign-names
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+204
−14
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...ineract/infrastructure/campaigns/sms/exception/SmsCampaignNameAlreadyExistsException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * 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.infrastructure.campaigns.sms.exception; | ||
|
|
||
| import org.apache.fineract.infrastructure.core.exception.AbstractPlatformDomainRuleException; | ||
|
|
||
| public class SmsCampaignNameAlreadyExistsException extends AbstractPlatformDomainRuleException { | ||
|
|
||
| public SmsCampaignNameAlreadyExistsException(final String name) { | ||
| super("error.msg.sms.campaign.duplicate.name", "An SMS campaign with name '" + name + "' already exists", name); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
...-tests/src/test/java/org/apache/fineract/integrationtests/SmsCampaignIntegrationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| /** | ||
| * 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; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
|
|
||
| import io.restassured.builder.RequestSpecBuilder; | ||
| import io.restassured.builder.ResponseSpecBuilder; | ||
| import io.restassured.http.ContentType; | ||
| import io.restassured.specification.RequestSpecification; | ||
| import io.restassured.specification.ResponseSpecification; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import org.apache.fineract.integrationtests.common.CommonConstants; | ||
| import org.apache.fineract.integrationtests.common.Utils; | ||
| import org.apache.fineract.integrationtests.common.organisation.CampaignsHelper; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockserver.integration.ClientAndServer; | ||
| import org.mockserver.junit.jupiter.MockServerExtension; | ||
| import org.mockserver.junit.jupiter.MockServerSettings; | ||
| import org.mockserver.model.HttpRequest; | ||
| import org.mockserver.model.HttpResponse; | ||
| import org.mockserver.model.MediaType; | ||
|
|
||
| /** | ||
| * Integration tests for SMS Campaign duplicate name validation. | ||
| */ | ||
| @ExtendWith(MockServerExtension.class) | ||
| @MockServerSettings(ports = { 9191 }) | ||
| public class SmsCampaignIntegrationTest { | ||
|
|
||
| private RequestSpecification requestSpec; | ||
| private ResponseSpecification responseSpec; | ||
| private ResponseSpecification errorResponseSpec; | ||
| private CampaignsHelper campaignsHelper; | ||
| private final ClientAndServer client; | ||
|
|
||
| public SmsCampaignIntegrationTest(ClientAndServer client) { | ||
| this.client = client; | ||
| this.client.when(HttpRequest.request().withMethod("GET").withPath("/smsbridges")) | ||
| .respond(HttpResponse.response().withContentType(MediaType.APPLICATION_JSON).withBody( | ||
| "[{\"id\":1,\"tenantId\":1,\"phoneNo\":\"+1234567890\",\"providerName\":\"Dummy SMS Provider - Testing\",\"providerDescription\":\"Dummy, just for testing\"}]")); | ||
| } | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| Utils.initializeRESTAssured(); | ||
| this.requestSpec = new RequestSpecBuilder().setContentType(ContentType.JSON).build(); | ||
| this.requestSpec.header("Authorization", "Basic " + Utils.loginIntoServerAndGetBase64EncodedAuthenticationKey()); | ||
| this.requestSpec.header("Fineract-Platform-TenantId", "default"); | ||
| this.responseSpec = new ResponseSpecBuilder().expectStatusCode(200).build(); | ||
| this.errorResponseSpec = new ResponseSpecBuilder().expectStatusCode(403).build(); | ||
| this.campaignsHelper = new CampaignsHelper(this.requestSpec, this.responseSpec); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateCampaignWithDuplicateNameShouldFail() { | ||
| String reportName = "Prospective Clients"; | ||
| int triggerType = 1; | ||
| String campaignName = "Duplicate_Test_Campaign_" + System.currentTimeMillis(); | ||
|
|
||
| // Create first campaign with specific name | ||
| Integer firstCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName); | ||
| assertNotNull(firstCampaignId, "First campaign should be created successfully"); | ||
| campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, firstCampaignId); | ||
|
|
||
| // Attempt to create second campaign with the same name - should fail | ||
| List<HashMap> errors = campaignsHelper.createCampaignWithNameExpectingError(errorResponseSpec, reportName, triggerType, | ||
| campaignName); | ||
|
|
||
| assertNotNull(errors, "Error response should not be null"); | ||
| assertEquals(1, errors.size(), "Should have exactly one error"); | ||
| assertEquals("error.msg.sms.campaign.duplicate.name", errors.get(0).get(CommonConstants.RESPONSE_ERROR_MESSAGE_CODE), | ||
| "Error code should indicate duplicate campaign name"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCreateCampaignWithUniqueNameShouldSucceed() { | ||
| String reportName = "Prospective Clients"; | ||
| int triggerType = 1; | ||
| String campaignName1 = "Unique_Campaign_1_" + System.currentTimeMillis(); | ||
| String campaignName2 = "Unique_Campaign_2_" + System.currentTimeMillis(); | ||
|
|
||
| // Create first campaign | ||
| Integer firstCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName1); | ||
| assertNotNull(firstCampaignId, "First campaign should be created successfully"); | ||
|
|
||
| // Create second campaign with different name - should succeed | ||
| Integer secondCampaignId = campaignsHelper.createCampaignWithName(reportName, triggerType, campaignName2); | ||
| assertNotNull(secondCampaignId, "Second campaign with different name should be created successfully"); | ||
|
|
||
| // Verify both campaigns exist | ||
| campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, firstCampaignId); | ||
| campaignsHelper.verifyCampaignCreatedOnServer(requestSpec, responseSpec, secondCampaignId); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the default isolation level we use is read_committed, meaning only committed things will be revealed by this check. It not guarantees anything.
2 transactions concurrently writing the same campaign name is possible.
If you wanna prevent it, ensure having a unique key on the DB level. This is good for a best-effort check but it's not bullet-proof,.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, there is already a unique key on the DB level. You might see further I catch DataIntegrityViolationException and throw the same exception.
see realCause.getMessage().contains("campaign_name_UNIQUE")