From 93529c8b692ee6335b135d5e6c0369842481eab2 Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Wed, 22 Jun 2016 11:10:46 +0530 Subject: [PATCH 1/9] NMS-330 --- .../nms/api/web/OpsController.java | 22 ++++- .../DeactivateSubscriptionContract.java | 20 ++++ .../kilkari/domain/DeactivationReason.java | 3 +- .../kilkari/service/SubscriptionService.java | 2 + .../service/impl/SubscriptionServiceImpl.java | 23 +++++ .../testing/it/api/OpsControllerBundleIT.java | 93 ++++++++++++++++--- 6 files changed, 146 insertions(+), 17 deletions(-) create mode 100644 api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java diff --git a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java index 716df6578..a6aab877b 100644 --- a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java +++ b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java @@ -3,6 +3,7 @@ import org.motechproject.event.MotechEvent; import org.motechproject.event.listener.EventRelay; import org.motechproject.nms.api.web.contract.AddFlwRequest; +import org.motechproject.nms.api.web.contract.kilkari.DeactivateSubscriptionContract; import org.motechproject.nms.flw.service.FrontLineWorkerService; import org.motechproject.nms.flw.utils.FlwConstants; import org.motechproject.nms.api.web.contract.mobileAcademy.GetBookmarkResponse; @@ -62,6 +63,8 @@ public class OpsController extends BaseController { @Autowired private FrontLineWorkerService frontLineWorkerService; + private String contactNumber = "contactNumber"; + /** * Provided for OPS as a crutch to be able to empty all MDS cache directly after modifying the database by hand */ @@ -119,8 +122,8 @@ public void createUpdateFlw(@RequestBody AddFlwRequest addFlwRequest) { addFlwRequest.getDistrictId())); StringBuilder failureReasons = new StringBuilder(); - validateField10Digits(failureReasons, "contactNumber", addFlwRequest.getContactNumber()); - validateFieldPositiveLong(failureReasons, "contactNumber", addFlwRequest.getContactNumber()); + validateField10Digits(failureReasons, contactNumber, addFlwRequest.getContactNumber()); + validateFieldPositiveLong(failureReasons, contactNumber, addFlwRequest.getContactNumber()); validateFieldPresent(failureReasons, "mctsFlwId", addFlwRequest.getMctsFlwId()); validateFieldPresent(failureReasons, "stateId", addFlwRequest.getStateId()); validateFieldPresent(failureReasons, "districtId", addFlwRequest.getDistrictId()); @@ -175,5 +178,20 @@ public GetBookmarkResponse getBookmarkWithScore(@RequestParam(required = false) log("RESPONSE: /ops/getbookmark", String.format("bookmark=%s", ret.toString())); return ret; } + + @RequestMapping(value = "/releaseNumber", + method = RequestMethod.POST, + headers = { "Content-type=application/json" }) + @ResponseStatus(HttpStatus.OK) + @Transactional + public void releaseNumber(@RequestBody DeactivateSubscriptionContract deactivateSubscriptionContract) { + log("REQUEST: /ops/releaseNumber", String.format( + "callingNumber=%s", + LogHelper.obscure(deactivateSubscriptionContract.getContactNumber()))); + StringBuilder failureReasons = new StringBuilder(); + validateField10Digits(failureReasons, contactNumber, deactivateSubscriptionContract.getContactNumber()); + validateFieldPositiveLong(failureReasons, contactNumber, deactivateSubscriptionContract.getContactNumber()); + subscriptionService.deactivateSubscriptionForSpecificMsisdn(deactivateSubscriptionContract.getContactNumber()); + } } diff --git a/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java b/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java new file mode 100644 index 000000000..dacec7eb0 --- /dev/null +++ b/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java @@ -0,0 +1,20 @@ +package org.motechproject.nms.api.web.contract.kilkari; + +/** + * Created by sripooja on 20/6/16. + */ +public class DeactivateSubscriptionContract { + + private Long contactNumber; + + public DeactivateSubscriptionContract() { + } + + public Long getContactNumber() { + return contactNumber; + } + + public void setContactNumber(Long contactNumber) { + this.contactNumber = contactNumber; + } +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java index 86ea69d08..88a4021a7 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java @@ -12,5 +12,6 @@ public enum DeactivationReason { MATERNAL_DEATH, INVALID_NUMBER, // See https://github.com/motech-implementations/mim/issues/169 DO_NOT_DISTURB, - MCTS_UPDATE + MCTS_UPDATE, + MOHFW_REQUEST } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java index d6f45a17e..bdd2f4323 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java @@ -198,4 +198,6 @@ Subscription createSubscription(long callingNumber, Language language, Circle ci * */ List retrieveAll(); + + void deactivateSubscriptionForSpecificMsisdn(long callingNumber); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 80abcc1cd..88ee88b7a 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -29,6 +29,7 @@ import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; import org.motechproject.nms.kilkari.service.CsrVerifierService; +import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; import org.motechproject.nms.kilkari.utils.PhoneNumberHelper; @@ -69,6 +70,9 @@ public class SubscriptionServiceImpl implements SubscriptionService { private EventRelay eventRelay; private boolean allowMctsSubscriptions; + @Autowired + private SubscriberService subscriberService; + @Autowired public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade settingsFacade, // NO CHECKSTYLE More than 7 parameters SubscriberDataService subscriberDataService, @@ -690,4 +694,23 @@ public void toggleActiveSubscriptionCapping(MotechEvent event) { public List retrieveAll() { return subscriptionDataService.retrieveAll(); } + + + @Override + public void deactivateSubscriptionForSpecificMsisdn(long callingNumber) { + LOGGER.info("Recieved Release Number {} for Deactivation.", callingNumber); + Subscriber subscriberByMsisdn = subscriberService.getSubscriber(callingNumber); + if(subscriberByMsisdn == null) { + LOGGER.info("Subscriber for msisdn {} is not found." , callingNumber); + return; + } + LOGGER.info("Found Subscriber for msisdn {} .", callingNumber); + for(Subscription subscription : subscriberByMsisdn.getAllSubscriptions()) { + LOGGER.info("Deactivating Subscrition with start date {} for msisdn .", subscription.getStartDate()); + deactivateSubscription(subscription, DeactivationReason.MOHFW_REQUEST); + LOGGER.info("Deactivated Subscrition with start date {} for msisdn .", subscription.getStartDate()); + } + + } + } diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index 2603f6a94..3027d53e8 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -6,11 +6,19 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.motechproject.nms.api.web.contract.AddFlwRequest; +import org.motechproject.nms.api.web.contract.kilkari.DeactivateSubscriptionContract; import org.motechproject.nms.flw.domain.FlwError; import org.motechproject.nms.flw.domain.FlwErrorReason; import org.motechproject.nms.flw.domain.FrontLineWorker; import org.motechproject.nms.flw.repository.FlwErrorDataService; import org.motechproject.nms.flw.repository.FrontLineWorkerDataService; +import org.motechproject.nms.kilkari.domain.Subscriber; +import org.motechproject.nms.kilkari.domain.Subscription; +import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; +import org.motechproject.nms.kilkari.repository.*; +import org.motechproject.nms.kilkari.service.SubscriberService; +import org.motechproject.nms.kilkari.service.SubscriptionService; +import org.motechproject.nms.props.repository.DeployedServiceDataService; import org.motechproject.nms.region.domain.District; import org.motechproject.nms.region.domain.HealthBlock; import org.motechproject.nms.region.domain.HealthFacility; @@ -20,17 +28,13 @@ import org.motechproject.nms.region.domain.State; import org.motechproject.nms.region.domain.Taluka; import org.motechproject.nms.region.domain.Village; -import org.motechproject.nms.region.repository.DistrictDataService; -import org.motechproject.nms.region.repository.HealthBlockDataService; -import org.motechproject.nms.region.repository.HealthFacilityDataService; -import org.motechproject.nms.region.repository.HealthFacilityTypeDataService; -import org.motechproject.nms.region.repository.HealthSubFacilityDataService; -import org.motechproject.nms.region.repository.LanguageDataService; -import org.motechproject.nms.region.repository.StateDataService; -import org.motechproject.nms.region.repository.TalukaDataService; -import org.motechproject.nms.region.repository.VillageDataService; +import org.motechproject.nms.region.repository.*; +import org.motechproject.nms.region.service.DistrictService; import org.motechproject.nms.region.service.HealthBlockService; +import org.motechproject.nms.region.service.LanguageService; import org.motechproject.nms.testing.it.api.utils.RequestBuilder; +import org.motechproject.nms.testing.it.utils.RegionHelper; +import org.motechproject.nms.testing.it.utils.SubscriptionHelper; import org.motechproject.nms.testing.service.TestingService; import org.motechproject.testing.osgi.BasePaxIT; import org.motechproject.testing.osgi.container.MotechNativeTestContainerFactory; @@ -65,6 +69,8 @@ public class OpsControllerBundleIT extends BasePaxIT { private String addFlwEndpoint = String.format("http://localhost:%d/api/ops/createUpdateFlw", TestContext.getJettyPort()); + private String releaseNumber = String.format("http://localhost:%d/api/ops/releaseNumber", + TestContext.getJettyPort()); State state; District district; Taluka taluka; @@ -82,21 +88,44 @@ public class OpsControllerBundleIT extends BasePaxIT { TestingService testingService; @Inject - LanguageDataService languageDataService; + FrontLineWorkerDataService frontLineWorkerDataService; @Inject - StateDataService stateDataService; + FlwErrorDataService flwErrorDataService; - @Inject - FrontLineWorkerDataService frontLineWorkerDataService; @Inject - FlwErrorDataService flwErrorDataService; + SubscriberService subscriberService; + @Inject + SubscriptionService subscriptionService; + @Inject + SubscriberDataService subscriberDataService; + @Inject + SubscriptionPackDataService subscriptionPackDataService; + @Inject + SubscriptionDataService subscriptionDataService; + @Inject + LanguageDataService languageDataService; + @Inject + LanguageService languageService; + @Inject + CircleDataService circleDataService; + @Inject + StateDataService stateDataService; + @Inject + DistrictDataService districtDataService; + @Inject + DistrictService districtService; + + + private RegionHelper rh; + private SubscriptionHelper sh; @Before public void setupTestData() { testingService.clearDatabase(); initializeLocationData(); + createSubscriberHelper(); } // Test flw update with empty flw request @@ -368,4 +397,40 @@ private void initializeLocationData() { transactionManager.commit(status); } + // create subscriber with many subscriptions helper + private void createSubscriberHelper() { + + TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); + // create subscription for a msisdn + rh = new RegionHelper(languageDataService, languageService, circleDataService, stateDataService, + districtDataService, districtService); + + sh = new SubscriptionHelper(subscriptionService, subscriberDataService, subscriptionPackDataService, + languageDataService, languageService, circleDataService, stateDataService, districtDataService, + districtService); + + Subscriber subscriber = subscriberDataService.create(new Subscriber(1000000000L)); + + subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.hindiLanguage(), + sh.childPack(), SubscriptionOrigin.IVR); + subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.hindiLanguage(), + sh.pregnancyPack(), SubscriptionOrigin.IVR); + + transactionManager.commit(status); + } + + + // Test deactivation of specific msisdn + @Test + public void testDeactivateSpecificMsisdn() throws IOException, InterruptedException { + + // Http Post request to deactivate subscriber + DeactivateSubscriptionContract deactivateSubscriptionContract = new DeactivateSubscriptionContract(); + deactivateSubscriptionContract.setContactNumber(1000000000L); + HttpPost httpRequest = RequestBuilder.createPostRequest(releaseNumber, deactivateSubscriptionContract); + + assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + + } + } From de83630e0c6edce04fa8a8403281d701f28783fd Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Wed, 22 Jun 2016 18:31:49 +0530 Subject: [PATCH 2/9] Changed Http POST to DELETE method for deactivation of subscribers --- .../nms/api/web/OpsController.java | 28 ++++++++++------- .../DeactivateSubscriptionContract.java | 20 ------------ .../kilkari/service/SubscriberService.java | 7 +++++ .../kilkari/service/SubscriptionService.java | 1 - .../service/impl/SubscriberServiceImpl.java | 21 +++++++++++++ .../service/impl/SubscriptionServiceImpl.java | 22 ------------- .../testing/it/api/OpsControllerBundleIT.java | 31 ++++++++++++------- .../testing/it/api/utils/RequestBuilder.java | 22 +++++++++++++ 8 files changed, 87 insertions(+), 65 deletions(-) delete mode 100644 api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java diff --git a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java index a6aab877b..22d2d2392 100644 --- a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java +++ b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java @@ -3,13 +3,13 @@ import org.motechproject.event.MotechEvent; import org.motechproject.event.listener.EventRelay; import org.motechproject.nms.api.web.contract.AddFlwRequest; -import org.motechproject.nms.api.web.contract.kilkari.DeactivateSubscriptionContract; import org.motechproject.nms.flw.service.FrontLineWorkerService; import org.motechproject.nms.flw.utils.FlwConstants; import org.motechproject.nms.api.web.contract.mobileAcademy.GetBookmarkResponse; import org.motechproject.nms.api.web.converter.MobileAcademyConverter; import org.motechproject.nms.imi.service.CdrFileService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; +import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; import org.motechproject.nms.mcts.service.MctsWsImportService; @@ -45,6 +45,9 @@ public class OpsController extends BaseController { @Autowired private SubscriptionDataService subscriptionDataService; + @Autowired + private SubscriberService subscriberService; + @Autowired private SubscriptionService subscriptionService; @@ -63,7 +66,7 @@ public class OpsController extends BaseController { @Autowired private FrontLineWorkerService frontLineWorkerService; - private String contactNumber = "contactNumber"; + private final String contactNumber = "contactNumber"; /** * Provided for OPS as a crutch to be able to empty all MDS cache directly after modifying the database by hand @@ -179,19 +182,22 @@ public GetBookmarkResponse getBookmarkWithScore(@RequestParam(required = false) return ret; } - @RequestMapping(value = "/releaseNumber", - method = RequestMethod.POST, - headers = { "Content-type=application/json" }) + @RequestMapping(value = "/deactivationRequest", + method = RequestMethod.DELETE) @ResponseStatus(HttpStatus.OK) @Transactional - public void releaseNumber(@RequestBody DeactivateSubscriptionContract deactivateSubscriptionContract) { - log("REQUEST: /ops/releaseNumber", String.format( + public void deactivationRequest(@RequestParam(value = "msisdn") String msisdn) { + long phoneNumber = Long.parseLong(msisdn); + log("REQUEST: /ops/deactivationRequest", String.format( "callingNumber=%s", - LogHelper.obscure(deactivateSubscriptionContract.getContactNumber()))); + LogHelper.obscure(phoneNumber))); StringBuilder failureReasons = new StringBuilder(); - validateField10Digits(failureReasons, contactNumber, deactivateSubscriptionContract.getContactNumber()); - validateFieldPositiveLong(failureReasons, contactNumber, deactivateSubscriptionContract.getContactNumber()); - subscriptionService.deactivateSubscriptionForSpecificMsisdn(deactivateSubscriptionContract.getContactNumber()); + validateField10Digits(failureReasons, contactNumber, phoneNumber); + validateFieldPositiveLong(failureReasons, contactNumber, phoneNumber); + if (failureReasons.length() > 0) { + throw new IllegalArgumentException(failureReasons.toString()); + } + subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); } } diff --git a/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java b/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java deleted file mode 100644 index dacec7eb0..000000000 --- a/api/src/main/java/org/motechproject/nms/api/web/contract/kilkari/DeactivateSubscriptionContract.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.motechproject.nms.api.web.contract.kilkari; - -/** - * Created by sripooja on 20/6/16. - */ -public class DeactivateSubscriptionContract { - - private Long contactNumber; - - public DeactivateSubscriptionContract() { - } - - public Long getContactNumber() { - return contactNumber; - } - - public void setContactNumber(Long contactNumber) { - this.contactNumber = contactNumber; - } -} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java index 0ef565e22..7d4dd6325 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java @@ -77,4 +77,11 @@ public interface SubscriberService { @InstanceLifecycleListener(InstanceLifecycleListenerType.PRE_DELETE) void deleteAllowed(Subscriber subscriber); + + /** + * Deactivate all Subscriptions of a given callingNumber on MOHFW request + * @param callingNumber + */ + void deactivateAllSubscriptionsForSubscriber(long callingNumber); + } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java index bdd2f4323..b8fa4cbcd 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriptionService.java @@ -199,5 +199,4 @@ Subscription createSubscription(long callingNumber, Language language, Circle ci */ List retrieveAll(); - void deactivateSubscriptionForSpecificMsisdn(long callingNumber); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 37df21266..99f3f5af6 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -25,6 +25,8 @@ import org.motechproject.nms.region.domain.District; import org.motechproject.nms.region.domain.Language; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -40,6 +42,8 @@ @Service("subscriberService") public class SubscriberServiceImpl implements SubscriberService { + private static final Logger LOGGER = LoggerFactory.getLogger(SubscriberServiceImpl.class); + private SubscriberDataService subscriberDataService; private SubscriptionService subscriptionService; private SubscriptionDataService subscriptionDataService; @@ -362,4 +366,21 @@ public void deleteAllowed(Subscriber subscriber) { subscriptionService.deletePreconditionCheck(subscription); } } + + @Override + public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { + LOGGER.info("Recieved Release Number {} for Deactivation.", callingNumber); + Subscriber subscriberByMsisdn = this.getSubscriber(callingNumber); + if (subscriberByMsisdn == null) { + LOGGER.info("Subscriber for msisdn {} is not found." , callingNumber); + return; + } + LOGGER.info("Found Subscriber for msisdn {} .", callingNumber); + for (Subscription subscription : subscriberByMsisdn.getAllSubscriptions()) { + LOGGER.info("Deactivating Subscrition with start date {} for msisdn .", subscription.getStartDate()); + subscriptionService.deactivateSubscription(subscription, DeactivationReason.MOHFW_REQUEST); + LOGGER.info("Deactivated Subscrition with start date {} for msisdn .", subscription.getStartDate()); + } + } + } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 88ee88b7a..c7fd5ec15 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -29,7 +29,6 @@ import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; import org.motechproject.nms.kilkari.service.CsrVerifierService; -import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; import org.motechproject.nms.kilkari.utils.PhoneNumberHelper; @@ -70,9 +69,6 @@ public class SubscriptionServiceImpl implements SubscriptionService { private EventRelay eventRelay; private boolean allowMctsSubscriptions; - @Autowired - private SubscriberService subscriberService; - @Autowired public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade settingsFacade, // NO CHECKSTYLE More than 7 parameters SubscriberDataService subscriberDataService, @@ -695,22 +691,4 @@ public List retrieveAll() { return subscriptionDataService.retrieveAll(); } - - @Override - public void deactivateSubscriptionForSpecificMsisdn(long callingNumber) { - LOGGER.info("Recieved Release Number {} for Deactivation.", callingNumber); - Subscriber subscriberByMsisdn = subscriberService.getSubscriber(callingNumber); - if(subscriberByMsisdn == null) { - LOGGER.info("Subscriber for msisdn {} is not found." , callingNumber); - return; - } - LOGGER.info("Found Subscriber for msisdn {} .", callingNumber); - for(Subscription subscription : subscriberByMsisdn.getAllSubscriptions()) { - LOGGER.info("Deactivating Subscrition with start date {} for msisdn .", subscription.getStartDate()); - deactivateSubscription(subscription, DeactivationReason.MOHFW_REQUEST); - LOGGER.info("Deactivated Subscrition with start date {} for msisdn .", subscription.getStartDate()); - } - - } - } diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index 3027d53e8..4b81a9494 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -1,24 +1,28 @@ package org.motechproject.nms.testing.it.api; import org.apache.commons.httpclient.HttpStatus; +import org.apache.commons.httpclient.NameValuePair; +import org.apache.commons.httpclient.URIException; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; +import org.apache.http.message.BasicNameValuePair; +import org.apache.regexp.RE; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.motechproject.nms.api.web.contract.AddFlwRequest; -import org.motechproject.nms.api.web.contract.kilkari.DeactivateSubscriptionContract; import org.motechproject.nms.flw.domain.FlwError; import org.motechproject.nms.flw.domain.FlwErrorReason; import org.motechproject.nms.flw.domain.FrontLineWorker; import org.motechproject.nms.flw.repository.FlwErrorDataService; import org.motechproject.nms.flw.repository.FrontLineWorkerDataService; import org.motechproject.nms.kilkari.domain.Subscriber; -import org.motechproject.nms.kilkari.domain.Subscription; import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; import org.motechproject.nms.kilkari.repository.*; import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; -import org.motechproject.nms.props.repository.DeployedServiceDataService; import org.motechproject.nms.region.domain.District; import org.motechproject.nms.region.domain.HealthBlock; import org.motechproject.nms.region.domain.HealthFacility; @@ -30,7 +34,6 @@ import org.motechproject.nms.region.domain.Village; import org.motechproject.nms.region.repository.*; import org.motechproject.nms.region.service.DistrictService; -import org.motechproject.nms.region.service.HealthBlockService; import org.motechproject.nms.region.service.LanguageService; import org.motechproject.nms.testing.it.api.utils.RequestBuilder; import org.motechproject.nms.testing.it.utils.RegionHelper; @@ -44,13 +47,19 @@ import org.ops4j.pax.exam.junit.PaxExam; import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; import org.ops4j.pax.exam.spi.reactors.PerSuite; +import org.osgi.service.dmt.Uri; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.DefaultTransactionDefinition; import javax.inject.Inject; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -69,7 +78,7 @@ public class OpsControllerBundleIT extends BasePaxIT { private String addFlwEndpoint = String.format("http://localhost:%d/api/ops/createUpdateFlw", TestContext.getJettyPort()); - private String releaseNumber = String.format("http://localhost:%d/api/ops/releaseNumber", + private String deactivationRequest = String.format("http://localhost:%d/api/ops/deactivationRequest", TestContext.getJettyPort()); State state; District district; @@ -422,13 +431,13 @@ private void createSubscriberHelper() { // Test deactivation of specific msisdn @Test - public void testDeactivateSpecificMsisdn() throws IOException, InterruptedException { - - // Http Post request to deactivate subscriber - DeactivateSubscriptionContract deactivateSubscriptionContract = new DeactivateSubscriptionContract(); - deactivateSubscriptionContract.setContactNumber(1000000000L); - HttpPost httpRequest = RequestBuilder.createPostRequest(releaseNumber, deactivateSubscriptionContract); + public void testDeactivateSpecificMsisdn() throws IOException, InterruptedException, URISyntaxException { + // Http Delete request to deactivate subscriber + Map params = new LinkedHashMap<>(); + params.put("msisdn", "1000000000"); + HttpDelete httpRequest = RequestBuilder.createDeleteRequest(RequestBuilder.createUriWithQueryParamters("localhost",TestContext.getJettyPort() + , "/api/ops/deactivationRequest", params)); assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); } diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/utils/RequestBuilder.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/utils/RequestBuilder.java index 88c971393..b4518b707 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/utils/RequestBuilder.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/utils/RequestBuilder.java @@ -1,14 +1,20 @@ package org.motechproject.nms.testing.it.api.utils; +import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.StringEntity; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.map.JsonMappingException; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectWriter; +import org.motechproject.testing.utils.TestContext; import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Map; /** * Request builder helper class for web IT tests @@ -41,4 +47,20 @@ public static HttpGet createGetRequest(String endpoint) { HttpGet getRequest = new HttpGet(endpoint); return getRequest; } + + public static HttpDelete createDeleteRequest(String endpoint) { + HttpDelete deleteRequest = new HttpDelete(endpoint); + return deleteRequest; + } + + public static String createUriWithQueryParamters(String host, int port, String path, Map params) throws URISyntaxException { + URIBuilder builder = new URIBuilder(); + builder.setScheme("http").setHost(host).setPort(port).setPath(path); + for(String key : params.keySet()) { + builder.setParameter(key, params.get(key)); + } + + return builder.build().toString(); + + } } From e243f0a4ee3c238bb9bf813282277dc0cc4c0356 Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Thu, 30 Jun 2016 17:09:37 +0530 Subject: [PATCH 3/9] changed version to 1.0.18 and added more testcases for invalid msisdn --- api/pom.xml | 4 +- .../nms/api/web/OpsController.java | 11 ++-- csv/pom.xml | 4 +- flw/pom.xml | 4 +- imi/pom.xml | 4 +- kilkari/pom.xml | 4 +- .../kilkari/domain/DeactivationReason.java | 2 +- .../nms/kilkari/domain/Subscriber.java | 5 +- .../handler/SubscriptionManagerHandler.java | 2 +- .../kilkari/service/SubscriberService.java | 3 +- .../service/impl/CsrVerifierServiceImpl.java | 2 +- .../MctsBeneficiaryValueProcessorImpl.java | 6 ++- .../service/impl/SubscriberServiceImpl.java | 28 +++++----- .../service/impl/SubscriptionServiceImpl.java | 9 ++-- mcts/pom.xml | 4 +- mobile-academy/pom.xml | 4 +- pom.xml | 2 +- props/pom.xml | 4 +- region/pom.xml | 4 +- testing/pom.xml | 4 +- .../testing/it/api/OpsControllerBundleIT.java | 52 ++++++++++++++----- tracking/pom.xml | 4 +- 22 files changed, 98 insertions(+), 68 deletions(-) diff --git a/api/pom.xml b/api/pom.xml index 9de905e52..a6f37662f 100644 --- a/api/pom.xml +++ b/api/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ api - 1.0.17 + 1.0.18 bundle API module diff --git a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java index 22d2d2392..be99dc79e 100644 --- a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java +++ b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java @@ -21,6 +21,7 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; @@ -184,10 +185,9 @@ public GetBookmarkResponse getBookmarkWithScore(@RequestParam(required = false) @RequestMapping(value = "/deactivationRequest", method = RequestMethod.DELETE) - @ResponseStatus(HttpStatus.OK) @Transactional - public void deactivationRequest(@RequestParam(value = "msisdn") String msisdn) { - long phoneNumber = Long.parseLong(msisdn); + public ResponseEntity deactivationRequest(@RequestParam(value = "msisdn") Long msisdn) { + Long phoneNumber = msisdn; log("REQUEST: /ops/deactivationRequest", String.format( "callingNumber=%s", LogHelper.obscure(phoneNumber))); @@ -195,9 +195,10 @@ public void deactivationRequest(@RequestParam(value = "msisdn") String msisdn) { validateField10Digits(failureReasons, contactNumber, phoneNumber); validateFieldPositiveLong(failureReasons, contactNumber, phoneNumber); if (failureReasons.length() > 0) { - throw new IllegalArgumentException(failureReasons.toString()); + return new ResponseEntity(HttpStatus.BAD_REQUEST); } - subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); + HttpStatus status = subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); + return new ResponseEntity(status); } } diff --git a/csv/pom.xml b/csv/pom.xml index 9ce79b643..efc65a91b 100644 --- a/csv/pom.xml +++ b/csv/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ csv - 1.0.17 + 1.0.18 bundle CSV Module diff --git a/flw/pom.xml b/flw/pom.xml index e1105b6d8..d402c8eb9 100644 --- a/flw/pom.xml +++ b/flw/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ flw - 1.0.17 + 1.0.18 bundle FLW module diff --git a/imi/pom.xml b/imi/pom.xml index eaa3f52d1..6bd4c4511 100644 --- a/imi/pom.xml +++ b/imi/pom.xml @@ -7,12 +7,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ imi - 1.0.17 + 1.0.18 bundle IMI module diff --git a/kilkari/pom.xml b/kilkari/pom.xml index 65e51d8df..3202fcffd 100644 --- a/kilkari/pom.xml +++ b/kilkari/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ kilkari - 1.0.17 + 1.0.18 bundle Kilkari module diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java index 88a4021a7..1d6d8ef13 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationReason.java @@ -13,5 +13,5 @@ public enum DeactivationReason { INVALID_NUMBER, // See https://github.com/motech-implementations/mim/issues/169 DO_NOT_DISTURB, MCTS_UPDATE, - MOHFW_REQUEST + WEEKLY_CALLS_NOT_ANSWERED } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/Subscriber.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/Subscriber.java index aaabb6dd3..45f5f9501 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/Subscriber.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/Subscriber.java @@ -46,7 +46,7 @@ public class Subscriber extends MdsEntity { @Field private Circle circle; - //TODO: making this a bi-directional relationship until MOTECH-1638 is fixed. See #31. + // TODO: making this a bi-directional relationship until MOTECH-1638 is fixed. See #31. @Field @Persistent(mappedBy = "subscriber", defaultFetchGroup = "false") @JsonManagedReference @@ -148,8 +148,7 @@ public Set getActiveAndPendingSubscriptions() { @Ignore @JsonIgnore public Set getAllSubscriptions() { - // TODO: I have no idea why I need to do this, but returning just this.subscriptions always results in - // an empty set. Bi-directional relationship bug? + // TODO: I have no idea why I need to do this, but returning just this.subscriptions always results in an empty set. Bi-directional relationship bug? Set allSubscriptions = new HashSet<>(); Iterator subscriptionIterator = subscriptions.iterator(); diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/handler/SubscriptionManagerHandler.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/handler/SubscriptionManagerHandler.java index c4cb3877e..8e6b9f29b 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/handler/SubscriptionManagerHandler.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/handler/SubscriptionManagerHandler.java @@ -61,7 +61,7 @@ public void initSubscriptionManager() { LOGGER.debug("Scheduled cron job with subject {} and expression {}", KilkariConstants.SUBSCRIPTION_UPKEEP_SUBJECT, cronExpression); } - @MotechListener(subjects = { KilkariConstants.SUBSCRIPTION_UPKEEP_SUBJECT}) + @MotechListener(subjects = { KilkariConstants.SUBSCRIPTION_UPKEEP_SUBJECT }) public void upkeepSubscriptions(MotechEvent event) { LOGGER.debug("Received event for subscription upkeep"); DateTime tomorrow = DateTime.now().plusDays(1).withTimeAtStartOfDay(); diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java index 7d4dd6325..ef7950a37 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java @@ -8,6 +8,7 @@ import org.motechproject.nms.kilkari.domain.MctsMother; import org.motechproject.nms.kilkari.domain.Subscriber; import org.motechproject.nms.kilkari.domain.Subscription; +import org.springframework.http.HttpStatus; /** * Service interface for managing Kilkari subscribers @@ -82,6 +83,6 @@ public interface SubscriberService { * Deactivate all Subscriptions of a given callingNumber on MOHFW request * @param callingNumber */ - void deactivateAllSubscriptionsForSubscriber(long callingNumber); + HttpStatus deactivateAllSubscriptionsForSubscriber(long callingNumber); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/CsrVerifierServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/CsrVerifierServiceImpl.java index 813dd7cef..ed4842076 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/CsrVerifierServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/CsrVerifierServiceImpl.java @@ -67,7 +67,7 @@ public void cacheEvict() { eventRelay.broadcastEventMessage(new MotechEvent(KilkariConstants.LANGUAGE_CACHE_EVICT_SUBJECT)); } - @MotechListener(subjects = { KilkariConstants.CSR_VERIFIER_CACHE_EVICT_SUBJECT}) + @MotechListener(subjects = { KilkariConstants.CSR_VERIFIER_CACHE_EVICT_SUBJECT }) public void cacheEvict(MotechEvent event) { cacheEvict(); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java index 9d86796e5..d04b24376 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java @@ -23,6 +23,8 @@ public class MctsBeneficiaryValueProcessorImpl implements MctsBeneficiaryValuePr @Autowired private MctsChildDataService mctsChildDataService; + private final int msisdnLength = 10; + @Override public MctsMother getOrCreateMotherInstance(String value) { MctsMother mother = mctsMotherDataService.findByBeneficiaryId(value); @@ -92,10 +94,10 @@ public DateTime getDateByString(String value) { @Override public Long getMsisdnByString(String value) { - if (value.length() < 10) { + if (value.length() < msisdnLength) { throw new NumberFormatException("Beneficiary MSISDN too short, must be at least 10 digits"); } - String msisdn = value.substring(value.length() - 10); + String msisdn = value.substring(value.length() - msisdnLength); return Long.parseLong(msisdn); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 99f3f5af6..39529710f 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -3,17 +3,7 @@ import org.datanucleus.store.rdbms.query.ForwardQueryResult; import org.joda.time.DateTime; import org.motechproject.mds.query.SqlQueryExecution; -import org.motechproject.nms.kilkari.domain.DeactivationReason; -import org.motechproject.nms.kilkari.domain.MctsBeneficiary; -import org.motechproject.nms.kilkari.domain.MctsChild; -import org.motechproject.nms.kilkari.domain.MctsMother; -import org.motechproject.nms.kilkari.domain.Subscriber; -import org.motechproject.nms.kilkari.domain.Subscription; -import org.motechproject.nms.kilkari.domain.SubscriptionError; -import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; -import org.motechproject.nms.kilkari.domain.SubscriptionPack; -import org.motechproject.nms.kilkari.domain.SubscriptionPackType; -import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; +import org.motechproject.nms.kilkari.domain.*; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; @@ -28,6 +18,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -368,19 +359,24 @@ public void deleteAllowed(Subscriber subscriber) { } @Override - public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { + public HttpStatus deactivateAllSubscriptionsForSubscriber(long callingNumber) { LOGGER.info("Recieved Release Number {} for Deactivation.", callingNumber); Subscriber subscriberByMsisdn = this.getSubscriber(callingNumber); if (subscriberByMsisdn == null) { LOGGER.info("Subscriber for msisdn {} is not found." , callingNumber); - return; + return HttpStatus.NOT_FOUND; } LOGGER.info("Found Subscriber for msisdn {} .", callingNumber); + int counter = 0; for (Subscription subscription : subscriberByMsisdn.getAllSubscriptions()) { - LOGGER.info("Deactivating Subscrition with start date {} for msisdn .", subscription.getStartDate()); - subscriptionService.deactivateSubscription(subscription, DeactivationReason.MOHFW_REQUEST); - LOGGER.info("Deactivated Subscrition with start date {} for msisdn .", subscription.getStartDate()); + if ((subscription.getStatus() == SubscriptionStatus.PENDING_ACTIVATION) || (subscription.getStatus() == SubscriptionStatus.ACTIVE) || (subscription.getStatus() == SubscriptionStatus.HOLD)) { + LOGGER.info("Deactivating Subscrition with Id {} for msisdn.", subscription.getSubscriptionId()); + subscriptionService.deactivateSubscription(subscription, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); + counter++; + } } + LOGGER.info("Deactivated {} Subscritions for msisdn {}.", counter, callingNumber); + return HttpStatus.OK; } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index c7fd5ec15..4b6366934 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -308,6 +308,7 @@ private Subscription createSubscriptionViaIvr(Subscriber subscriber, Subscriptio private Subscription createSubscriptionViaMcts(Subscriber subscriber, SubscriptionPack pack) { if (!enrollmentPreconditionCheck(subscriber, pack)) { + LOGGER.info("PreCondition test passed"); return null; } @@ -325,6 +326,7 @@ private Subscription createSubscriptionViaMcts(Subscriber subscriber, Subscripti subscription.setStatus(SubscriptionStatus.HOLD); } + LOGGER.info("Creating Subscription ()", subscription.getSubscriptionId()); return subscriptionDataService.create(subscription); } @@ -578,10 +580,11 @@ private void deleteCallRetry(String subscriptionId) { @Override public void deactivateSubscription(Subscription subscription, DeactivationReason reason) { if (subscription != null && (subscription.getStatus() == SubscriptionStatus.ACTIVE || - subscription.getStatus() == SubscriptionStatus.PENDING_ACTIVATION)) { + subscription.getStatus() == SubscriptionStatus.PENDING_ACTIVATION || subscription.getStatus() == SubscriptionStatus.HOLD)) { subscription.setStatus(SubscriptionStatus.DEACTIVATED); subscription.setDeactivationReason(reason); - subscriptionDataService.update(subscription); + Subscription subscriptionDeativated = subscriptionDataService.update(subscription); + LOGGER.info("Deactivated Subscription "+ subscriptionDeativated.getSubscriptionId()); // Let's not retry calling subscribers with deactivated subscriptions deleteCallRetry(subscription.getSubscriptionId()); @@ -672,7 +675,7 @@ public void broadcastCacheEvictMessage(SubscriptionPack pack) { } - @MotechListener(subjects = { KilkariConstants.PACK_CACHE_EVICT_MESSAGE_SUBJECT}) + @MotechListener(subjects = { KilkariConstants.PACK_CACHE_EVICT_MESSAGE_SUBJECT }) @CacheEvict(value = {"pack" }, allEntries = true) public void cacheEvict(MotechEvent event) { csrVerifierService.cacheEvict(); diff --git a/mcts/pom.xml b/mcts/pom.xml index 885c91477..e1cdabc4f 100644 --- a/mcts/pom.xml +++ b/mcts/pom.xml @@ -6,12 +6,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ mcts - 1.0.17 + 1.0.18 bundle Mother Children Tracking Service diff --git a/mobile-academy/pom.xml b/mobile-academy/pom.xml index a551a6732..0b34263af 100644 --- a/mobile-academy/pom.xml +++ b/mobile-academy/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ mobile-academy - 1.0.17 + 1.0.18 bundle Mobile Academy module diff --git a/pom.xml b/pom.xml index b28b91f1d..7006d2479 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 pom National Motech System diff --git a/props/pom.xml b/props/pom.xml index b9d71ef31..25bd821b1 100644 --- a/props/pom.xml +++ b/props/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ props - 1.0.17 + 1.0.18 bundle Properties module diff --git a/region/pom.xml b/region/pom.xml index a84733c96..ed54bf419 100644 --- a/region/pom.xml +++ b/region/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ region - 1.0.17 + 1.0.18 bundle Region Module diff --git a/testing/pom.xml b/testing/pom.xml index 7e65ecc66..d43f07c50 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -7,12 +7,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ testing - 1.0.17 + 1.0.18 bundle Testing module diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index 4b81a9494..7ecced17f 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -5,10 +5,13 @@ import org.apache.commons.httpclient.URIException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpDelete; +import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.regexp.RE; +import org.joda.time.DateTime; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -18,6 +21,7 @@ import org.motechproject.nms.flw.domain.FrontLineWorker; import org.motechproject.nms.flw.repository.FlwErrorDataService; import org.motechproject.nms.flw.repository.FrontLineWorkerDataService; +import org.motechproject.nms.kilkari.domain.MctsMother; import org.motechproject.nms.kilkari.domain.Subscriber; import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; import org.motechproject.nms.kilkari.repository.*; @@ -125,6 +129,8 @@ public class OpsControllerBundleIT extends BasePaxIT { DistrictDataService districtDataService; @Inject DistrictService districtService; + @Inject + MctsMotherDataService mctsMotherDataService; private RegionHelper rh; @@ -418,28 +424,50 @@ private void createSubscriberHelper() { languageDataService, languageService, circleDataService, stateDataService, districtDataService, districtService); - Subscriber subscriber = subscriberDataService.create(new Subscriber(1000000000L)); + Subscriber subscriberIVR = subscriberDataService.create(new Subscriber(1000000000L)); + subscriberIVR.setLastMenstrualPeriod(DateTime.now().plusWeeks(70)); + subscriberIVR = subscriberDataService.update(subscriberIVR); - subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.hindiLanguage(), - sh.childPack(), SubscriptionOrigin.IVR); - subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.hindiLanguage(), + subscriptionService.createSubscription(subscriberIVR.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), sh.pregnancyPack(), SubscriptionOrigin.IVR); + Subscriber subscriberMCTS = subscriberDataService.create(new Subscriber(2000000000L)); + subscriberMCTS.setLastMenstrualPeriod(DateTime.now().plusWeeks(70)); + subscriberMCTS = subscriberDataService.update(subscriberMCTS); + subscriptionService.createSubscription(subscriberMCTS.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), + sh.pregnancyPack(), SubscriptionOrigin.MCTS_IMPORT); transactionManager.commit(status); } - // Test deactivation of specific msisdn + public HttpDelete FormHttpRequest(Long msisdn) { + StringBuilder sb = new StringBuilder(deactivationRequest); + sb.append("?"); + sb.append(String.format("msisdn=%s", msisdn.toString())); + HttpDelete httpRequest = new HttpDelete(sb.toString()); + return httpRequest; + } + + //Test deactivation of specific msisdn @Test - public void testDeactivateSpecificMsisdn() throws IOException, InterruptedException, URISyntaxException { + public void testDeactivateSpecificValidMsisdn() throws IOException, InterruptedException, URISyntaxException { - // Http Delete request to deactivate subscriber - Map params = new LinkedHashMap<>(); - params.put("msisdn", "1000000000"); - HttpDelete httpRequest = RequestBuilder.createDeleteRequest(RequestBuilder.createUriWithQueryParamters("localhost",TestContext.getJettyPort() - , "/api/ops/deactivationRequest", params)); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + HttpDelete httpRequestIVR = FormHttpRequest(1000000000L); + assertTrue(SimpleHttpClient.execHttpRequest(httpRequestIVR, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + + HttpDelete httpRequestMCTS = FormHttpRequest(2000000000L); + assertTrue(SimpleHttpClient.execHttpRequest(httpRequestMCTS, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + } + @Test + public void testDeactivateSpecificValidNotInDatabaseMsisdn() throws IOException, InterruptedException, URISyntaxException { + HttpDelete httpRequest = FormHttpRequest(3000000000L); + assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_NOT_FOUND, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); } + @Test + public void testDeactivateSpecificInValidMsisdn() throws IOException, InterruptedException, URISyntaxException { + HttpDelete httpRequest = FormHttpRequest(1000-00L); + assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_BAD_REQUEST, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + } } diff --git a/tracking/pom.xml b/tracking/pom.xml index 0c504f2c2..b1082ce66 100644 --- a/tracking/pom.xml +++ b/tracking/pom.xml @@ -5,12 +5,12 @@ nms org.motechproject.nms - 1.0.17 + 1.0.18 ../ tracking - 1.0.17 + 1.0.18 bundle Tracking Module From 9e75097e2a00e208199905d8086e860b68c99162 Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Fri, 1 Jul 2016 16:46:05 +0530 Subject: [PATCH 4/9] Fixed IT tests and comments from koshalt --- .../nms/api/web/OpsController.java | 9 +++-- .../kilkari/service/SubscriberService.java | 3 +- .../service/impl/SubscriberServiceImpl.java | 19 +++++++--- .../nms/kilkari/utils/KilkariConstants.java | 1 + testing/cdr-files-local | 2 ++ testing/pom.xml | 1 + .../testing/it/api/OpsControllerBundleIT.java | 35 ++++++++++++++----- 7 files changed, 50 insertions(+), 20 deletions(-) create mode 100644 testing/cdr-files-local diff --git a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java index be99dc79e..9f4b64657 100644 --- a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java +++ b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java @@ -21,7 +21,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestBody; @@ -185,8 +184,9 @@ public GetBookmarkResponse getBookmarkWithScore(@RequestParam(required = false) @RequestMapping(value = "/deactivationRequest", method = RequestMethod.DELETE) + @ResponseStatus(HttpStatus.OK) @Transactional - public ResponseEntity deactivationRequest(@RequestParam(value = "msisdn") Long msisdn) { + public void deactivationRequest(@RequestParam(value = "msisdn") Long msisdn) { Long phoneNumber = msisdn; log("REQUEST: /ops/deactivationRequest", String.format( "callingNumber=%s", @@ -195,10 +195,9 @@ public ResponseEntity deactivationRequest(@RequestParam(value = "msisdn") Long m validateField10Digits(failureReasons, contactNumber, phoneNumber); validateFieldPositiveLong(failureReasons, contactNumber, phoneNumber); if (failureReasons.length() > 0) { - return new ResponseEntity(HttpStatus.BAD_REQUEST); + throw new IllegalArgumentException(failureReasons.toString()); } - HttpStatus status = subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); - return new ResponseEntity(status); + subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java index ef7950a37..7d4dd6325 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/SubscriberService.java @@ -8,7 +8,6 @@ import org.motechproject.nms.kilkari.domain.MctsMother; import org.motechproject.nms.kilkari.domain.Subscriber; import org.motechproject.nms.kilkari.domain.Subscription; -import org.springframework.http.HttpStatus; /** * Service interface for managing Kilkari subscribers @@ -83,6 +82,6 @@ public interface SubscriberService { * Deactivate all Subscriptions of a given callingNumber on MOHFW request * @param callingNumber */ - HttpStatus deactivateAllSubscriptionsForSubscriber(long callingNumber); + void deactivateAllSubscriptionsForSubscriber(long callingNumber); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 39529710f..765c84f96 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -3,7 +3,18 @@ import org.datanucleus.store.rdbms.query.ForwardQueryResult; import org.joda.time.DateTime; import org.motechproject.mds.query.SqlQueryExecution; -import org.motechproject.nms.kilkari.domain.*; +import org.motechproject.nms.kilkari.domain.DeactivationReason; +import org.motechproject.nms.kilkari.domain.MctsBeneficiary; +import org.motechproject.nms.kilkari.domain.MctsChild; +import org.motechproject.nms.kilkari.domain.MctsMother; +import org.motechproject.nms.kilkari.domain.Subscriber; +import org.motechproject.nms.kilkari.domain.Subscription; +import org.motechproject.nms.kilkari.domain.SubscriptionError; +import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; +import org.motechproject.nms.kilkari.domain.SubscriptionPack; +import org.motechproject.nms.kilkari.domain.SubscriptionPackType; +import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; +import org.motechproject.nms.kilkari.domain.SubscriptionStatus; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; @@ -18,7 +29,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -359,12 +369,12 @@ public void deleteAllowed(Subscriber subscriber) { } @Override - public HttpStatus deactivateAllSubscriptionsForSubscriber(long callingNumber) { + public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { LOGGER.info("Recieved Release Number {} for Deactivation.", callingNumber); Subscriber subscriberByMsisdn = this.getSubscriber(callingNumber); if (subscriberByMsisdn == null) { LOGGER.info("Subscriber for msisdn {} is not found." , callingNumber); - return HttpStatus.NOT_FOUND; + throw new IllegalArgumentException(String.format(KilkariConstants.SUBSCRIBER_NOT_FOUND, callingNumber)); } LOGGER.info("Found Subscriber for msisdn {} .", callingNumber); int counter = 0; @@ -376,7 +386,6 @@ public HttpStatus deactivateAllSubscriptionsForSubscriber(long callingNumber) { } } LOGGER.info("Deactivated {} Subscritions for msisdn {}.", counter, callingNumber); - return HttpStatus.OK; } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java index f3bded782..b89a31a8a 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java @@ -71,6 +71,7 @@ public final class KilkariConstants { // Log constants public static final String MORE_THAN_ONE_SUBSCRIBER = "More than one subscriber returned for callingNumber %s"; + public static final String SUBSCRIBER_NOT_FOUND = "callingNumber %s not Found"; public static final String SQL_QUERY_LOG = "SQL QUERY: {}"; // Message constants diff --git a/testing/cdr-files-local b/testing/cdr-files-local new file mode 100644 index 000000000..6cad38653 --- /dev/null +++ b/testing/cdr-files-local @@ -0,0 +1,2 @@ +RequestId,Msisdn,CallId,AttemptNo,CallStartTime,CallAnswerTime,CallEndTime,CallDurationInPulse,CallStatus,LanguageLocationId,ContentFile,MsgPlayStartTime,MsgPlayEndTime,CircleId,OperatorId,Priority,CallDisconnectReason,WeekId +20160622121604:2d528584-77b0-4235-9c1a-f9d1d27ac0bf,6284895352,xxx,1,1,1466559966,1,1,1001,hi,w5_1.wav,1,117,DE,xx,0,1,w4_1 diff --git a/testing/pom.xml b/testing/pom.xml index d43f07c50..06f6d4848 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -164,6 +164,7 @@ target/failsafe-reports/failsafe-summary-it.xml ${modules.root.dir}/maven.properties random + -Xmx2048m -XX:MaxPermSize=2048m diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index 7ecced17f..b9e1f3144 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -21,9 +21,7 @@ import org.motechproject.nms.flw.domain.FrontLineWorker; import org.motechproject.nms.flw.repository.FlwErrorDataService; import org.motechproject.nms.flw.repository.FrontLineWorkerDataService; -import org.motechproject.nms.kilkari.domain.MctsMother; -import org.motechproject.nms.kilkari.domain.Subscriber; -import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; +import org.motechproject.nms.kilkari.domain.*; import org.motechproject.nms.kilkari.repository.*; import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; @@ -60,10 +58,7 @@ import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -440,6 +435,28 @@ private void createSubscriberHelper() { } + public void testifSubscriberDectivated() { + + TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); + + Subscriber subscriberIVR = subscriberDataService.findByNumber(1000000000L); + Set subscriptionsIVR = ( Set ) subscriberDataService.getDetachedField(subscriberIVR, "subscriptions"); + for (Subscription subscriptionIVR : subscriptionsIVR) { + Assert.assertTrue(subscriptionIVR.getDeactivationReason().equals(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); + Assert.assertTrue(subscriptionIVR.getStatus().equals(SubscriptionStatus.DEACTIVATED)); + } + + Subscriber subscriberMCTS = subscriberDataService.findByNumber(2000000000L); + Set subscriptionsMCTS = ( Set ) subscriberDataService.getDetachedField(subscriberMCTS, "subscriptions"); + for (Subscription subscriptionMCTS : subscriptionsMCTS) { + Assert.assertTrue(subscriptionMCTS.getDeactivationReason().equals(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); + Assert.assertTrue(subscriptionMCTS.getStatus().equals(SubscriptionStatus.DEACTIVATED)); + } + transactionManager.commit(status); + + } + + public HttpDelete FormHttpRequest(Long msisdn) { StringBuilder sb = new StringBuilder(deactivationRequest); sb.append("?"); @@ -457,12 +474,14 @@ public void testDeactivateSpecificValidMsisdn() throws IOException, InterruptedE HttpDelete httpRequestMCTS = FormHttpRequest(2000000000L); assertTrue(SimpleHttpClient.execHttpRequest(httpRequestMCTS, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + + testifSubscriberDectivated(); } @Test public void testDeactivateSpecificValidNotInDatabaseMsisdn() throws IOException, InterruptedException, URISyntaxException { HttpDelete httpRequest = FormHttpRequest(3000000000L); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_NOT_FOUND, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_BAD_REQUEST, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); } @Test From 944fe9c8dc03b8a3ed05fb3fee48bb409d355a6e Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Mon, 4 Jul 2016 19:47:19 +0530 Subject: [PATCH 5/9] Added a check to not create a subscription if that subscriber is deactivated due to WeeklyCallsNotAnswered --- .../nms/api/web/OpsController.java | 9 ++-- .../domain/SubscriptionRejectionReason.java | 3 +- .../WeeklyCallsNotAnsweredMsisdnRecord.java | 51 ++++++++++++++++++ ...llsNotAnsweredMsisdnRecordDataService.java | 12 +++++ .../MctsBeneficiaryValueProcessorImpl.java | 7 ++- .../service/impl/SubscriberServiceImpl.java | 10 +++- .../service/impl/SubscriptionServiceImpl.java | 19 ++++++- .../nms/kilkari/utils/KilkariConstants.java | 1 + .../resources/META-INF/spring/blueprint.xml | 4 ++ testing/pom.xml | 1 - .../testing/it/api/OpsControllerBundleIT.java | 53 +++++++++++-------- 11 files changed, 134 insertions(+), 36 deletions(-) create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java diff --git a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java index 9f4b64657..7d7086ff8 100644 --- a/api/src/main/java/org/motechproject/nms/api/web/OpsController.java +++ b/api/src/main/java/org/motechproject/nms/api/web/OpsController.java @@ -187,17 +187,16 @@ public GetBookmarkResponse getBookmarkWithScore(@RequestParam(required = false) @ResponseStatus(HttpStatus.OK) @Transactional public void deactivationRequest(@RequestParam(value = "msisdn") Long msisdn) { - Long phoneNumber = msisdn; log("REQUEST: /ops/deactivationRequest", String.format( "callingNumber=%s", - LogHelper.obscure(phoneNumber))); + LogHelper.obscure(msisdn))); StringBuilder failureReasons = new StringBuilder(); - validateField10Digits(failureReasons, contactNumber, phoneNumber); - validateFieldPositiveLong(failureReasons, contactNumber, phoneNumber); + validateField10Digits(failureReasons, contactNumber, msisdn); + validateFieldPositiveLong(failureReasons, contactNumber, msisdn); if (failureReasons.length() > 0) { throw new IllegalArgumentException(failureReasons.toString()); } - subscriberService.deactivateAllSubscriptionsForSubscriber(phoneNumber); + subscriberService.deactivateAllSubscriptionsForSubscriber(msisdn); } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/SubscriptionRejectionReason.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/SubscriptionRejectionReason.java index 732cca3cf..baadd510d 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/SubscriptionRejectionReason.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/SubscriptionRejectionReason.java @@ -9,5 +9,6 @@ public enum SubscriptionRejectionReason { MISSING_LMP, INVALID_DOB, INVALID_LMP, - MISSING_MSISDN; + MISSING_MSISDN, + WEEKLY_CALLS_NOT_ANSWERED; } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java new file mode 100644 index 000000000..189dfe8f3 --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java @@ -0,0 +1,51 @@ +package org.motechproject.nms.kilkari.domain; + +import org.joda.time.DateTime; +import org.motechproject.mds.annotations.Entity; +import org.motechproject.mds.annotations.Field; + +import javax.jdo.annotations.Column; +import javax.jdo.annotations.Unique; +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; + +/** + * Record of Weekly Calls Not Answered Deactivated Numbers + */ +@Entity(tableName = "nms_weekly_calls_not_answered_msisdn") +public class WeeklyCallsNotAnsweredMsisdnRecord { + + @Field + @Unique + @Min(value = 1000000000L, message = "callingNumber must be 10 digits") + @Max(value = 9999999999L, message = "callingNumber must be 10 digits") + @Column(length = 10, allowsNull = "false") + private Long callingNumber; + + @Field + private DateTime deativationDate; + + public WeeklyCallsNotAnsweredMsisdnRecord() { + } + + public WeeklyCallsNotAnsweredMsisdnRecord(Long callingNumber, DateTime deativationDate) { + this.callingNumber = callingNumber; + this.deativationDate = deativationDate; + } + + public Long getCallingNumber() { + return callingNumber; + } + + public void setCallingNumber(Long callingNumber) { + this.callingNumber = callingNumber; + } + + public DateTime getDeativationDate() { + return deativationDate; + } + + public void setDeativationDate(DateTime deativationDate) { + this.deativationDate = deativationDate; + } +} \ No newline at end of file diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java new file mode 100644 index 000000000..6af186d40 --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java @@ -0,0 +1,12 @@ +package org.motechproject.nms.kilkari.repository; + +import org.motechproject.mds.annotations.Lookup; +import org.motechproject.mds.annotations.LookupField; +import org.motechproject.mds.service.MotechDataService; +import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; + +public interface WeeklyCallsNotAnsweredMsisdnRecordDataService extends MotechDataService { + + @Lookup + WeeklyCallsNotAnsweredMsisdnRecord findByNumber(@LookupField(name = "callingNumber") Long callingNumber); +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java index d04b24376..bf2b86ef1 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/MctsBeneficiaryValueProcessorImpl.java @@ -11,6 +11,7 @@ import org.motechproject.nms.kilkari.repository.MctsChildDataService; import org.motechproject.nms.kilkari.repository.MctsMotherDataService; import org.motechproject.nms.kilkari.service.MctsBeneficiaryValueProcessor; +import org.motechproject.nms.kilkari.utils.KilkariConstants; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -23,8 +24,6 @@ public class MctsBeneficiaryValueProcessorImpl implements MctsBeneficiaryValuePr @Autowired private MctsChildDataService mctsChildDataService; - private final int msisdnLength = 10; - @Override public MctsMother getOrCreateMotherInstance(String value) { MctsMother mother = mctsMotherDataService.findByBeneficiaryId(value); @@ -94,10 +93,10 @@ public DateTime getDateByString(String value) { @Override public Long getMsisdnByString(String value) { - if (value.length() < msisdnLength) { + if (value.length() < KilkariConstants.MSISDN_LENGTH) { throw new NumberFormatException("Beneficiary MSISDN too short, must be at least 10 digits"); } - String msisdn = value.substring(value.length() - msisdnLength); + String msisdn = value.substring(value.length() - KilkariConstants.MSISDN_LENGTH); return Long.parseLong(msisdn); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 765c84f96..6165973af 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -15,10 +15,12 @@ import org.motechproject.nms.kilkari.domain.SubscriptionPackType; import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; import org.motechproject.nms.kilkari.domain.SubscriptionStatus; +import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; +import org.motechproject.nms.kilkari.repository.WeeklyCallsNotAnsweredMsisdnRecordDataService; import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; @@ -50,17 +52,20 @@ public class SubscriberServiceImpl implements SubscriberService { private SubscriptionDataService subscriptionDataService; private SubscriptionErrorDataService subscriptionErrorDataService; private SubscriptionPackDataService subscriptionPackDataService; + private WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; @Autowired public SubscriberServiceImpl(SubscriberDataService subscriberDataService, SubscriptionService subscriptionService, SubscriptionDataService subscriptionDataService, SubscriptionErrorDataService subscriptionErrorDataService, - SubscriptionPackDataService subscriptionPackDataService) { + SubscriptionPackDataService subscriptionPackDataService, + WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService) { this.subscriberDataService = subscriberDataService; this.subscriptionService = subscriptionService; this.subscriptionDataService = subscriptionDataService; this.subscriptionErrorDataService = subscriptionErrorDataService; this.subscriptionPackDataService = subscriptionPackDataService; + this.weeklyCallsNotAnsweredMsisdnRecordDataService = weeklyCallsNotAnsweredMsisdnRecordDataService; } @Override @@ -386,6 +391,9 @@ public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { } } LOGGER.info("Deactivated {} Subscritions for msisdn {}.", counter, callingNumber); + // Add callingNumber to WeeklyCallsNotAnsweredMsisdnRecord (tableName = "nms_weekly_calls_not_answered_msisdn") + weeklyCallsNotAnsweredMsisdnRecordDataService.create(new WeeklyCallsNotAnsweredMsisdnRecord(callingNumber, DateTime.now())); + LOGGER.info("Added callingNumber {} to WeeklyCallsNotAnsweredMsisdnRecord.", callingNumber); } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 4b6366934..382806d1a 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -23,11 +23,13 @@ import org.motechproject.nms.kilkari.domain.SubscriptionPackType; import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; import org.motechproject.nms.kilkari.domain.SubscriptionStatus; +import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; import org.motechproject.nms.kilkari.repository.CallRetryDataService; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; +import org.motechproject.nms.kilkari.repository.WeeklyCallsNotAnsweredMsisdnRecordDataService; import org.motechproject.nms.kilkari.service.CsrVerifierService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; @@ -68,6 +70,8 @@ public class SubscriptionServiceImpl implements SubscriptionService { private CsrVerifierService csrVerifierService; private EventRelay eventRelay; private boolean allowMctsSubscriptions; + private WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; + @Autowired public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade settingsFacade, // NO CHECKSTYLE More than 7 parameters @@ -77,7 +81,8 @@ public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade sett SubscriptionErrorDataService subscriptionErrorDataService, EventRelay eventRelay, CallRetryDataService callRetryDataService, - CsrVerifierService csrVerifierService) { + CsrVerifierService csrVerifierService, + WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService) { this.subscriberDataService = subscriberDataService; this.subscriptionPackDataService = subscriptionPackDataService; this.subscriptionDataService = subscriptionDataService; @@ -87,6 +92,7 @@ public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade sett this.callRetryDataService = callRetryDataService; this.csrVerifierService = csrVerifierService; this.allowMctsSubscriptions = true; + this.weeklyCallsNotAnsweredMsisdnRecordDataService = weeklyCallsNotAnsweredMsisdnRecordDataService; } @@ -224,6 +230,15 @@ public Subscription createSubscription(long callingNumber, Language language, Ci SubscriptionPack subscriptionPack, SubscriptionOrigin mode) { long number = PhoneNumberHelper.truncateLongNumber(callingNumber); + + // Check if the callingNumber is in Weekly_Calls_Not_Answered_Msisdn_Records + WeeklyCallsNotAnsweredMsisdnRecord weeklyCallsNotAnsweredMsisdnRecord = weeklyCallsNotAnsweredMsisdnRecordDataService.findByNumber(callingNumber); + if (weeklyCallsNotAnsweredMsisdnRecord != null) { + LOGGER.info("Can't create a Subscription as the number {} is deactivated due to Weekly Calls Not Answered", callingNumber); + subscriptionErrorDataService.create(new SubscriptionError(number, SubscriptionRejectionReason.WEEKLY_CALLS_NOT_ANSWERED, subscriptionPack.getType())); + return null; + } + Subscriber subscriber = subscriberDataService.findByNumber(callingNumber); Subscription subscription; @@ -584,7 +599,7 @@ public void deactivateSubscription(Subscription subscription, DeactivationReason subscription.setStatus(SubscriptionStatus.DEACTIVATED); subscription.setDeactivationReason(reason); Subscription subscriptionDeativated = subscriptionDataService.update(subscription); - LOGGER.info("Deactivated Subscription "+ subscriptionDeativated.getSubscriptionId()); + LOGGER.info("Deactivated Subscription " + subscriptionDeativated.getSubscriptionId()); // Let's not retry calling subscribers with deactivated subscriptions deleteCallRetry(subscription.getSubscriptionId()); diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java index b89a31a8a..3b42ad241 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/utils/KilkariConstants.java @@ -65,6 +65,7 @@ public final class KilkariConstants { public static final int CHILD_PACK_LENGTH_WEEKS = 48; public static final int CHILD_PACK_LENGTH_DAYS = CHILD_PACK_LENGTH_WEEKS * DAY_IN_WEEK; public static final Long DEFAULT_MAX_ACTIVE_SUBSCRIPTION_CAP = 1000000L; + public static final int MSISDN_LENGTH = 10; // SQL constants public static final String SELECT_SUBSCRIBERS_BY_NUMBER = "select * from nms_subscribers where callingNumber = ?"; diff --git a/kilkari/src/main/resources/META-INF/spring/blueprint.xml b/kilkari/src/main/resources/META-INF/spring/blueprint.xml index 41300696f..67fcb9b2e 100644 --- a/kilkari/src/main/resources/META-INF/spring/blueprint.xml +++ b/kilkari/src/main/resources/META-INF/spring/blueprint.xml @@ -111,6 +111,10 @@ + + + diff --git a/testing/pom.xml b/testing/pom.xml index 06f6d4848..d43f07c50 100644 --- a/testing/pom.xml +++ b/testing/pom.xml @@ -164,7 +164,6 @@ target/failsafe-reports/failsafe-summary-it.xml ${modules.root.dir}/maven.properties random - -Xmx2048m -XX:MaxPermSize=2048m diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index b9e1f3144..f26fa8acf 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -126,6 +126,8 @@ public class OpsControllerBundleIT extends BasePaxIT { DistrictService districtService; @Inject MctsMotherDataService mctsMotherDataService; + @Inject + WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; private RegionHelper rh; @@ -135,7 +137,6 @@ public class OpsControllerBundleIT extends BasePaxIT { public void setupTestData() { testingService.clearDatabase(); initializeLocationData(); - createSubscriberHelper(); } // Test flw update with empty flw request @@ -414,19 +415,18 @@ private void createSubscriberHelper() { // create subscription for a msisdn rh = new RegionHelper(languageDataService, languageService, circleDataService, stateDataService, districtDataService, districtService); - sh = new SubscriptionHelper(subscriptionService, subscriberDataService, subscriptionPackDataService, languageDataService, languageService, circleDataService, stateDataService, districtDataService, districtService); - Subscriber subscriberIVR = subscriberDataService.create(new Subscriber(1000000000L)); + Subscriber subscriberIVR = subscriberDataService.create(new Subscriber(5000000000L)); subscriberIVR.setLastMenstrualPeriod(DateTime.now().plusWeeks(70)); subscriberIVR = subscriberDataService.update(subscriberIVR); subscriptionService.createSubscription(subscriberIVR.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), sh.pregnancyPack(), SubscriptionOrigin.IVR); - Subscriber subscriberMCTS = subscriberDataService.create(new Subscriber(2000000000L)); + Subscriber subscriberMCTS = subscriberDataService.create(new Subscriber(6000000000L)); subscriberMCTS.setLastMenstrualPeriod(DateTime.now().plusWeeks(70)); subscriberMCTS = subscriberDataService.update(subscriberMCTS); subscriptionService.createSubscription(subscriberMCTS.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), @@ -435,18 +435,18 @@ private void createSubscriberHelper() { } - public void testifSubscriberDectivated() { + public void testifAllSubscriberDectivated() { TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); - Subscriber subscriberIVR = subscriberDataService.findByNumber(1000000000L); + Subscriber subscriberIVR = subscriberDataService.findByNumber(5000000000L); Set subscriptionsIVR = ( Set ) subscriberDataService.getDetachedField(subscriberIVR, "subscriptions"); for (Subscription subscriptionIVR : subscriptionsIVR) { Assert.assertTrue(subscriptionIVR.getDeactivationReason().equals(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); Assert.assertTrue(subscriptionIVR.getStatus().equals(SubscriptionStatus.DEACTIVATED)); } - Subscriber subscriberMCTS = subscriberDataService.findByNumber(2000000000L); + Subscriber subscriberMCTS = subscriberDataService.findByNumber(6000000000L); Set subscriptionsMCTS = ( Set ) subscriberDataService.getDetachedField(subscriberMCTS, "subscriptions"); for (Subscription subscriptionMCTS : subscriptionsMCTS) { Assert.assertTrue(subscriptionMCTS.getDeactivationReason().equals(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); @@ -457,36 +457,45 @@ public void testifSubscriberDectivated() { } - public HttpDelete FormHttpRequest(Long msisdn) { + private void testDeactivationRequestByMsisdn(Long msisdn, int status) throws IOException, InterruptedException, URISyntaxException { StringBuilder sb = new StringBuilder(deactivationRequest); sb.append("?"); sb.append(String.format("msisdn=%s", msisdn.toString())); HttpDelete httpRequest = new HttpDelete(sb.toString()); - return httpRequest; + assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, status, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); } - //Test deactivation of specific msisdn + //Test deactivation of specific msisdn - 5000000000L as IVR and 6000000000L as MCTS import @Test public void testDeactivateSpecificValidMsisdn() throws IOException, InterruptedException, URISyntaxException { - - HttpDelete httpRequestIVR = FormHttpRequest(1000000000L); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequestIVR, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); - - HttpDelete httpRequestMCTS = FormHttpRequest(2000000000L); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequestMCTS, HttpStatus.SC_OK, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); - - testifSubscriberDectivated(); + createSubscriberHelper(); + testDeactivationRequestByMsisdn(5000000000L, HttpStatus.SC_OK); + testDeactivationRequestByMsisdn(6000000000L, HttpStatus.SC_OK); + testifAllSubscriberDectivated(); + testReactivationDisabledAfterDeactivation(5000000000L); + testReactivationDisabledAfterDeactivation(6000000000L); } @Test public void testDeactivateSpecificValidNotInDatabaseMsisdn() throws IOException, InterruptedException, URISyntaxException { - HttpDelete httpRequest = FormHttpRequest(3000000000L); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_BAD_REQUEST, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + testDeactivationRequestByMsisdn(7000000000L, HttpStatus.SC_BAD_REQUEST); } @Test public void testDeactivateSpecificInValidMsisdn() throws IOException, InterruptedException, URISyntaxException { - HttpDelete httpRequest = FormHttpRequest(1000-00L); - assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, HttpStatus.SC_BAD_REQUEST, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); + testDeactivationRequestByMsisdn(1000-00L, HttpStatus.SC_BAD_REQUEST); + } + + private void testReactivationDisabledAfterDeactivation(long msisdn) throws IOException, InterruptedException, URISyntaxException { + TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); + // Testing weekly_calls_not_answered record with the given number + WeeklyCallsNotAnsweredMsisdnRecord weeklyCallsNotAnsweredMsisdnRecord = weeklyCallsNotAnsweredMsisdnRecordDataService.findByNumber(msisdn); + assertNotNull(weeklyCallsNotAnsweredMsisdnRecord); + + Subscriber subscriber = subscriberDataService.findByNumber(msisdn); + Subscription subscription = subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), + sh.pregnancyPack(), SubscriptionOrigin.IVR); + Assert.assertNull(subscription); + transactionManager.commit(status); } } From ea7fbd93cc0e055accbf07861337f760336ea1cb Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Thu, 7 Jul 2016 11:47:05 +0530 Subject: [PATCH 6/9] Added nms_weekly_calls_not_answered table to Clear Database table set --- .../nms/testing/service/impl/TestingServiceImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java b/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java index 10acaab5b..03b8f2136 100644 --- a/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java +++ b/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java @@ -133,6 +133,8 @@ public class TestingServiceImpl implements TestingService { "nms_whitelisted_states", "nms_whitelisted_states__TRASH", "TRACKING_MODULE_CHANGELOG", + "nms_weekly_calls_not_answered_msisdn", + "nms_weekly_calls_not_answered_msisdn__TRASH", "TRACKING_MODULE_CHANGELOG__TRASH" }; From b849b322376d19df3f159f14e61c8025eeee65b6 Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Tue, 12 Jul 2016 22:17:59 +0530 Subject: [PATCH 7/9] Added audit trail for deactivation subscriptions --- ...dnRecord.java => BlockedMsisdnRecord.java} | 23 ++--- .../DeactivationSubscriptionRecord.java | 95 +++++++++++++++++++ .../BlockedMsisdnRecordDataService.java | 12 +++ ...tivationSubscriptionRecordDataService.java | 7 ++ ...llsNotAnsweredMsisdnRecordDataService.java | 12 --- .../service/DeactivationAuditService.java | 11 +++ .../impl/DeactivationAuditServiceImpl.java | 50 ++++++++++ .../service/impl/SubscriberServiceImpl.java | 39 ++++++-- .../service/impl/SubscriptionServiceImpl.java | 14 +-- .../resources/META-INF/spring/blueprint.xml | 12 ++- .../service/impl/TestingServiceImpl.java | 6 +- .../testing/it/api/OpsControllerBundleIT.java | 7 +- 12 files changed, 240 insertions(+), 48 deletions(-) rename kilkari/src/main/java/org/motechproject/nms/kilkari/domain/{WeeklyCallsNotAnsweredMsisdnRecord.java => BlockedMsisdnRecord.java} (60%) create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/repository/BlockedMsisdnRecordDataService.java create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java delete mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/BlockedMsisdnRecord.java similarity index 60% rename from kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java rename to kilkari/src/main/java/org/motechproject/nms/kilkari/domain/BlockedMsisdnRecord.java index 189dfe8f3..4c8afc768 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/WeeklyCallsNotAnsweredMsisdnRecord.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/BlockedMsisdnRecord.java @@ -1,6 +1,5 @@ package org.motechproject.nms.kilkari.domain; -import org.joda.time.DateTime; import org.motechproject.mds.annotations.Entity; import org.motechproject.mds.annotations.Field; @@ -8,12 +7,13 @@ import javax.jdo.annotations.Unique; import javax.validation.constraints.Max; import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; /** * Record of Weekly Calls Not Answered Deactivated Numbers */ -@Entity(tableName = "nms_weekly_calls_not_answered_msisdn") -public class WeeklyCallsNotAnsweredMsisdnRecord { +@Entity(tableName = "nms_blocked_msisdn") +public class BlockedMsisdnRecord { @Field @Unique @@ -23,14 +23,15 @@ public class WeeklyCallsNotAnsweredMsisdnRecord { private Long callingNumber; @Field - private DateTime deativationDate; + @NotNull + private DeactivationReason deactivationReason; - public WeeklyCallsNotAnsweredMsisdnRecord() { + public BlockedMsisdnRecord() { } - public WeeklyCallsNotAnsweredMsisdnRecord(Long callingNumber, DateTime deativationDate) { + public BlockedMsisdnRecord(Long callingNumber, DeactivationReason deactivationReason) { this.callingNumber = callingNumber; - this.deativationDate = deativationDate; + this.deactivationReason = deactivationReason; } public Long getCallingNumber() { @@ -41,11 +42,11 @@ public void setCallingNumber(Long callingNumber) { this.callingNumber = callingNumber; } - public DateTime getDeativationDate() { - return deativationDate; + public DeactivationReason getDeactivationReason() { + return deactivationReason; } - public void setDeativationDate(DateTime deativationDate) { - this.deativationDate = deativationDate; + public void setDeactivationReason(DeactivationReason deactivationReason) { + this.deactivationReason = deactivationReason; } } \ No newline at end of file diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java new file mode 100644 index 000000000..38cc3186f --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java @@ -0,0 +1,95 @@ +package org.motechproject.nms.kilkari.domain; + +import org.motechproject.mds.annotations.Entity; +import org.motechproject.mds.annotations.Field; + +import javax.jdo.annotations.Column; +import javax.validation.constraints.NotNull; + +@Entity(tableName = "nms_deactivation_subscription_audit_records") +public class DeactivationSubscriptionRecord { + + public static final int MAX_OUTCOME_LENGTH = 1000; // Includes the "Failure: " string + + @Field + @NotNull + private String subscriptionId; + + @Field + @NotNull + private Long subscriberId; + + @Field + @NotNull + private SubscriptionOrigin subscriptionOrigin; + + @Field + @NotNull + private Long msisdn; + + @Field + @NotNull + private SubscriptionStatus preStatus; + + public SubscriptionStatus getStatus() { + return preStatus; + } + + public void setStatus(SubscriptionStatus status) { + this.preStatus = preStatus; + } + + @Field + @NotNull + @Column(length = MAX_OUTCOME_LENGTH) + private String outcome; + + public String getOutcome() { + return outcome; + } + + public void setOutcome(String outcome) { + this.outcome = outcome; + } + + public String getSubscriptionId() { + return subscriptionId; + } + + public void setSubscriptionId(String subscriptionId) { + this.subscriptionId = subscriptionId; + } + + public Long getSubscriberId() { + return subscriberId; + } + + public void setSubscriberId(Long subscriberId) { + this.subscriberId = subscriberId; + } + + public SubscriptionOrigin getSubscriptionOrigin() { + return subscriptionOrigin; + } + + public void setSubscriptionOrigin(SubscriptionOrigin subscriptionOrigin) { + this.subscriptionOrigin = subscriptionOrigin; + } + + public Long getMsisdn() { + return msisdn; + } + + public void setMsisdn(Long msisdn) { + this.msisdn = msisdn; + } + + public DeactivationSubscriptionRecord(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus preStatus, Long msisdn, String outcome) { + this.subscriptionId = subscriptionId; + this.subscriberId = subscriberId; + this.subscriptionOrigin = subscriptionOrigin; + this.msisdn = msisdn; + this.preStatus = preStatus; + this.outcome = outcome; + } +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/BlockedMsisdnRecordDataService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/BlockedMsisdnRecordDataService.java new file mode 100644 index 000000000..3fa501b47 --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/BlockedMsisdnRecordDataService.java @@ -0,0 +1,12 @@ +package org.motechproject.nms.kilkari.repository; + +import org.motechproject.mds.annotations.Lookup; +import org.motechproject.mds.annotations.LookupField; +import org.motechproject.mds.service.MotechDataService; +import org.motechproject.nms.kilkari.domain.BlockedMsisdnRecord; + +public interface BlockedMsisdnRecordDataService extends MotechDataService { + + @Lookup + BlockedMsisdnRecord findByNumber(@LookupField(name = "callingNumber") Long callingNumber); +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java new file mode 100644 index 000000000..f2170bc2f --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java @@ -0,0 +1,7 @@ +package org.motechproject.nms.kilkari.repository; + +import org.motechproject.mds.service.MotechDataService; +import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionRecord; + +public interface DeactivationSubscriptionRecordDataService extends MotechDataService { +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java deleted file mode 100644 index 6af186d40..000000000 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/WeeklyCallsNotAnsweredMsisdnRecordDataService.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.motechproject.nms.kilkari.repository; - -import org.motechproject.mds.annotations.Lookup; -import org.motechproject.mds.annotations.LookupField; -import org.motechproject.mds.service.MotechDataService; -import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; - -public interface WeeklyCallsNotAnsweredMsisdnRecordDataService extends MotechDataService { - - @Lookup - WeeklyCallsNotAnsweredMsisdnRecord findByNumber(@LookupField(name = "callingNumber") Long callingNumber); -} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java new file mode 100644 index 000000000..e09eb9d46 --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java @@ -0,0 +1,11 @@ +package org.motechproject.nms.kilkari.service; + +import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; +import org.motechproject.nms.kilkari.domain.SubscriptionStatus; + +public interface DeactivationAuditService { + + void auditSuccess(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn); + void auditFailure(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String failure); +} + diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java new file mode 100644 index 000000000..36c36b5ba --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java @@ -0,0 +1,50 @@ +package org.motechproject.nms.kilkari.service.impl; + + +import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionRecord; +import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; +import org.motechproject.nms.kilkari.domain.SubscriptionStatus; +import org.motechproject.nms.kilkari.repository.DeactivationSubscriptionRecordDataService; +import org.motechproject.nms.kilkari.service.DeactivationAuditService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service("deactivationAuditService") +public class DeactivationAuditServiceImpl implements DeactivationAuditService { + + private static final Logger LOGGER = LoggerFactory.getLogger(DeactivationAuditServiceImpl.class); + public static final String SUCCESS = "Success"; + public static final String FAILURE = "Failure: "; + + private DeactivationSubscriptionRecordDataService deactivationSubscriptionRecordDataService; + + @Autowired + public void setDeactivationSubscriptionRecordDataService(DeactivationSubscriptionRecordDataService deactivationSubscriptionRecordDataService) { + this.deactivationSubscriptionRecordDataService = deactivationSubscriptionRecordDataService; + } + + private void audit(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String outcome) { + String truncatedOutcome; + + if (outcome.length() > DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH) { + truncatedOutcome = outcome.substring(0, DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH); + LOGGER.warn("The provided outcome field was more than {} characters and was truncated, original " + + "outcome: {}", DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH - FAILURE.length(), outcome); + } else { + truncatedOutcome = outcome; + } + + deactivationSubscriptionRecordDataService.create(new DeactivationSubscriptionRecord(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, truncatedOutcome)); + } + @Override + public void auditSuccess(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn) { + audit(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, SUCCESS); + } + + @Override + public void auditFailure(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String failure) { + audit(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, FAILURE + failure); + } +} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 6165973af..7cbd1557e 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -1,5 +1,6 @@ package org.motechproject.nms.kilkari.service.impl; +import org.apache.commons.lang.exception.ExceptionUtils; import org.datanucleus.store.rdbms.query.ForwardQueryResult; import org.joda.time.DateTime; import org.motechproject.mds.query.SqlQueryExecution; @@ -15,12 +16,13 @@ import org.motechproject.nms.kilkari.domain.SubscriptionPackType; import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; import org.motechproject.nms.kilkari.domain.SubscriptionStatus; -import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; +import org.motechproject.nms.kilkari.domain.BlockedMsisdnRecord; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; -import org.motechproject.nms.kilkari.repository.WeeklyCallsNotAnsweredMsisdnRecordDataService; +import org.motechproject.nms.kilkari.repository.BlockedMsisdnRecordDataService; +import org.motechproject.nms.kilkari.service.DeactivationAuditService; import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; @@ -52,20 +54,23 @@ public class SubscriberServiceImpl implements SubscriberService { private SubscriptionDataService subscriptionDataService; private SubscriptionErrorDataService subscriptionErrorDataService; private SubscriptionPackDataService subscriptionPackDataService; - private WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; + private DeactivationAuditService deactivationAuditService; + private BlockedMsisdnRecordDataService blockedMsisdnRecordDataService; @Autowired public SubscriberServiceImpl(SubscriberDataService subscriberDataService, SubscriptionService subscriptionService, SubscriptionDataService subscriptionDataService, SubscriptionErrorDataService subscriptionErrorDataService, SubscriptionPackDataService subscriptionPackDataService, - WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService) { + BlockedMsisdnRecordDataService blockedMsisdnRecordDataService, + DeactivationAuditService deactivationAuditService) { this.subscriberDataService = subscriberDataService; this.subscriptionService = subscriptionService; this.subscriptionDataService = subscriptionDataService; this.subscriptionErrorDataService = subscriptionErrorDataService; this.subscriptionPackDataService = subscriptionPackDataService; - this.weeklyCallsNotAnsweredMsisdnRecordDataService = weeklyCallsNotAnsweredMsisdnRecordDataService; + this.deactivationAuditService = deactivationAuditService; + this.blockedMsisdnRecordDataService = blockedMsisdnRecordDataService; } @Override @@ -385,14 +390,28 @@ public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { int counter = 0; for (Subscription subscription : subscriberByMsisdn.getAllSubscriptions()) { if ((subscription.getStatus() == SubscriptionStatus.PENDING_ACTIVATION) || (subscription.getStatus() == SubscriptionStatus.ACTIVE) || (subscription.getStatus() == SubscriptionStatus.HOLD)) { - LOGGER.info("Deactivating Subscrition with Id {} for msisdn.", subscription.getSubscriptionId()); - subscriptionService.deactivateSubscription(subscription, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); - counter++; + try { + LOGGER.info("Deactivating Subscription with Id {} for msisdn.", subscription.getSubscriptionId()); + subscriptionService.deactivateSubscription(subscription, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); + deactivationAuditService.auditSuccess(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), subscription.getStatus(), callingNumber); + counter++; + } catch (Exception e) { + LOGGER.error(String.format("Unexpected exception in deactivating subscription %s: %s", subscription.getSubscriptionId(), ExceptionUtils.getFullStackTrace(e))); + deactivationAuditService.auditFailure(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), subscription.getStatus(), callingNumber, ExceptionUtils.getFullStackTrace(e)); + throw new IllegalStateException(e); + } } } LOGGER.info("Deactivated {} Subscritions for msisdn {}.", counter, callingNumber); - // Add callingNumber to WeeklyCallsNotAnsweredMsisdnRecord (tableName = "nms_weekly_calls_not_answered_msisdn") - weeklyCallsNotAnsweredMsisdnRecordDataService.create(new WeeklyCallsNotAnsweredMsisdnRecord(callingNumber, DateTime.now())); + // Add callingNumber to WeeklyCallsNotAnsweredMsisdnRecord (tableName = "nms_blocked_msisdn") + BlockedMsisdnRecord record = blockedMsisdnRecordDataService.findByNumber(callingNumber); + //TODO: we can use createOrUpdate method of MotechDataService once the bug is fixed. + if (record == null) { + blockedMsisdnRecordDataService.create(new BlockedMsisdnRecord(callingNumber, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); + } else { + record.setDeactivationReason(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); + blockedMsisdnRecordDataService.update(record); + } LOGGER.info("Added callingNumber {} to WeeklyCallsNotAnsweredMsisdnRecord.", callingNumber); } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java index 382806d1a..7cbec90b3 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriptionServiceImpl.java @@ -23,13 +23,13 @@ import org.motechproject.nms.kilkari.domain.SubscriptionPackType; import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; import org.motechproject.nms.kilkari.domain.SubscriptionStatus; -import org.motechproject.nms.kilkari.domain.WeeklyCallsNotAnsweredMsisdnRecord; +import org.motechproject.nms.kilkari.domain.BlockedMsisdnRecord; import org.motechproject.nms.kilkari.repository.CallRetryDataService; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; -import org.motechproject.nms.kilkari.repository.WeeklyCallsNotAnsweredMsisdnRecordDataService; +import org.motechproject.nms.kilkari.repository.BlockedMsisdnRecordDataService; import org.motechproject.nms.kilkari.service.CsrVerifierService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; @@ -70,7 +70,7 @@ public class SubscriptionServiceImpl implements SubscriptionService { private CsrVerifierService csrVerifierService; private EventRelay eventRelay; private boolean allowMctsSubscriptions; - private WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; + private BlockedMsisdnRecordDataService blockedMsisdnRecordDataService; @Autowired @@ -82,7 +82,7 @@ public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade sett EventRelay eventRelay, CallRetryDataService callRetryDataService, CsrVerifierService csrVerifierService, - WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService) { + BlockedMsisdnRecordDataService blockedMsisdnRecordDataService) { this.subscriberDataService = subscriberDataService; this.subscriptionPackDataService = subscriptionPackDataService; this.subscriptionDataService = subscriptionDataService; @@ -92,7 +92,7 @@ public SubscriptionServiceImpl(@Qualifier("kilkariSettings") SettingsFacade sett this.callRetryDataService = callRetryDataService; this.csrVerifierService = csrVerifierService; this.allowMctsSubscriptions = true; - this.weeklyCallsNotAnsweredMsisdnRecordDataService = weeklyCallsNotAnsweredMsisdnRecordDataService; + this.blockedMsisdnRecordDataService = blockedMsisdnRecordDataService; } @@ -232,8 +232,8 @@ public Subscription createSubscription(long callingNumber, Language language, Ci long number = PhoneNumberHelper.truncateLongNumber(callingNumber); // Check if the callingNumber is in Weekly_Calls_Not_Answered_Msisdn_Records - WeeklyCallsNotAnsweredMsisdnRecord weeklyCallsNotAnsweredMsisdnRecord = weeklyCallsNotAnsweredMsisdnRecordDataService.findByNumber(callingNumber); - if (weeklyCallsNotAnsweredMsisdnRecord != null) { + BlockedMsisdnRecord blockedMsisdnRecord = blockedMsisdnRecordDataService.findByNumber(callingNumber); + if (blockedMsisdnRecord != null) { LOGGER.info("Can't create a Subscription as the number {} is deactivated due to Weekly Calls Not Answered", callingNumber); subscriptionErrorDataService.create(new SubscriptionError(number, SubscriptionRejectionReason.WEEKLY_CALLS_NOT_ANSWERED, subscriptionPack.getType())); return null; diff --git a/kilkari/src/main/resources/META-INF/spring/blueprint.xml b/kilkari/src/main/resources/META-INF/spring/blueprint.xml index 67fcb9b2e..65c8c8635 100644 --- a/kilkari/src/main/resources/META-INF/spring/blueprint.xml +++ b/kilkari/src/main/resources/META-INF/spring/blueprint.xml @@ -111,9 +111,15 @@ - - + + + + + + diff --git a/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java b/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java index 03b8f2136..c97583319 100644 --- a/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java +++ b/testing/src/main/java/org/motechproject/nms/testing/service/impl/TestingServiceImpl.java @@ -132,9 +132,11 @@ public class TestingServiceImpl implements TestingService { "nms_whitelist_entries__TRASH", "nms_whitelisted_states", "nms_whitelisted_states__TRASH", + "nms_blocked_msisdn", + "nms_blocked_msisdn__TRASH", + "nms_deactivation_subscription_audit_records", + "nms_deactivation_subscription_audit_records__TRASH", "TRACKING_MODULE_CHANGELOG", - "nms_weekly_calls_not_answered_msisdn", - "nms_weekly_calls_not_answered_msisdn__TRASH", "TRACKING_MODULE_CHANGELOG__TRASH" }; diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index f26fa8acf..2ca450fa6 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -127,7 +127,7 @@ public class OpsControllerBundleIT extends BasePaxIT { @Inject MctsMotherDataService mctsMotherDataService; @Inject - WeeklyCallsNotAnsweredMsisdnRecordDataService weeklyCallsNotAnsweredMsisdnRecordDataService; + BlockedMsisdnRecordDataService blockedMsisdnRecordDataService; private RegionHelper rh; @@ -470,6 +470,7 @@ private void testDeactivationRequestByMsisdn(Long msisdn, int status) throws IOE public void testDeactivateSpecificValidMsisdn() throws IOException, InterruptedException, URISyntaxException { createSubscriberHelper(); testDeactivationRequestByMsisdn(5000000000L, HttpStatus.SC_OK); + testDeactivationRequestByMsisdn(5000000000L, HttpStatus.SC_OK); // Test deactivation of same number again testDeactivationRequestByMsisdn(6000000000L, HttpStatus.SC_OK); testifAllSubscriberDectivated(); testReactivationDisabledAfterDeactivation(5000000000L); @@ -489,8 +490,8 @@ public void testDeactivateSpecificInValidMsisdn() throws IOException, Interrupte private void testReactivationDisabledAfterDeactivation(long msisdn) throws IOException, InterruptedException, URISyntaxException { TransactionStatus status = transactionManager.getTransaction(new DefaultTransactionDefinition()); // Testing weekly_calls_not_answered record with the given number - WeeklyCallsNotAnsweredMsisdnRecord weeklyCallsNotAnsweredMsisdnRecord = weeklyCallsNotAnsweredMsisdnRecordDataService.findByNumber(msisdn); - assertNotNull(weeklyCallsNotAnsweredMsisdnRecord); + BlockedMsisdnRecord blockedMsisdnRecord = blockedMsisdnRecordDataService.findByNumber(msisdn); + assertNotNull(blockedMsisdnRecord); Subscriber subscriber = subscriberDataService.findByNumber(msisdn); Subscription subscription = subscriptionService.createSubscription(subscriber.getCallingNumber(), rh.kannadaLanguage(), rh.karnatakaCircle(), From b28518ad9f31dc99940d7b20a6ee51353f8e8abd Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Tue, 12 Jul 2016 23:44:14 +0530 Subject: [PATCH 8/9] Modified audit service of deactivation subscription --- .../nms/kilkari/domain/AuditStatus.java | 9 ++++ ... DeactivationSubscriptionAuditRecord.java} | 28 ++++++++--- ...onSubscriptionAuditRecordDataService.java} | 4 +- .../service/DeactivationAuditService.java | 11 ---- .../impl/DeactivationAuditServiceImpl.java | 50 ------------------- .../service/impl/SubscriberServiceImpl.java | 26 +++++++--- .../resources/META-INF/spring/blueprint.xml | 7 +-- 7 files changed, 51 insertions(+), 84 deletions(-) create mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/domain/AuditStatus.java rename kilkari/src/main/java/org/motechproject/nms/kilkari/domain/{DeactivationSubscriptionRecord.java => DeactivationSubscriptionAuditRecord.java} (75%) rename kilkari/src/main/java/org/motechproject/nms/kilkari/repository/{DeactivationSubscriptionRecordDataService.java => DeactivationSubscriptionAuditRecordDataService.java} (54%) delete mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java delete mode 100644 kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/AuditStatus.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/AuditStatus.java new file mode 100644 index 000000000..4fd6d1e0e --- /dev/null +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/AuditStatus.java @@ -0,0 +1,9 @@ +package org.motechproject.nms.kilkari.domain; + +/** + * The Audit Status + */ +public enum AuditStatus { + SUCCESS, + FAILURE +} \ No newline at end of file diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionAuditRecord.java similarity index 75% rename from kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java rename to kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionAuditRecord.java index 38cc3186f..5b91eedf9 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionRecord.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/domain/DeactivationSubscriptionAuditRecord.java @@ -7,7 +7,7 @@ import javax.validation.constraints.NotNull; @Entity(tableName = "nms_deactivation_subscription_audit_records") -public class DeactivationSubscriptionRecord { +public class DeactivationSubscriptionAuditRecord { public static final int MAX_OUTCOME_LENGTH = 1000; // Includes the "Failure: " string @@ -31,18 +31,29 @@ public class DeactivationSubscriptionRecord { @NotNull private SubscriptionStatus preStatus; - public SubscriptionStatus getStatus() { + @Field + @NotNull + private AuditStatus auditStatus; + + @Field + @Column(length = MAX_OUTCOME_LENGTH) + private String outcome; + + public SubscriptionStatus getPreStatus() { return preStatus; } - public void setStatus(SubscriptionStatus status) { + public void setPreStatus(SubscriptionStatus preStatus) { this.preStatus = preStatus; } - @Field - @NotNull - @Column(length = MAX_OUTCOME_LENGTH) - private String outcome; + public AuditStatus getAuditStatus() { + return auditStatus; + } + + public void setAuditStatus(AuditStatus auditStatus) { + this.auditStatus = auditStatus; + } public String getOutcome() { return outcome; @@ -84,12 +95,13 @@ public void setMsisdn(Long msisdn) { this.msisdn = msisdn; } - public DeactivationSubscriptionRecord(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus preStatus, Long msisdn, String outcome) { + public DeactivationSubscriptionAuditRecord(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, Long msisdn, SubscriptionStatus preStatus, AuditStatus auditStatus, String outcome) { this.subscriptionId = subscriptionId; this.subscriberId = subscriberId; this.subscriptionOrigin = subscriptionOrigin; this.msisdn = msisdn; this.preStatus = preStatus; + this.auditStatus = auditStatus; this.outcome = outcome; } } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionAuditRecordDataService.java similarity index 54% rename from kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java rename to kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionAuditRecordDataService.java index f2170bc2f..dba9821b9 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionRecordDataService.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/repository/DeactivationSubscriptionAuditRecordDataService.java @@ -1,7 +1,7 @@ package org.motechproject.nms.kilkari.repository; import org.motechproject.mds.service.MotechDataService; -import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionRecord; +import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionAuditRecord; -public interface DeactivationSubscriptionRecordDataService extends MotechDataService { +public interface DeactivationSubscriptionAuditRecordDataService extends MotechDataService { } diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java deleted file mode 100644 index e09eb9d46..000000000 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/DeactivationAuditService.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.motechproject.nms.kilkari.service; - -import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; -import org.motechproject.nms.kilkari.domain.SubscriptionStatus; - -public interface DeactivationAuditService { - - void auditSuccess(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn); - void auditFailure(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String failure); -} - diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java deleted file mode 100644 index 36c36b5ba..000000000 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/DeactivationAuditServiceImpl.java +++ /dev/null @@ -1,50 +0,0 @@ -package org.motechproject.nms.kilkari.service.impl; - - -import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionRecord; -import org.motechproject.nms.kilkari.domain.SubscriptionOrigin; -import org.motechproject.nms.kilkari.domain.SubscriptionStatus; -import org.motechproject.nms.kilkari.repository.DeactivationSubscriptionRecordDataService; -import org.motechproject.nms.kilkari.service.DeactivationAuditService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -@Service("deactivationAuditService") -public class DeactivationAuditServiceImpl implements DeactivationAuditService { - - private static final Logger LOGGER = LoggerFactory.getLogger(DeactivationAuditServiceImpl.class); - public static final String SUCCESS = "Success"; - public static final String FAILURE = "Failure: "; - - private DeactivationSubscriptionRecordDataService deactivationSubscriptionRecordDataService; - - @Autowired - public void setDeactivationSubscriptionRecordDataService(DeactivationSubscriptionRecordDataService deactivationSubscriptionRecordDataService) { - this.deactivationSubscriptionRecordDataService = deactivationSubscriptionRecordDataService; - } - - private void audit(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String outcome) { - String truncatedOutcome; - - if (outcome.length() > DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH) { - truncatedOutcome = outcome.substring(0, DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH); - LOGGER.warn("The provided outcome field was more than {} characters and was truncated, original " + - "outcome: {}", DeactivationSubscriptionRecord.MAX_OUTCOME_LENGTH - FAILURE.length(), outcome); - } else { - truncatedOutcome = outcome; - } - - deactivationSubscriptionRecordDataService.create(new DeactivationSubscriptionRecord(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, truncatedOutcome)); - } - @Override - public void auditSuccess(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn) { - audit(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, SUCCESS); - } - - @Override - public void auditFailure(String subscriptionId, Long subscriberId, SubscriptionOrigin subscriptionOrigin, SubscriptionStatus status, Long msisdn, String failure) { - audit(subscriptionId, subscriberId, subscriptionOrigin, status, msisdn, FAILURE + failure); - } -} diff --git a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java index 7cbd1557e..3c2a2b87e 100644 --- a/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java +++ b/kilkari/src/main/java/org/motechproject/nms/kilkari/service/impl/SubscriberServiceImpl.java @@ -17,12 +17,14 @@ import org.motechproject.nms.kilkari.domain.SubscriptionRejectionReason; import org.motechproject.nms.kilkari.domain.SubscriptionStatus; import org.motechproject.nms.kilkari.domain.BlockedMsisdnRecord; +import org.motechproject.nms.kilkari.domain.DeactivationSubscriptionAuditRecord; +import org.motechproject.nms.kilkari.domain.AuditStatus; import org.motechproject.nms.kilkari.repository.SubscriberDataService; import org.motechproject.nms.kilkari.repository.SubscriptionDataService; import org.motechproject.nms.kilkari.repository.SubscriptionErrorDataService; import org.motechproject.nms.kilkari.repository.SubscriptionPackDataService; import org.motechproject.nms.kilkari.repository.BlockedMsisdnRecordDataService; -import org.motechproject.nms.kilkari.service.DeactivationAuditService; +import org.motechproject.nms.kilkari.repository.DeactivationSubscriptionAuditRecordDataService; import org.motechproject.nms.kilkari.service.SubscriberService; import org.motechproject.nms.kilkari.service.SubscriptionService; import org.motechproject.nms.kilkari.utils.KilkariConstants; @@ -54,7 +56,7 @@ public class SubscriberServiceImpl implements SubscriberService { private SubscriptionDataService subscriptionDataService; private SubscriptionErrorDataService subscriptionErrorDataService; private SubscriptionPackDataService subscriptionPackDataService; - private DeactivationAuditService deactivationAuditService; + private DeactivationSubscriptionAuditRecordDataService deactivationSubscriptionAuditRecordDataService; private BlockedMsisdnRecordDataService blockedMsisdnRecordDataService; @Autowired @@ -63,13 +65,13 @@ public SubscriberServiceImpl(SubscriberDataService subscriberDataService, Subscr SubscriptionErrorDataService subscriptionErrorDataService, SubscriptionPackDataService subscriptionPackDataService, BlockedMsisdnRecordDataService blockedMsisdnRecordDataService, - DeactivationAuditService deactivationAuditService) { + DeactivationSubscriptionAuditRecordDataService deactivationSubscriptionAuditRecordDataService) { this.subscriberDataService = subscriberDataService; this.subscriptionService = subscriptionService; this.subscriptionDataService = subscriptionDataService; this.subscriptionErrorDataService = subscriptionErrorDataService; this.subscriptionPackDataService = subscriptionPackDataService; - this.deactivationAuditService = deactivationAuditService; + this.deactivationSubscriptionAuditRecordDataService = deactivationSubscriptionAuditRecordDataService; this.blockedMsisdnRecordDataService = blockedMsisdnRecordDataService; } @@ -393,11 +395,18 @@ public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { try { LOGGER.info("Deactivating Subscription with Id {} for msisdn.", subscription.getSubscriptionId()); subscriptionService.deactivateSubscription(subscription, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); - deactivationAuditService.auditSuccess(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), subscription.getStatus(), callingNumber); + deactivationSubscriptionAuditRecordDataService.create(new DeactivationSubscriptionAuditRecord(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), callingNumber, subscription.getStatus(), AuditStatus.SUCCESS, "")); counter++; } catch (Exception e) { - LOGGER.error(String.format("Unexpected exception in deactivating subscription %s: %s", subscription.getSubscriptionId(), ExceptionUtils.getFullStackTrace(e))); - deactivationAuditService.auditFailure(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), subscription.getStatus(), callingNumber, ExceptionUtils.getFullStackTrace(e)); + String error = ExceptionUtils.getFullStackTrace(e); + String truncatedError; + LOGGER.error(String.format("Unexpected exception in deactivating subscription %s: %s", subscription.getSubscriptionId(), error)); + if (error.length() > DeactivationSubscriptionAuditRecord.MAX_OUTCOME_LENGTH) { + truncatedError = error.substring(0, DeactivationSubscriptionAuditRecord.MAX_OUTCOME_LENGTH); + } else { + truncatedError = error; + } + deactivationSubscriptionAuditRecordDataService.create(new DeactivationSubscriptionAuditRecord(subscription.getSubscriptionId(), subscriberByMsisdn.getId(), subscription.getOrigin(), callingNumber, subscription.getStatus(), AuditStatus.FAILURE, truncatedError)); throw new IllegalStateException(e); } } @@ -408,11 +417,12 @@ public void deactivateAllSubscriptionsForSubscriber(long callingNumber) { //TODO: we can use createOrUpdate method of MotechDataService once the bug is fixed. if (record == null) { blockedMsisdnRecordDataService.create(new BlockedMsisdnRecord(callingNumber, DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED)); + LOGGER.info("Added callingNumber {} to BlockedMsisdnRecord.", callingNumber); } else { record.setDeactivationReason(DeactivationReason.WEEKLY_CALLS_NOT_ANSWERED); blockedMsisdnRecordDataService.update(record); + LOGGER.info("Updated existing BlockedMsisdnRecord for callingNumber {}", callingNumber); } - LOGGER.info("Added callingNumber {} to WeeklyCallsNotAnsweredMsisdnRecord.", callingNumber); } } diff --git a/kilkari/src/main/resources/META-INF/spring/blueprint.xml b/kilkari/src/main/resources/META-INF/spring/blueprint.xml index 65c8c8635..d3c8a637a 100644 --- a/kilkari/src/main/resources/META-INF/spring/blueprint.xml +++ b/kilkari/src/main/resources/META-INF/spring/blueprint.xml @@ -115,11 +115,8 @@ - - - + From 81c1242d7564615b4811b4d9ca2eb4e2ffca7dac Mon Sep 17 00:00:00 2001 From: Sri Pooja Date: Wed, 13 Jul 2016 17:40:41 +0530 Subject: [PATCH 9/9] Added test to check audit records --- .../nms/testing/it/api/OpsControllerBundleIT.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java index 2ca450fa6..1c8553ac5 100644 --- a/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java +++ b/testing/src/test/java/org/motechproject/nms/testing/it/api/OpsControllerBundleIT.java @@ -128,6 +128,8 @@ public class OpsControllerBundleIT extends BasePaxIT { MctsMotherDataService mctsMotherDataService; @Inject BlockedMsisdnRecordDataService blockedMsisdnRecordDataService; + @Inject + DeactivationSubscriptionAuditRecordDataService deactivationSubscriptionAuditRecordDataService; private RegionHelper rh; @@ -465,13 +467,24 @@ private void testDeactivationRequestByMsisdn(Long msisdn, int status) throws IOE assertTrue(SimpleHttpClient.execHttpRequest(httpRequest, status, RequestBuilder.ADMIN_USERNAME, RequestBuilder.ADMIN_PASSWORD)); } - //Test deactivation of specific msisdn - 5000000000L as IVR and 6000000000L as MCTS import + // Test audit trail of deactivation subscriptions + public void testDeactivationSubscriptionAuditService(Long msisdn, SubscriptionOrigin origin, int testNumber) { + List auditRecords = deactivationSubscriptionAuditRecordDataService.retrieveAll(); + assertEquals(testNumber, auditRecords.size()); + assertEquals(msisdn, auditRecords.get(testNumber-1).getMsisdn()); + assertEquals(origin, auditRecords.get(testNumber-1).getSubscriptionOrigin()); + assertEquals(AuditStatus.SUCCESS, auditRecords.get(testNumber-1).getAuditStatus()); + } + + //Test deactivation of specific msisdn - 5000000000L as IVR and 6000000000L as MCTS import @Test public void testDeactivateSpecificValidMsisdn() throws IOException, InterruptedException, URISyntaxException { createSubscriberHelper(); testDeactivationRequestByMsisdn(5000000000L, HttpStatus.SC_OK); testDeactivationRequestByMsisdn(5000000000L, HttpStatus.SC_OK); // Test deactivation of same number again + testDeactivationSubscriptionAuditService(5000000000L, SubscriptionOrigin.IVR, 1); testDeactivationRequestByMsisdn(6000000000L, HttpStatus.SC_OK); + testDeactivationSubscriptionAuditService(6000000000L, SubscriptionOrigin.MCTS_IMPORT, 2); testifAllSubscriberDectivated(); testReactivationDisabledAfterDeactivation(5000000000L); testReactivationDisabledAfterDeactivation(6000000000L);