From b6bb47de42e295a96c207082a599c8399c5f6e86 Mon Sep 17 00:00:00 2001 From: Rahul Ramesh Date: Tue, 21 Nov 2023 12:01:33 +0530 Subject: [PATCH] [Parvathy, Rahul] | Add. Replace OpenERPClient With XML Strategy Implementation Co-authored-by: Parvathy Babu --- .../openerp/client/AtomFeedClientHelper.java | 19 ++- .../openerp/client/FeedClientFactory.java | 33 ++-- .../OpenERPCustomerServiceEventWorker.java | 10 +- .../worker/OpenERPDrugServiceEventWorker.java | 10 +- ...OpenERPLabOrderTypeServiceEventWorker.java | 11 +- .../worker/OpenERPSaleOrderEventWorker.java | 11 +- .../worker/OpenERPSaleableResourceWorker.java | 10 +- .../worker/OpenElisSaleOrderEventWorker.java | 20 ++- .../feed/openerp/worker/WorkerFactory.java | 24 ++- .../src/main/resources/jobs-context.xml | 5 +- .../openerp/client/OpenERPCustomerFeedIT.java | 10 +- ...OpenERPCustomerServiceEventWorkerTest.java | 10 +- .../OpenERPDrugServiceEventWorkerTest.java | 10 +- ...ERPLabOrderTypeServiceEventWorkerTest.java | 16 +- .../worker/OpenERPSaleOrderEventWorkerIT.java | 20 +-- .../OpenERPSaleableResourceWorkerTest.java | 10 +- .../openerp/web/client/OpenERPClient.java | 152 ------------------ .../web/service/CustomerAccountService.java | 10 +- .../openerp/web/service/CustomerService.java | 18 +-- .../openerp/web/service/ProductService.java | 12 +- .../service/CustomerAccountServiceTest.java | 13 +- .../web/service/CustomerServiceTest.java | 12 +- 22 files changed, 141 insertions(+), 305 deletions(-) delete mode 100644 openerp-client/src/main/java/org/bahmni/openerp/web/client/OpenERPClient.java diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java index 39eea99d..4733f737 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/AtomFeedClientHelper.java @@ -4,7 +4,8 @@ import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.job.Jobs; import org.bahmni.feed.openerp.worker.WorkerFactory; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; +import org.bahmni.openerp.web.client.strategy.implementation.OpenERPXMLClient; import org.bahmni.webclients.ClientCookies; import org.ict4h.atomfeed.client.AtomFeedProperties; import org.ict4h.atomfeed.client.repository.AllFailedEvents; @@ -16,16 +17,16 @@ import org.ict4h.atomfeed.server.transaction.AtomFeedSpringTransactionSupport; public class AtomFeedClientHelper { - private OpenERPAtomFeedProperties atomFeedProperties; - private AtomFeedSpringTransactionSupport transactionManager; - private OpenERPClient openERPClient; + private final OpenERPAtomFeedProperties atomFeedProperties; + private final AtomFeedSpringTransactionSupport transactionManager; + private final OpenERPXMLClient openERPXMLClient; private FeedClientFactory feedClientFactory; - private WebClientProvider webClientProvider; + private final WebClientProvider webClientProvider; - public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPClient openERPClient) { + public AtomFeedClientHelper(OpenERPAtomFeedProperties atomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPXMLClient openERPXMLClient) { this.atomFeedProperties = atomFeedProperties; this.transactionManager = transactionManager; - this.openERPClient = openERPClient; + this.openERPXMLClient = openERPXMLClient; this.webClientProvider = new WebClientProvider(atomFeedProperties); } @@ -42,7 +43,9 @@ FeedClient getAtomFeedClient(Jobs jobName, FeedClientFactory feedClientFactory) AllFeeds allFeeds = getAllFeeds(atomFeedProperties, cookies); AllMarkers allMarkers = new AllMarkersJdbcImpl(transactionManager); AllFailedEvents allFailedEvents = new AllFailedEventsJdbcImpl(transactionManager); - return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPClient, allFeeds, allMarkers, allFailedEvents, jobName); + //TODO: Create a toggle between rest and xml client + OpenERPContext openERPContext = new OpenERPContext(openERPXMLClient); + return feedClientFactory.getFeedClient(atomFeedProperties, transactionManager, openERPContext, allFeeds, allMarkers, allFailedEvents, jobName); } static AllFeeds getAllFeeds(OpenERPAtomFeedProperties atomFeedProperties, ClientCookies cookies) { diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java index e8e40719..f7fbcfaa 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/client/FeedClientFactory.java @@ -7,7 +7,7 @@ import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.job.Jobs; import org.bahmni.feed.openerp.worker.WorkerFactory; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.ict4h.atomfeed.client.AtomFeedProperties; import org.ict4h.atomfeed.client.repository.AllFailedEvents; import org.ict4h.atomfeed.client.repository.AllFeeds; @@ -24,15 +24,14 @@ public class FeedClientFactory { - private WorkerFactory workerFactory; - private static Logger logger = LogManager.getLogger(FeedClientFactory.class); + private final WorkerFactory workerFactory; + private static final Logger logger = LogManager.getLogger(FeedClientFactory.class); public FeedClientFactory(WorkerFactory workerFactory) { this.workerFactory = workerFactory; } - public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, - OpenERPClient openERPClient, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName) { + public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedProperties, AtomFeedSpringTransactionSupport transactionManager, OpenERPContext openERPContext, AllFeeds allFeeds, AllMarkers allMarkers, AllFailedEvents allFailedEvents, Jobs jobName) { String feedUri = openERPAtomFeedProperties.getFeedUriForJob(jobName); if (StringUtils.isBlank(feedUri)) { String message = String.format("No feed-uri defined for Job [%s][%s]", jobName, jobName.getFeedUriRef()); @@ -42,19 +41,20 @@ public AtomFeedClient getFeedClient(OpenERPAtomFeedProperties openERPAtomFeedPro try { String urlPrefix = getURLPrefix(jobName,openERPAtomFeedProperties); - EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, openERPClient, - urlPrefix); + EventWorker eventWorker = workerFactory.getWorker(jobName, feedUri, openERPContext, urlPrefix); return new AtomFeedClient(allFeeds, allMarkers, allFailedEvents, atomFeedProperties(openERPAtomFeedProperties), transactionManager, new URI(feedUri), eventWorker) ; } catch (URISyntaxException e) { throw new RuntimeException("error for uri:" + feedUri, e); } } - static String getURLPrefix(Jobs jobName,OpenERPAtomFeedProperties atomFeedProperties) { + static String getURLPrefix(Jobs jobName, OpenERPAtomFeedProperties atomFeedProperties) { String endpointURI = getURIForJob(jobName,atomFeedProperties); try { - URL endpointUrl = new URL(endpointURI); - return String.format("%s://%s", endpointUrl.getProtocol(), endpointUrl.getAuthority()); + if(endpointURI != null && !endpointURI.isEmpty()){ + URL endpointUrl = new URL(endpointURI); + return String.format("%s://%s", endpointUrl.getProtocol(), endpointUrl.getAuthority()); + } else throw new RuntimeException("Endpoint URI is null or empty"); } catch (MalformedURLException e) { throw new RuntimeException("Is not a valid URI - " + endpointURI, e); } @@ -62,11 +62,12 @@ static String getURLPrefix(Jobs jobName,OpenERPAtomFeedProperties atomFeedProper private static String getURIForJob(Jobs jobName,OpenERPAtomFeedProperties atomFeedProperties){ switch (jobName){ - case CUSTOMER_FEED: return atomFeedProperties.getAuthenticationURI(); - case SALEORDER_FEED: return atomFeedProperties.getAuthenticationURI(); - case DRUG_FEED: return atomFeedProperties.getAuthenticationURI(); - case LAB_FEED: return atomFeedProperties.getAuthenticationURI(); - case SALEABLE_FEED: return atomFeedProperties.getAuthenticationURI(); + case CUSTOMER_FEED: + case SALEORDER_FEED: + case SALEABLE_FEED: + case DRUG_FEED: + case LAB_FEED: + return atomFeedProperties.getAuthenticationURI(); case REFERENCE_DATA_FEED: return atomFeedProperties.getReferenceDataEndpointURI(); case OPENELIS_SALEORDER_FEED: return atomFeedProperties.getOpenElisURI(); default: return null; @@ -79,8 +80,6 @@ static AtomFeedProperties atomFeedProperties(OpenERPAtomFeedProperties openERPAt atomFeedProperties.setConnectTimeout(openERPAtomFeedProperties.getConnectionTimeoutInMilliseconds()); atomFeedProperties.setReadTimeout(openERPAtomFeedProperties.getReplyTimeoutInMilliseconds()); atomFeedProperties.setMaxFailedEvents(openERPAtomFeedProperties.getMaxFailedEvents()); - return atomFeedProperties; } - } \ No newline at end of file diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java index 7071dd40..e5c45f76 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPCustomerServiceEventWorker.java @@ -10,7 +10,7 @@ import org.bahmni.feed.openerp.domain.OpenMRSPatientIdentifier; import org.bahmni.feed.openerp.domain.OpenMRSPerson; import org.bahmni.feed.openerp.domain.OpenMRSPersonAddress; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -22,16 +22,16 @@ import java.util.List; public class OpenERPCustomerServiceEventWorker implements EventWorker { - OpenERPClient openERPClient; + OpenERPContext openERPContext; private String feedUrl; private OpenMRSWebClient webClient; private String urlPrefix; private static Logger logger = LogManager.getLogger(OpenERPCustomerServiceEventWorker.class); - public OpenERPCustomerServiceEventWorker(String feedUrl, OpenERPClient openERPClient, OpenMRSWebClient webClient, String urlPrefix) { + public OpenERPCustomerServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; - this.openERPClient = openERPClient; + this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; } @@ -39,7 +39,7 @@ public OpenERPCustomerServiceEventWorker(String feedUrl, OpenERPClient openERPCl @Override public void process(Event event) { try { - openERPClient.execute(mapRequest(event)); + openERPContext.execute(mapRequest(event)); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java index 069df0a7..3ea2db41 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorker.java @@ -5,7 +5,7 @@ import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.domain.OpenMRSDrug; import org.bahmni.feed.openerp.client.OpenMRSWebClient; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -20,14 +20,14 @@ public class OpenERPDrugServiceEventWorker implements EventWorker { private static Logger logger = LogManager.getLogger(OpenERPDrugServiceEventWorker.class); - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; private String feedUrl; private OpenMRSWebClient webClient; private String urlPrefix; - public OpenERPDrugServiceEventWorker(String feedUrl, OpenERPClient openERPClient, OpenMRSWebClient openMRSWebClient, String urlPrefix) { - this.openERPClient = openERPClient; + public OpenERPDrugServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + this.openERPContext = openERPContext; this.feedUrl = feedUrl; this.webClient = openMRSWebClient; this.urlPrefix = urlPrefix; @@ -37,7 +37,7 @@ public OpenERPDrugServiceEventWorker(String feedUrl, OpenERPClient openERPClient public void process(Event event) { logger.debug("Processing the event [{}]", event.getContent()); try { - openERPClient.execute(mapRequest(event)); + openERPContext.execute(mapRequest(event)); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java index 91fe0578..a53efa60 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorker.java @@ -7,7 +7,7 @@ import org.bahmni.feed.openerp.domain.labOrderType.OpenMRSLabPanelEvent; import org.bahmni.feed.openerp.domain.labOrderType.OpenMRSLabTestEvent; import org.bahmni.feed.openerp.domain.labOrderType.OpenMRSRadiologyTestEvent; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.ict4h.atomfeed.client.domain.Event; import org.ict4h.atomfeed.client.service.EventWorker; @@ -21,19 +21,18 @@ public class OpenERPLabOrderTypeServiceEventWorker implements EventWorker { private static Logger logger = LogManager.getLogger(OpenERPLabOrderTypeServiceEventWorker.class); - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; private String feedUrl; private OpenMRSWebClient webClient; private String urlPrefix; private Map labOrderTypeEventMap = new HashMap<>(); - public OpenERPLabOrderTypeServiceEventWorker(String feedUrl, OpenERPClient openERPClient, OpenMRSWebClient openMRSWebClient, String urlPrefix) { - this.openERPClient = openERPClient; + public OpenERPLabOrderTypeServiceEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + this.openERPContext = openERPContext; this.feedUrl = feedUrl; this.webClient = openMRSWebClient; this.urlPrefix = urlPrefix; - labOrderTypeEventMap.put(OpenMRSRadiologyTestEvent.RADIOLOGY_TEST_EVENT_NAME, new OpenMRSRadiologyTestEvent()); labOrderTypeEventMap.put(OpenMRSLabTestEvent.LAB_TEST_EVENT_NAME, new OpenMRSLabTestEvent()); labOrderTypeEventMap.put(OpenMRSLabPanelEvent.LAB_PANEL_EVENT_NAME, new OpenMRSLabPanelEvent()); @@ -45,7 +44,7 @@ public void process(Event event) { try { OpenMRSLabOrderTypeEvent openMRSLabOrderTypeEvent = labOrderTypeEventMap.get(event.getTitle()); if(openMRSLabOrderTypeEvent == null) return ; - openERPClient.execute(mapRequest(event, openMRSLabOrderTypeEvent)); + openERPContext.execute(mapRequest(event, openMRSLabOrderTypeEvent)); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java index 23d0a612..e00b5baa 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorker.java @@ -7,7 +7,7 @@ import org.bahmni.feed.openerp.domain.encounter.MapERPOrders; import org.bahmni.feed.openerp.domain.encounter.OpenMRSEncounter; import org.bahmni.feed.openerp.domain.visit.OpenMRSVisit; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -17,7 +17,7 @@ import java.net.URI; public class OpenERPSaleOrderEventWorker implements EventWorker { - OpenERPClient openERPClient; + OpenERPContext openERPContext; private String feedUrl; private OpenMRSWebClient webClient; private String urlPrefix; @@ -25,9 +25,9 @@ public class OpenERPSaleOrderEventWorker implements EventWorker { private static Logger logger = LogManager.getLogger(OpenERPSaleOrderEventWorker.class); - public OpenERPSaleOrderEventWorker(String feedUrl, OpenERPClient openERPClient, OpenMRSWebClient webClient, String urlPrefix) { + public OpenERPSaleOrderEventWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; - this.openERPClient = openERPClient; + this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; } @@ -38,8 +38,7 @@ public void process(Event event) { OpenERPRequest openERPRequest = mapRequest(event); if (!openERPRequest.shouldERPConsumeEvent()) return; - - openERPClient.execute(openERPRequest); + openERPContext.execute(openERPRequest); } catch (Exception e) { throw new RuntimeException(e); } diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java index 66e991ce..2f4fae19 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorker.java @@ -5,7 +5,7 @@ import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.client.OpenMRSWebClient; import org.bahmni.feed.openerp.domain.OpenMRSResource; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -21,15 +21,15 @@ public class OpenERPSaleableResourceWorker implements EventWorker { public static final String ERP_EVENT_CATEGORY = "create.service.saleable"; public static final String SALEABLE_PROPERTY_NAME = "saleable"; public static final String PRODUCT_CATEGORY = "product_category"; - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; private String feedUrl; private OpenMRSWebClient openMRSWebClient; private String urlPrefix; private static Logger logger = LogManager.getLogger(OpenERPSaleableResourceWorker.class); - public OpenERPSaleableResourceWorker(String feedUrl, OpenERPClient openERPClient, OpenMRSWebClient openMRSWebClient, String urlPrefix) { - this.openERPClient = openERPClient; + public OpenERPSaleableResourceWorker(String feedUrl, OpenERPContext openERPContext, OpenMRSWebClient openMRSWebClient, String urlPrefix) { + this.openERPContext = openERPContext; this.feedUrl = feedUrl; this.openMRSWebClient = openMRSWebClient; this.urlPrefix = urlPrefix; @@ -44,7 +44,7 @@ public void process(Event event) { logger.info(String.format("Resource is not a saleable resource. Ignoring. Event [%s]", event.getId())); return; } - openERPClient.execute(mapToOpenERPRequest(event, resource)); + openERPContext.execute(mapToOpenERPRequest(event, resource)); } catch (Exception e) { logger.error(String.format("Error occurred while trying to process Saleable Event [%s]", event.getId()), e); throw new RuntimeException(String.format("Error occurred while trying to process Saleable Event [%s]", event.getId()), e); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java index 32cfa724..0e51383a 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/OpenElisSaleOrderEventWorker.java @@ -5,7 +5,7 @@ import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.client.OpenElisWebClient; import org.bahmni.feed.openerp.domain.openelis.OpenElisLabOrder; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.bahmni.openerp.web.service.ProductService; @@ -16,18 +16,17 @@ import java.net.URI; public class OpenElisSaleOrderEventWorker implements EventWorker { - private String feedUrl; - private OpenERPClient openERPClient; - private OpenElisWebClient webClient; - private String urlPrefix; + private final String feedUrl; + private final OpenERPContext openERPContext; + private final OpenElisWebClient webClient; + private final String urlPrefix; private ProductService productService; + private static final Logger logger = LogManager.getLogger(OpenElisSaleOrderEventWorker.class); - private static Logger logger = LogManager.getLogger(OpenElisSaleOrderEventWorker.class); - - public OpenElisSaleOrderEventWorker(String feedUrl, OpenERPClient openERPClient, OpenElisWebClient webClient, String urlPrefix) { + public OpenElisSaleOrderEventWorker(String feedUrl, OpenERPContext openERPContext, OpenElisWebClient webClient, String urlPrefix) { this.feedUrl = feedUrl; - this.openERPClient = openERPClient; + this.openERPContext = openERPContext; this.webClient = webClient; this.urlPrefix = urlPrefix; } @@ -39,8 +38,7 @@ public void process(Event event) { OpenERPRequest openERPRequest = mapRequest(event); if (!openERPRequest.shouldERPConsumeEvent()) return; - - openERPClient.execute(openERPRequest); + openERPContext.execute(openERPRequest); } catch (Exception e) { logger.error("Error processing openelis sale order event : {}", event.toString(), e); throw new RuntimeException(e); diff --git a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java index 397e6dce..b373bc45 100644 --- a/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java +++ b/openerp-atomfeed-service/src/main/java/org/bahmni/feed/openerp/worker/WorkerFactory.java @@ -2,33 +2,27 @@ import org.bahmni.feed.openerp.job.Jobs; import org.bahmni.feed.openerp.client.WebClientProvider; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.ict4h.atomfeed.client.service.EventWorker; public class WorkerFactory { - private WebClientProvider webClientProvider; + private final WebClientProvider webClientProvider; public WorkerFactory(WebClientProvider webClientProvider) { this.webClientProvider = webClientProvider; } - public EventWorker getWorker(Jobs jobName, String feedUrl, OpenERPClient openERPClient, - String urlPrefix) { - + public EventWorker getWorker(Jobs jobName, String feedUrl, OpenERPContext openERPContext, String urlPrefix) { switch (jobName){ - case CUSTOMER_FEED: return new OpenERPCustomerServiceEventWorker(feedUrl, openERPClient, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, openERPClient, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case OPENELIS_SALEORDER_FEED: return new OpenElisSaleOrderEventWorker(feedUrl, openERPClient, webClientProvider.openElisWebClient(), urlPrefix); - case DRUG_FEED: return new OpenERPDrugServiceEventWorker(feedUrl, openERPClient, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case LAB_FEED: return new OpenERPLabOrderTypeServiceEventWorker(feedUrl, openERPClient, webClientProvider.getOpenMRSWebClient(), urlPrefix); - case SALEABLE_FEED: return new OpenERPSaleableResourceWorker(feedUrl, openERPClient, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case CUSTOMER_FEED: return new OpenERPCustomerServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case SALEORDER_FEED: return new OpenERPSaleOrderEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case OPENELIS_SALEORDER_FEED: return new OpenElisSaleOrderEventWorker(feedUrl, openERPContext, webClientProvider.openElisWebClient(), urlPrefix); + case DRUG_FEED: return new OpenERPDrugServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case LAB_FEED: return new OpenERPLabOrderTypeServiceEventWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); + case SALEABLE_FEED: return new OpenERPSaleableResourceWorker(feedUrl, openERPContext, webClientProvider.getOpenMRSWebClient(), urlPrefix); } - throw new RuntimeException(String.format("No worker for %s", jobName)); } - - - } diff --git a/openerp-atomfeed-service/src/main/resources/jobs-context.xml b/openerp-atomfeed-service/src/main/resources/jobs-context.xml index 8f61af3e..f079463f 100644 --- a/openerp-atomfeed-service/src/main/resources/jobs-context.xml +++ b/openerp-atomfeed-service/src/main/resources/jobs-context.xml @@ -249,7 +249,7 @@ - + @@ -259,7 +259,4 @@ - - - diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java index 41feee4a..600f229b 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/client/OpenERPCustomerFeedIT.java @@ -5,13 +5,14 @@ import com.sun.syndication.feed.atom.Feed; import com.sun.syndication.feed.atom.Link; import com.sun.syndication.io.FeedException; +import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.job.Jobs; import org.bahmni.feed.openerp.job.OpenERPCustomerFeedJob; import org.bahmni.feed.openerp.job.SimpleFeedJob; import org.bahmni.feed.openerp.worker.OpenERPCustomerServiceEventWorker; import org.bahmni.feed.openerp.worker.WorkerFactory; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.webclients.openmrs.OpenMRSAuthenticationResponse; import org.bahmni.webclients.openmrs.OpenMRSAuthenticator; import org.ict4h.atomfeed.Configuration; @@ -48,7 +49,6 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.MockitoAnnotations.initMocks; @@ -73,7 +73,7 @@ public class OpenERPCustomerFeedIT { private AllFailedEvents allFailedEvents; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private WorkerFactory workerFactory; @@ -187,10 +187,10 @@ public void shouldCreateCustomerInOpenERP() throws URISyntaxException, FeedExcep OpenMRSAuthenticationResponse authenticationResponse = new OpenMRSAuthenticationResponse(); authenticationResponse.setAuthenticated(true); authenticationResponse.setSessionId("sessionIdValue"); -// when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); + when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); when(webClientProvider.getWebClient(any(Jobs.class))).thenReturn(webClient); - when(workerFactory.getWorker(Jobs.CUSTOMER_FEED, feedUrl,openERPClient, "http://mrs.auth.uri")).thenReturn(openERPCustomerServiceEventWorker); + when(workerFactory.getWorker(Jobs.CUSTOMER_FEED, feedUrl, openERPContext, "http://mrs.auth.uri")).thenReturn(openERPCustomerServiceEventWorker); AtomFeedClientHelper clientHelper = mock(AtomFeedClientHelper.class); AtomFeedClient atomFeedClient = new AtomFeedClient(allFeedsMock, allMarkersJdbc, allFailedEvents, FeedClientFactory.atomFeedProperties(atomFeedProperties), diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java index dc117e18..fa3fc111 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/event/OpenERPCustomerServiceEventWorkerTest.java @@ -4,7 +4,7 @@ import com.sun.syndication.feed.atom.Entry; import org.bahmni.feed.openerp.client.OpenMRSWebClient; import org.bahmni.feed.openerp.worker.OpenERPCustomerServiceEventWorker; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -24,13 +24,13 @@ import static org.mockito.Mockito.when; public class OpenERPCustomerServiceEventWorkerTest { - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; private OpenMRSWebClient mockWebClient; private String MRSURLPrefix; @Before public void setUp() throws Exception { - openERPClient = mock(OpenERPClient.class); + openERPContext = mock(OpenERPContext.class); mockWebClient = mock(OpenMRSWebClient.class); } @@ -38,7 +38,7 @@ public void setUp() throws Exception { public void shouldCallOpenERPClientWithRightParameters() throws FileNotFoundException { MRSURLPrefix = "urlPrefixTest"; OpenERPCustomerServiceEventWorker customerServiceEventWorker = - new OpenERPCustomerServiceEventWorker("www.openmrs.com", openERPClient, mockWebClient, MRSURLPrefix); + new OpenERPCustomerServiceEventWorker("www.openmrs.com", openERPContext, mockWebClient, MRSURLPrefix); InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("patientResource.xml"); String patientResource = new Scanner(resourceAsStream).useDelimiter("\\Z").next(); @@ -48,7 +48,7 @@ public void shouldCallOpenERPClientWithRightParameters() throws FileNotFoundExce customerServiceEventWorker.process(event); - verify(openERPClient).execute(createOpenERPRequest(event)); + verify(openERPContext).execute(createOpenERPRequest(event)); } diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java index 67aebb91..cd13c245 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPDrugServiceEventWorkerTest.java @@ -2,7 +2,7 @@ import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.client.OpenMRSWebClient; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -27,7 +27,7 @@ public class OpenERPDrugServiceEventWorkerTest { @Mock private OpenERPAtomFeedProperties atomFeedProperties; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private OpenMRSWebClient webClient; @@ -49,7 +49,7 @@ public void setUp(){ " \"name\": \"Paracetamol 250mg\"\n" + "}"; MockitoAnnotations.initMocks(this); - worker = new OpenERPDrugServiceEventWorker(feedUri,openERPClient,webClient,"http://prefix/"); + worker = new OpenERPDrugServiceEventWorker(feedUri,openERPContext,webClient,"http://prefix/"); } @@ -60,7 +60,7 @@ public void testProcess() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); @@ -87,7 +87,7 @@ public void testFailedEvent() throws IOException{ worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java index 68b92e33..de8bb7cd 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPLabOrderTypeServiceEventWorkerTest.java @@ -2,7 +2,7 @@ import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.client.OpenMRSWebClient; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -29,7 +29,7 @@ public class OpenERPLabOrderTypeServiceEventWorkerTest { @Mock private OpenERPAtomFeedProperties atomFeedProperties; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private OpenMRSWebClient webClient; @@ -84,7 +84,7 @@ public void setUp() { "}"; MockitoAnnotations.initMocks(this); - worker = new OpenERPLabOrderTypeServiceEventWorker(feedUri, openERPClient, webClient, "http://prefix/"); + worker = new OpenERPLabOrderTypeServiceEventWorker(feedUri, openERPContext, webClient, "http://prefix/"); when(webClient.get(URI.create("http://prefix/radiology"))).thenReturn(radiologyTestJson); when(webClient.get(URI.create("http://prefix/test"))).thenReturn(labTestJson); when(webClient.get(URI.create("http://prefix/panel"))).thenReturn(labPanelJson); @@ -97,7 +97,7 @@ public void shouldProcessRadiologyTestEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -117,7 +117,7 @@ public void shouldProcessLabTestEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -136,7 +136,7 @@ public void shouldProcessLabPanelEvent() throws Exception { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -155,7 +155,7 @@ public void testRadiologyFailedEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); @@ -169,7 +169,7 @@ public void testTestFailedEvent() throws IOException { worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List actualParameters = openERPRequest.getParameters(); diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java index 3bf8f351..41b6ab97 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleOrderEventWorkerIT.java @@ -3,7 +3,7 @@ import org.bahmni.feed.openerp.ObjectMapperRepository; import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.client.OpenMRSWebClient; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.webclients.openmrs.OpenMRSAuthenticationResponse; import org.bahmni.webclients.openmrs.OpenMRSAuthenticator; import org.ict4h.atomfeed.client.domain.Event; @@ -36,7 +36,7 @@ public class OpenERPSaleOrderEventWorkerIT { @Mock private OpenERPAtomFeedProperties atomFeedProperties; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private OpenMRSAuthenticator openMRSAuthenticator; @Mock @@ -79,15 +79,15 @@ public void create_SaleOrder_In_ERP_For_Lab_Order(){ OpenMRSAuthenticationResponse authenticationResponse = new OpenMRSAuthenticationResponse(); authenticationResponse.setAuthenticated(true); authenticationResponse.setSessionId("sessionIdValue"); -// when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); + when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); HashMap fieldMap = new HashMap(); fieldMap.put("uuid","12345"); - when(openERPClient.search(any(String.class), any(Vector.class))).thenReturn(new Object[]{12345}); - when(openERPClient.read(any(String.class),any(Vector.class), any(Vector.class))).thenReturn(new Object[]{fieldMap}); + when(openERPContext.search(any(String.class), any(Vector.class))).thenReturn(new Object[]{12345}); + when(openERPContext.read(any(String.class),any(Vector.class), any(Vector.class))).thenReturn(new Object[]{fieldMap}); String feedUrl = "http://xxxx/encounter/feed/2"; - OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl,openERPClient,webClient,"http://mrs.auth.uri"); + OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl,openERPContext,webClient,"http://mrs.auth.uri"); Event event = new Event("Test",feedUrl); eventWorker.process(event); } @@ -108,17 +108,17 @@ public void create_SaleOrder_For_Bed_In_ERP_When_Bed_Is_Assigned() { OpenMRSAuthenticationResponse authenticationResponse = new OpenMRSAuthenticationResponse(); authenticationResponse.setAuthenticated(true); authenticationResponse.setSessionId("sessionIdValue"); -// when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); + when(openMRSAuthenticator.authenticate("mrsuser", "mrspwd", ObjectMapperRepository.objectMapper)).thenReturn(authenticationResponse); HashMap fieldMap = new HashMap(); fieldMap.put("uuid","12345"); - when(openERPClient.search(any(String.class), any(Vector.class))).thenReturn(new Object[]{12345}); - when(openERPClient.read(any(String.class),any(Vector.class), any(Vector.class))).thenReturn(new Object[]{fieldMap}); + when(openERPContext.search(any(String.class), any(Vector.class))).thenReturn(new Object[]{12345}); + when(openERPContext.read(any(String.class),any(Vector.class), any(Vector.class))).thenReturn(new Object[]{fieldMap}); String feedUrl = "http://xxxx/encounter/feed/2"; Event event = new Event("Test", feedUrl); - OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl, openERPClient, webClient,"http://mrs.auth.uri"); + OpenERPSaleOrderEventWorker eventWorker = new OpenERPSaleOrderEventWorker(feedUrl, openERPContext, webClient,"http://mrs.auth.uri"); eventWorker.process(event); } diff --git a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java index 12a0e173..2ffc7ebc 100644 --- a/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java +++ b/openerp-atomfeed-service/src/test/java/org/bahmni/feed/openerp/worker/OpenERPSaleableResourceWorkerTest.java @@ -2,7 +2,7 @@ import org.bahmni.feed.openerp.OpenERPAtomFeedProperties; import org.bahmni.feed.openerp.client.OpenMRSWebClient; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.Parameter; import org.ict4h.atomfeed.client.domain.Event; @@ -26,7 +26,7 @@ public class OpenERPSaleableResourceWorkerTest { @Mock private OpenERPAtomFeedProperties atomFeedProperties; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private OpenMRSWebClient webClient; @@ -59,7 +59,7 @@ public class OpenERPSaleableResourceWorkerTest { @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - worker = new OpenERPSaleableResourceWorker(feedUri, openERPClient, webClient, "https://mybahmni/"); + worker = new OpenERPSaleableResourceWorker(feedUri, openERPContext, webClient, "https://mybahmni/"); when(webClient.get(URI.create(eventUrl))).thenReturn(sampleProcedureJson); } @@ -68,7 +68,7 @@ public void shouldProcessSaleableTypeEvent() throws Exception { Event event = new Event("1", eventContent, "Dressing", feedUri, new Date()); worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); OpenERPRequest openERPRequest = erpRequestCatcher.getValue(); List parameters = openERPRequest.getParameters(); @@ -90,7 +90,7 @@ public void shouldUpdateAsInactiveWhenNotSaleableEvenWhenMRSResourceIsActive() t Event event = new Event("1", eventContent, "Dressing", feedUri, new Date()); worker.process(event); ArgumentCaptor erpRequestCatcher = ArgumentCaptor.forClass(OpenERPRequest.class); - verify(openERPClient).execute(erpRequestCatcher.capture()); + verify(openERPContext).execute(erpRequestCatcher.capture()); List parameters = erpRequestCatcher.getValue().getParameters(); Assert.assertTrue(parameters.contains(new Parameter("name", "Dressing of Wound"))); diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/client/OpenERPClient.java b/openerp-client/src/main/java/org/bahmni/openerp/web/client/OpenERPClient.java deleted file mode 100644 index 4844eda7..00000000 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/client/OpenERPClient.java +++ /dev/null @@ -1,152 +0,0 @@ -package org.bahmni.openerp.web.client; - -import org.apache.xmlrpc.XmlRpcException; -import org.apache.xmlrpc.client.XmlRpcClient; -import org.apache.xmlrpc.client.XmlRpcClientConfigImpl; -import org.apache.xmlrpc.client.XmlRpcSun15HttpTransportFactory; -import org.bahmni.openerp.web.OpenERPException; -import org.bahmni.openerp.web.OpenERPProperties; -import org.bahmni.openerp.web.http.client.HttpClient; -import org.bahmni.openerp.web.request.OpenERPRequest; -import org.bahmni.openerp.web.request.builder.RequestBuilder; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Lazy; -import org.springframework.stereotype.Service; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Vector; - -@Service -@Lazy -public class OpenERPClient { - public static final String XML_RPC_OBJECT_ENDPOINT = "/xmlrpc/object"; - public static final String XML_RPC_COMMON_ENDPOINT = "/xmlrpc/common"; - - private final int connectionTimeoutInMilliseconds; - private final int replyTimeoutInMilliseconds; - private String host; - private int port; - private String database; - private String user; - private String password; - - private Object id; - - private XmlRpcClient xmlRpcClient; - private HttpClient httpClient; - - @Autowired - public OpenERPClient(HttpClient httpClient, OpenERPProperties openERPProperties) { - this.httpClient = httpClient; - host = openERPProperties.getHost(); - port = openERPProperties.getPort(); - database = openERPProperties.getDatabase(); - user = openERPProperties.getUser(); - password = openERPProperties.getPassword(); - connectionTimeoutInMilliseconds = openERPProperties.getConnectionTimeoutInMilliseconds(); - replyTimeoutInMilliseconds = openERPProperties.getReplyTimeoutInMilliseconds(); - } - - private Object executeRPC(XmlRpcClient loginRpcClient, Vector params, String methodName) { - try { - return loginRpcClient.execute(methodName, params); - } catch (XmlRpcException e) { - throw new OpenERPException(e); - } - } - - public Object search(String resource, Vector params) { - return execute(resource, "search", params); - } - - public Object read(String resource,Vector ids, Vector params) { - return executeRead(resource, "read", ids, params); - } - - public String execute(OpenERPRequest openERPRequest) { - login(); - String request = RequestBuilder.buildNewRequest(openERPRequest, id, database, password); - String response = httpClient().post("http://" + host + ":" + port + XML_RPC_OBJECT_ENDPOINT, request); - new OpenERPResponseErrorValidator().checkForError(response); - return response; - } - - private void login() { - if (id == null) { - XmlRpcClient loginRpcClient = xmlRpcClient(XML_RPC_COMMON_ENDPOINT); - - Vector params = new Vector(); - params.addElement(database); - params.addElement(user); - params.addElement(password); - - Object loginId = executeRPC(loginRpcClient, params, "login"); - if(loginId == null || loginId.getClass() != Integer.class) - throw new OpenERPException(String.format("Failed to login. The login id is : %s", loginId)); - id = loginId; - } - } - - public Object delete(String resource, Vector params) { - return execute(resource, "unlink", params); - } - - public Object execute(String resource, String operation, Vector params) { - login(); - Object args[] = {database, (Integer) id, password, resource, operation, params}; - - try { - return xmlRpcClient(XML_RPC_OBJECT_ENDPOINT).execute("execute", args); - } catch (XmlRpcException e) { - throw new OpenERPException(e); - } - } - - public Object executeRead(String resource, String operation,Vector ids, Vector params) { - login(); - Object args[] = {database, (Integer) id, password, resource, operation,ids, params}; - - try { - return xmlRpcClient(XML_RPC_OBJECT_ENDPOINT).execute("execute", args); - } catch (XmlRpcException e) { - throw new OpenERPException(e); - } - } - - public Object updateCustomerReceivables(String resource, Vector params) { - return execute(resource, "update_customer_receivables", params); - } - - private HttpClient httpClient() { - httpClient.setTimeout(replyTimeoutInMilliseconds); - return httpClient; - } - - private XmlRpcClient xmlRpcClient(String endpoint) { - if (xmlRpcClient == null) { - xmlRpcClient = createRPCClient(); - } - XmlRpcClientConfigImpl clientConfig = (XmlRpcClientConfigImpl) xmlRpcClient.getClientConfig(); - try { - clientConfig.setServerURL(new URL("http", host, port, endpoint)); - } catch (MalformedURLException e) { - throw new OpenERPException(e); - } - return xmlRpcClient; - } - - private XmlRpcClient createRPCClient() { - XmlRpcClientConfigImpl clientConfiguration = new XmlRpcClientConfigImpl(); - clientConfiguration.setEnabledForExtensions(true); - clientConfiguration.setEnabledForExceptions(true); - clientConfiguration.setConnectionTimeout(connectionTimeoutInMilliseconds); - clientConfiguration.setReplyTimeout(replyTimeoutInMilliseconds); - - XmlRpcClient rpcClient = new XmlRpcClient(); - rpcClient.setTransportFactory(new XmlRpcSun15HttpTransportFactory(rpcClient)); - rpcClient.setConfig(clientConfiguration); - return rpcClient; - } - -} diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerAccountService.java b/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerAccountService.java index d4b721a8..869d94cb 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerAccountService.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerAccountService.java @@ -3,7 +3,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.bahmni.openerp.web.OpenERPException; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -11,13 +11,13 @@ @Service public class CustomerAccountService { - OpenERPClient openERPClient; + OpenERPContext openERPContext; private static Logger logger = LogManager.getLogger(CustomerAccountService.class); @Autowired - public CustomerAccountService(OpenERPClient client) { - this.openERPClient = client; + public CustomerAccountService(OpenERPContext openERPContext) { + this.openERPContext = openERPContext; } public void updateCustomerReceivables(String patientId, double amount) { @@ -38,7 +38,7 @@ public boolean tryUpdateReceivables(String patientId, double amount){ params.addElement(args2); try { - openERPClient.updateCustomerReceivables("account.receivables", params); + openERPContext.updateCustomerReceivables("account.receivables", params); return true ; } catch (Exception exception) { logger.error(String.format("[%s] : Account Receivable update failed for amount of %s", patientId, amount), exception); diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerService.java b/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerService.java index 69a3ac91..b772d143 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerService.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/service/CustomerService.java @@ -1,7 +1,7 @@ package org.bahmni.openerp.web.service; import org.bahmni.openerp.web.OpenERPException; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.mapper.OpenERPCustomerParameterMapper; import org.bahmni.openerp.web.service.domain.Customer; @@ -12,24 +12,24 @@ @Service public class CustomerService { - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; private OpenERPCustomerParameterMapper parameterMapper; @Autowired - public CustomerService(OpenERPClient openERPClient) { - this.openERPClient = openERPClient; + public CustomerService(OpenERPContext openERPContext) { + this.openERPContext = openERPContext; this.parameterMapper = new OpenERPCustomerParameterMapper(); } - CustomerService(OpenERPClient openERPClient,OpenERPCustomerParameterMapper parameterMapper) { - this.openERPClient = openERPClient; + CustomerService(OpenERPContext openERPContext,OpenERPCustomerParameterMapper parameterMapper) { + this.openERPContext = openERPContext; this.parameterMapper = parameterMapper; } public void create(Customer customer) { if (noCustomersFound(findCustomersWithPatientReference(customer.getRef()))) { OpenERPRequest request = parameterMapper.mapCustomerParams(customer, "create"); - openERPClient.execute(request); + openERPContext.execute(request); } else throw new OpenERPException(String.format("Customer with id, name already exists: %s, %s ", customer.getRef(), customer.getName())); } @@ -38,14 +38,14 @@ public void deleteCustomerWithPatientReference(String patientId) { Object[] customerIds = findCustomersWithPatientReference(patientId); Vector params = new Vector(); params.addElement(customerIds[0]); - openERPClient.delete("res.partner", params); + openERPContext.delete("res.partner", params); } public Object[] findCustomersWithPatientReference(String patientId) { Object args[] = {"ref", "=", patientId}; Vector params = new Vector(); params.addElement(args); - return (Object[]) openERPClient.search("res.partner", params); + return (Object[]) openERPContext.search("res.partner", params); } private boolean noCustomersFound(Object[] customers) { diff --git a/openerp-client/src/main/java/org/bahmni/openerp/web/service/ProductService.java b/openerp-client/src/main/java/org/bahmni/openerp/web/service/ProductService.java index 67360a62..5a0eef58 100644 --- a/openerp-client/src/main/java/org/bahmni/openerp/web/service/ProductService.java +++ b/openerp-client/src/main/java/org/bahmni/openerp/web/service/ProductService.java @@ -1,6 +1,6 @@ package org.bahmni.openerp.web.service; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -9,18 +9,18 @@ @Service public class ProductService { - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Autowired - public ProductService(OpenERPClient openERPClient) { - this.openERPClient = openERPClient; + public ProductService(OpenERPContext openERPContext) { + this.openERPContext = openERPContext; } public String findProductByName(String name) { Object args[] = {"name", "=", name}; Vector params = new Vector(); params.addElement(args); - Object[] productIds = (Object[])openERPClient.search("product.product", params); + Object[] productIds = (Object[])openERPContext.search("product.product", params); Object[] productUUIDs = null; if(productIds.length > 0) { Integer prodId = (Integer) productIds[0]; @@ -28,7 +28,7 @@ public String findProductByName(String name) { ids.add(prodId); Vector paramFields = new Vector(); paramFields.add("uuid"); - productUUIDs = (Object[])openERPClient.read("product.product",ids, paramFields); + productUUIDs = (Object[])openERPContext.read("product.product",ids, paramFields); if(productUUIDs.length > 0){ Map productUUID = (Map) productUUIDs[0]; return (String) productUUID.get("uuid"); diff --git a/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerAccountServiceTest.java b/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerAccountServiceTest.java index 9adaa821..8a3ca78a 100644 --- a/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerAccountServiceTest.java +++ b/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerAccountServiceTest.java @@ -1,14 +1,13 @@ package org.bahmni.openerp.web.service; import org.bahmni.openerp.web.OpenERPException; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import java.util.Vector; -import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doThrow; @@ -17,17 +16,17 @@ public class CustomerAccountServiceTest { @Mock - OpenERPClient openERPClient; + OpenERPContext openERPContext; private CustomerAccountService customerAccountService; @Before public void setUp() { initMocks(this); - customerAccountService = new CustomerAccountService(openERPClient); + customerAccountService = new CustomerAccountService(openERPContext); } @Test - public void shouldUpdateCustomerReceivables() throws Exception { + public void shouldUpdateCustomerReceivables() { String patientId = "12345"; double amount = 27.0; @@ -39,14 +38,14 @@ public void shouldUpdateCustomerReceivables() throws Exception { customerAccountService.updateCustomerReceivables(patientId,amount); - verify(openERPClient).updateCustomerReceivables((String) any(),(Vector) any()); + verify(openERPContext).updateCustomerReceivables((String) any(),(Vector) any()); } @Test public void shouldThrowExceptionIfUpdationFails() { String patientId = "12345"; double amount = 27.0; - doThrow(new OpenERPException("message")).when(openERPClient).updateCustomerReceivables(anyString(), any(Vector.class)); + doThrow(new OpenERPException("message")).when(openERPContext).updateCustomerReceivables(anyString(), any(Vector.class)); try { customerAccountService.updateCustomerReceivables(patientId, amount); diff --git a/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerServiceTest.java b/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerServiceTest.java index 462ff0eb..66b2e961 100644 --- a/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerServiceTest.java +++ b/openerp-client/src/test/java/org/bahmni/openerp/web/service/CustomerServiceTest.java @@ -1,6 +1,6 @@ package org.bahmni.openerp.web.service; -import org.bahmni.openerp.web.client.OpenERPClient; +import org.bahmni.openerp.web.client.strategy.OpenERPContext; import org.bahmni.openerp.web.request.OpenERPRequest; import org.bahmni.openerp.web.request.builder.OpenERPRequestTestHelper; import org.bahmni.openerp.web.request.builder.Parameter; @@ -23,7 +23,7 @@ public class CustomerServiceTest { private CustomerService customerService; @Mock - private OpenERPClient openERPClient; + private OpenERPContext openERPContext; @Mock private OpenERPCustomerParameterMapper parameterMapper; @@ -32,7 +32,7 @@ public class CustomerServiceTest { @Before public void setup() { initMocks(this); - customerService = new CustomerService(openERPClient, parameterMapper); + customerService = new CustomerService(openERPContext, parameterMapper); openERPRequestTestHelper = new OpenERPRequestTestHelper(); } @@ -45,7 +45,7 @@ public void shouldCreateNewCustomerIfNotExisting() throws Exception { Vector searchparams = new Vector(); searchparams.addElement(new Object[]{"ref", "=", "12345"}); Object[] results = new Object[]{}; - when(openERPClient.search((String) any(), (Vector) any())).thenReturn(results); + when(openERPContext.search((String) any(), (Vector) any())).thenReturn(results); List parameters = openERPRequestTestHelper.createCustomerRequest(name,patientId,village); OpenERPRequest request = new OpenERPRequest("res_partner", "execute", parameters); @@ -54,7 +54,7 @@ public void shouldCreateNewCustomerIfNotExisting() throws Exception { customerService.create(customer); - verify(openERPClient).execute(request); + verify(openERPContext).execute(request); } @Test @@ -65,7 +65,7 @@ public void createCustomerShouldThrowExceptionIfCustomerAlreadyExisting() throws Vector searchparams = new Vector(); searchparams.addElement(new Object[]{"ref", "=", "12345"}); Object[] results = new Object[]{new Object()}; - when(openERPClient.search((String) any(), (Vector) any())).thenReturn(results); + when(openERPContext.search((String) any(), (Vector) any())).thenReturn(results); try { customerService.create(customer);