From 568bc421edf5bf9d1d4da4e8cfb03b804601920f Mon Sep 17 00:00:00 2001 From: Boubaker Khanfir Date: Thu, 21 Dec 2023 18:06:23 +0100 Subject: [PATCH] feat: Cleanup Dependencies to rely on Spring Dependencies Tree - Meeds-io/MIPs#57 --- commons-api/pom.xml | 6 +- .../notification/channel/AbstractChannel.java | 2 +- .../notification/model/NotificationInfo.java | 2 +- .../commons/cluster/ClusterNodeName.java | 2 +- .../comparators/NaturalComparator.java | 2 +- .../exoplatform/commons/diff/DiffService.java | 2 +- .../notification/NotificationUtils.java | 6 +- .../impl/jpa/email/dao/MailDigestDAO.java | 2 +- .../impl/jpa/email/dao/MailNotifDAO.java | 2 +- .../impl/jpa/email/dao/MailParamDAO.java | 2 +- .../jpa/email/entity/MailDigestEntity.java | 22 +- .../jpa/email/entity/MailNotifEntity.java | 22 +- .../jpa/email/entity/MailParamEntity.java | 24 +- .../jpa/email/entity/MailQueueEntity.java | 2 +- .../jpa/web/JPAWebNotificationStorage.java | 2 +- .../impl/jpa/web/dao/WebUsersDAO.java | 6 +- .../impl/jpa/web/entity/WebNotifEntity.java | 22 +- .../impl/jpa/web/entity/WebParamsEntity.java | 2 +- .../impl/jpa/web/entity/WebUsersEntity.java | 2 +- .../impl/service/NotificationServiceImpl.java | 2 +- .../service/WebNotificationServiceImpl.java | 2 +- .../cache/CachedWebNotificationStorage.java | 2 +- .../setting/NotificationPluginContainer.java | 2 +- .../notification/template/GroovyElement.java | 8 +- .../template/TemplateResourceBundle.java | 2 +- .../notification/template/TemplateUtils.java | 9 +- .../commons/utils/StringCommonUtils.java | 2 +- .../commons/utils/StringListConverter.java | 4 +- .../settings/impl/ExoFeatureServiceImpl.java | 2 +- .../jpa/JPAUserSettingServiceImpl.java | 2 +- .../settings/rest/ExoFeatureRest.java | 2 +- .../impl/ExoTransactionalAnnotationTest.java | 2 +- .../commons/persistence/impl/Project.java | 10 +- .../commons/persistence/impl/Task.java | 12 +- .../commons/persistence/impl/TaskDao.java | 2 +- .../impl/TestEntityManagerService.java | 2 +- .../resource/ResourceBundleRESTTest.java | 2 +- .../email/dao/MailNotifDAOTest.java | 2 +- .../settings/impl/FeatureServiceTest.java | 2 +- commons-component-product/pom.xml | 2 +- .../PlatformInformationRESTServiceTEST.java | 2 +- commons-component-upgrade/pom.xml | 2 +- .../commons/upgrade/UpgradeProductPlugin.java | 2 +- .../upgrade/UpgradeProductService.java | 2 +- .../commons/upgrade/UpgradeProductTest.java | 2 +- commons-extension-webapp/pom.xml | 4 - commons-search/pom.xml | 2 +- .../commons/search/domain/Document.java | 2 +- .../search/domain/IndexingOperation.java | 2 +- .../search/es/client/ElasticClient.java | 89 +++--- .../es/client/ElasticIndexingAuditTrail.java | 2 +- .../es/client/ElasticIndexingClient.java | 14 +- .../es/client/ElasticSearchingClient.java | 6 +- .../ElasticIndexingOperationProcessor.java | 2 +- .../impl/ElasticIndexingServiceConnector.java | 2 +- .../index/impl/QueueIndexingService.java | 2 +- .../es/client/ElasticIndexingClientTest.java | 277 ------------------ .../es/client/ElasticSearchingClientTest.java | 249 ---------------- commons-testing/pom.xml | 2 +- .../mock/SimpleMockOrganizationService.java | 2 +- 60 files changed, 168 insertions(+), 706 deletions(-) delete mode 100644 commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticIndexingClientTest.java delete mode 100644 commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticSearchingClientTest.java diff --git a/commons-api/pom.xml b/commons-api/pom.xml index f4d0d43355..203c87ab1e 100644 --- a/commons-api/pom.xml +++ b/commons-api/pom.xml @@ -29,7 +29,7 @@ jar eXo PLF:: Commons - API - 0.12 + 0.01 diff --git a/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductPlugin.java b/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductPlugin.java index b15877621d..53223c55fd 100644 --- a/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductPlugin.java +++ b/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductPlugin.java @@ -1,6 +1,6 @@ package org.exoplatform.commons.upgrade; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.api.settings.SettingService; import org.exoplatform.commons.api.settings.SettingValue; diff --git a/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductService.java b/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductService.java index dec2f465d1..ec5d5deaab 100644 --- a/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductService.java +++ b/commons-component-upgrade/src/main/java/org/exoplatform/commons/upgrade/UpgradeProductService.java @@ -6,7 +6,7 @@ import jakarta.servlet.ServletContext; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.api.settings.SettingService; import org.exoplatform.commons.api.settings.SettingValue; diff --git a/commons-component-upgrade/src/test/java/org/exoplatform/commons/upgrade/UpgradeProductTest.java b/commons-component-upgrade/src/test/java/org/exoplatform/commons/upgrade/UpgradeProductTest.java index 28364bf5b2..15b3435486 100644 --- a/commons-component-upgrade/src/test/java/org/exoplatform/commons/upgrade/UpgradeProductTest.java +++ b/commons-component-upgrade/src/test/java/org/exoplatform/commons/upgrade/UpgradeProductTest.java @@ -19,7 +19,7 @@ import java.util.Properties; import java.util.concurrent.atomic.AtomicLong; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.api.settings.SettingService; import org.exoplatform.commons.info.MissingProductInformationException; import org.exoplatform.commons.info.ProductInformations; diff --git a/commons-extension-webapp/pom.xml b/commons-extension-webapp/pom.xml index c3f4d5cf12..ddea017413 100644 --- a/commons-extension-webapp/pom.xml +++ b/commons-extension-webapp/pom.xml @@ -32,10 +32,6 @@ commons-extension - - org.apache.maven.plugins - maven-war-plugin - org.codehaus.mojo build-helper-maven-plugin diff --git a/commons-search/pom.xml b/commons-search/pom.xml index ae72af4450..0d383645c9 100644 --- a/commons-search/pom.xml +++ b/commons-search/pom.xml @@ -29,7 +29,7 @@ jar eXo PLF:: Commons - Commons Search - 0.47 + 0.33 1.3 diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/domain/Document.java b/commons-search/src/main/java/org/exoplatform/commons/search/domain/Document.java index 5551e510f6..139e044cab 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/domain/Document.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/domain/Document.java @@ -20,7 +20,7 @@ package org.exoplatform.commons.search.domain; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.json.simple.JSONArray; import org.json.simple.JSONObject; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/domain/IndexingOperation.java b/commons-search/src/main/java/org/exoplatform/commons/search/domain/IndexingOperation.java index 918688e80f..b695b6849c 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/domain/IndexingOperation.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/domain/IndexingOperation.java @@ -18,7 +18,7 @@ import org.exoplatform.commons.api.persistence.ExoEntity; -import javax.persistence.*; +import jakarta.persistence.*; import java.io.Serializable; import java.util.Date; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticClient.java b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticClient.java index c13f8c82a2..0a52b582d7 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticClient.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticClient.java @@ -17,26 +17,28 @@ package org.exoplatform.commons.search.es.client; import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.io.IOUtils; -import org.apache.commons.lang.StringUtils; -import org.apache.http.HttpResponse; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.CredentialsProvider; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.*; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.entity.ContentType; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.DefaultConnectionReuseStrategy; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.HttpClientBuilder; -import org.apache.http.impl.client.HttpClients; +import org.apache.commons.lang3.StringUtils; +import org.apache.hc.client5.http.auth.AuthScope; +import org.apache.hc.client5.http.auth.UsernamePasswordCredentials; +import org.apache.hc.client5.http.classic.HttpClient; +import org.apache.hc.client5.http.classic.methods.HttpDelete; +import org.apache.hc.client5.http.classic.methods.HttpGet; +import org.apache.hc.client5.http.classic.methods.HttpHead; +import org.apache.hc.client5.http.classic.methods.HttpPost; +import org.apache.hc.client5.http.classic.methods.HttpPut; +import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider; +import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.apache.hc.core5.http.ClassicHttpResponse; +import org.apache.hc.core5.http.ContentType; +import org.apache.hc.core5.http.HttpEntity; +import org.apache.hc.core5.http.ParseException; +import org.apache.hc.core5.http.impl.DefaultConnectionReuseStrategy; +import org.apache.hc.core5.http.io.entity.EntityUtils; +import org.apache.hc.core5.http.io.entity.StringEntity; + +import org.exoplatform.commons.search.es.ElasticSearchException; import org.exoplatform.services.log.ExoLogger; import org.exoplatform.services.log.Log; @@ -72,7 +74,7 @@ protected ElasticResponse sendHttpPostRequest(String url, String content) { if(StringUtils.isNotBlank(content)) { httpTypeRequest.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON)); } - response = handleHttpResponse(client.execute(httpTypeRequest)); + response = client.execute(httpTypeRequest, this::handleHttpResponse); LOG.debug("Sent request to ES:\n Method = POST \nURI = {} \nContent = {}", url, content); logResultDependingOnStatusCode(url, response); } catch (IOException e) { @@ -88,7 +90,7 @@ protected ElasticResponse sendHttpPutRequest(String url, String content) { if(StringUtils.isNotBlank(content)) { httpTypeRequest.setEntity(new StringEntity(content, ContentType.APPLICATION_JSON)); } - response = handleHttpResponse(client.execute(httpTypeRequest)); + response = client.execute(httpTypeRequest, this::handleHttpResponse); LOG.debug("Sent request to ES:\n Method = PUT \nURI = '{}' \nContent = '{}'", url, content); logResultDependingOnStatusCode(url, response); } catch (IOException e) { @@ -102,7 +104,7 @@ protected ElasticResponse sendHttpDeleteRequest(String url) { try { HttpDelete httpDeleteRequest = new HttpDelete(url); - response = handleHttpResponse(client.execute(httpDeleteRequest)); + response = client.execute(httpDeleteRequest, this::handleHttpResponse); LOG.debug("Sent request to ES:\n Method = DELETE \nURI = {}", url); logResultDependingOnStatusCode(url, response); } catch (IOException e) { @@ -116,7 +118,7 @@ protected ElasticResponse sendHttpGetRequest(String url) { try { HttpGet httpGetRequest = new HttpGet(url); - response = handleHttpResponse(client.execute(httpGetRequest)); + response = client.execute(httpGetRequest, this::handleHttpResponse); LOG.debug("Sent request to ES:\n Method = GET \nURI = {}", url); } catch (IOException e) { throw new ElasticClientException(e); @@ -130,7 +132,7 @@ protected ElasticResponse sendHttpHeadRequest(String url) { try { HttpHead httpHeadRequest = new HttpHead(url); - response = handleHttpResponse(client.execute(httpHeadRequest)); + response = client.execute(httpHeadRequest, this::handleHttpResponse); LOG.debug("Sent request to ES:\n Method = HEAD \nURI = {}", url); } catch (IOException e) { throw new ElasticClientException(e); @@ -145,16 +147,16 @@ protected void initHttpClient() { protected HttpClient getHttpClient() { // Check if Basic Authentication need to be used HttpClientConnectionManager clientConnectionManager = getClientConnectionManager(); - HttpClientBuilder httpClientBuilder = HttpClients.custom() + HttpClientBuilder httpClientBuilder = HttpClientBuilder + .create().disableAutomaticRetries() .setConnectionManager(clientConnectionManager) - .setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()) - .setMaxConnPerRoute(getMaxConnections()); + .setConnectionReuseStrategy(new DefaultConnectionReuseStrategy()); if (StringUtils.isNotBlank(getEsUsernameProperty())) { - CredentialsProvider credsProvider = new BasicCredentialsProvider(); + BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( - new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), + new AuthScope(null, -1), new UsernamePasswordCredentials(getEsUsernameProperty(), - getEsPasswordProperty())); + getEsPasswordProperty().toCharArray())); HttpClient httpClient = httpClientBuilder .setDefaultCredentialsProvider(credsProvider) @@ -174,26 +176,15 @@ protected HttpClient getHttpClient() { * * @param httpResponse The Http Response to handle */ - private ElasticResponse handleHttpResponse(HttpResponse httpResponse) throws IOException { - String response = null; - InputStream is = null; - - if (httpResponse.getEntity()!=null) { - try { - is = httpResponse.getEntity().getContent(); - response = IOUtils.toString(is, StandardCharsets.UTF_8); - } finally { - if (is != null) { - is.close(); - } - } - } - - int statusCode = httpResponse.getStatusLine().getStatusCode(); - if (statusCode == HttpStatus.SC_UNAUTHORIZED) { - throw new ElasticClientAuthenticationException(); + private ElasticResponse handleHttpResponse(ClassicHttpResponse httpResponse) throws IOException { + final HttpEntity entity = httpResponse.getEntity(); + int statusCode = httpResponse.getCode(); + try { + return new ElasticResponse(EntityUtils.toString(entity), statusCode); + } catch (ParseException e) { + throw new ElasticSearchException("Error while parsing http response with code " + statusCode + " and response " + entity, + e); } - return new ElasticResponse(response, statusCode); } private void logResultDependingOnStatusCode(String url, ElasticResponse response) { diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingAuditTrail.java b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingAuditTrail.java index fb821520fb..07d3240469 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingAuditTrail.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingAuditTrail.java @@ -1,6 +1,6 @@ package org.exoplatform.commons.search.es.client; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.utils.PropertyManager; import org.exoplatform.services.log.ExoLogger; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingClient.java b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingClient.java index b6475e0a6b..9f13192045 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingClient.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticIndexingClient.java @@ -16,13 +16,17 @@ */ package org.exoplatform.commons.search.es.client; -import java.util.*; +import java.util.Collections; +import java.util.Map; +import java.util.Set; import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.lang.StringUtils; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.json.simple.*; +import org.apache.commons.lang3.StringUtils; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.JSONValue; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticSearchingClient.java b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticSearchingClient.java index 71336c2c58..ec89cc169b 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticSearchingClient.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/es/client/ElasticSearchingClient.java @@ -2,9 +2,9 @@ import java.util.Map; -import org.apache.commons.lang.StringUtils; -import org.apache.http.conn.HttpClientConnectionManager; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.commons.lang3.StringUtils; +import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager; +import org.apache.hc.client5.http.io.HttpClientConnectionManager; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingOperationProcessor.java b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingOperationProcessor.java index 54e71b19e4..b8b4919da0 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingOperationProcessor.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingOperationProcessor.java @@ -20,7 +20,7 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.picocontainer.Startable; import org.exoplatform.commons.api.persistence.ExoTransactional; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingServiceConnector.java b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingServiceConnector.java index ae14611539..8a55f621a4 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingServiceConnector.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/ElasticIndexingServiceConnector.java @@ -18,7 +18,7 @@ import java.io.InputStream; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.json.simple.JSONObject; import org.exoplatform.commons.search.index.IndexingServiceConnector; diff --git a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/QueueIndexingService.java b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/QueueIndexingService.java index 133f936a05..f3cffe698b 100644 --- a/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/QueueIndexingService.java +++ b/commons-search/src/main/java/org/exoplatform/commons/search/index/impl/QueueIndexingService.java @@ -2,7 +2,7 @@ import java.util.List; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.search.dao.IndexingOperationDAO; import org.exoplatform.commons.search.domain.IndexingOperation; diff --git a/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticIndexingClientTest.java b/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticIndexingClientTest.java deleted file mode 100644 index 1716dba681..0000000000 --- a/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticIndexingClientTest.java +++ /dev/null @@ -1,277 +0,0 @@ -/* -* Copyright (C) 2003-2015 eXo Platform SAS. -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Lesser General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this program. If not, see http://www.gnu.org/licenses/ . -*/ -package org.exoplatform.commons.search.es.client; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.*; - -import java.io.IOException; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.*; - -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.*; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.runners.MockitoJUnitRunner; - -import org.exoplatform.commons.utils.PropertyManager; -import org.mockito.stubbing.Answer; - -/** - * Created by The eXo Platform SAS - * Author : Thibault Clement - * tclement@exoplatform.com - * 9/1/15 - */ -@RunWith(MockitoJUnitRunner.class) -public class ElasticIndexingClientTest { - private ElasticIndexingClient elasticIndexingClient; - @Mock - private HttpClient httpClient; - @Mock - private ElasticIndexingAuditTrail auditTrail; - @Captor - private ArgumentCaptor httpPutRequestCaptor; - @Captor - private ArgumentCaptor httpPostRequestCaptor; - @Captor - private ArgumentCaptor httpDeleteRequestCaptor; - @Captor - private ArgumentCaptor httpGetRequestCaptor; - - - @Before - public void initMock() throws IOException { - MockitoAnnotations.initMocks(this); - PropertyManager.setProperty("exo.es.index.server.url", "http://127.0.0.1:9200"); - elasticIndexingClient = new ElasticIndexingClient(auditTrail); - elasticIndexingClient.client = httpClient; - elasticIndexingClient.resetMaxConnections(); - } - - @Test - public void sendCreateIndexRequest_IfCreateNewIndex_requestShouldBeSentToElastic() throws IOException { - //Given - initClientMock(404, "Not Found", 200, "Success"); - //When - elasticIndexingClient.sendCreateIndexRequest("index", "fakeSettings", "fakeMappings"); - //Then - verify(httpClient, times(2)).execute(any(HttpRequestBase.class)); - } - - @Test - public void sendCUDRequest_IfCUDOperation_bulkRequestShouldBeSentToElastic() throws IOException { - //Given - String response = "{\"took\":15," + - "\"errors\":true," + - "\"items\":[" + - "{\"index\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_version\":3,\"status\":200}}" + - "]}"; - initClientMock(999, "", 200, response); - //When - elasticIndexingClient.sendCUDRequest("FakeBulkRequest"); - //Then - verify(httpClient).execute(httpPostRequestCaptor.capture()); - assertEquals("http://127.0.0.1:9200/_bulk", httpPostRequestCaptor.getValue().getURI().toString()); - assertEquals("FakeBulkRequest", IOUtils.toString(httpPostRequestCaptor.getValue().getEntity().getContent())); - - } - - @Test - public void sendBulkRequest_forEveryDocument_callAuditTrail() throws IOException { - //Given - String response = "{\"took\":15," + - "\"errors\":true," + - "\"items\":[" + - "{\"index\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_version\":3,\"status\":200}}," + - "{\"delete\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"2\",\"_version\":1,\"status\":404,\"found\":false}}," + - "{\"create\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"3\",\"status\":409,\"error\":{\"reason\":\"DocumentAlreadyExistsException[[test][4] [type1][3]: document already exists]\"}}}," + - "{\"update\":{\"_index\":\"index1\",\"_type\":\"type1\",\"_id\":\"1\",\"status\":404,\"error\":{\"reason\":\"DocumentMissingException[[index1][-1] [type1][1]: document missing]\"}}}" + - "]}"; - initClientMock(999, "", 200, response); - when(auditTrail.isFullLogEnabled()).thenReturn(true); - //When - elasticIndexingClient.sendCUDRequest("myBulk"); - //Then - verify(auditTrail).isFullLogEnabled(); - verify(auditTrail).logAcceptedBulkOperation(eq("index"), eq("1"), eq("test"), eq(HttpStatus.SC_OK), isNull(String.class), anyLong()); - verify(auditTrail).logRejectedDocumentBulkOperation(eq("delete"), eq("2"), eq("test"), eq(HttpStatus.SC_NOT_FOUND), isNull(String.class), anyLong()); - verify(auditTrail).logRejectedDocumentBulkOperation(eq("create"), eq("3"), eq("test"), eq(HttpStatus.SC_CONFLICT), eq("DocumentAlreadyExistsException[[test][4] [type1][3]: document already exists]"), anyLong()); - verify(auditTrail).logRejectedDocumentBulkOperation(eq("update"), eq("1"), eq("index1"), eq(HttpStatus.SC_NOT_FOUND), eq("DocumentMissingException[[index1][-1] [type1][1]: document missing]"), anyLong()); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void createIndex_callAuditTrail() throws IOException { - //Given - String response = "{\"error\":\"IndexAlreadyExistsException[[profile] already exists]\",\"status\":400}"; - initClientMock(404, "Not Found", 400, response); - //When - elasticIndexingClient.sendCreateIndexRequest("profile", "mySettings", "someMappings"); - //Then - verify(auditTrail).audit(eq("create_index"), isNull(String.class), eq("profile"), eq(HttpStatus.SC_BAD_REQUEST), eq("{\"error\":\"IndexAlreadyExistsException[[profile] already exists]\",\"status\":400}"), anyLong()); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void sendBulkRequest_fullLogNotEnabled_auditTrailNotCalled() throws IOException { - //Given - String response = "{\"took\":15," + - "\"errors\":false," + - "\"items\":[" + - "{\"index\":{\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_version\":3,\"status\":200}}" + - "]}"; - initClientMock(999, "", 200, response); - when(auditTrail.isFullLogEnabled()).thenReturn(false); - //When - elasticIndexingClient.sendCUDRequest("myBulk"); - //Then - verify(auditTrail).isFullLogEnabled(); - verifyNoMoreInteractions(auditTrail); - } - - @Test(expected = ElasticClientAuthenticationException.class) - public void createType_notAuthenticated_throwsException() throws IOException { - //Given - initClientMock(999, "", 401, "Authentication Required"); - //When - elasticIndexingClient.sendCUDRequest("myBulk"); - //Then - fail("ElasticClientAuthenticationException expected"); - } - - @Test - public void createIndex_indexAlreadyExists_noRequestIsSentToES() throws IOException { - //Given - initClientMock(200, "{my existing Mapping}", 999, ""); - //When - elasticIndexingClient.sendCreateIndexRequest("myIndex", "mySettings", "someMappings"); - //Then - verify(httpClient, times(1)).execute(httpGetRequestCaptor.capture()); - assertEquals("http://127.0.0.1:9200/myIndex", httpGetRequestCaptor.getValue().getURI().toString()); - verifyNoMoreInteractions(httpClient); - } - - @Test - public void createIndex_indexNotExists_requestIsSentToES() throws IOException { - //Given - initClientMock(404, ElasticIndexingClient.EMPTY_JSON, 200, "Success"); - //When - elasticIndexingClient.sendCreateIndexRequest("myIndex", "mySettings", "someMappings"); - //Then - verify(httpClient, times(2)).execute(httpPutRequestCaptor.capture()); - assertEquals("http://127.0.0.1:9200/myIndex", httpPutRequestCaptor.getValue().getURI().toString()); - assertEquals("{\"settings\": mySettings, \"mappings\": someMappings}", IOUtils.toString(httpPutRequestCaptor.getValue().getEntity().getContent())); - verifyNoMoreInteractions(httpClient); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenNoConfiguration() { - //Given - PropertyManager.setProperty("exo.es.index.http.connections.max", ""); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticIndexingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenConfiguredValueIsNotANumber() { - //Given - PropertyManager.setProperty("exo.es.index.http.connections.max", "string"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticIndexingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.index.http.connections.max", ""); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenConfiguredValueIsNotAPositiveNumber() { - //Given - PropertyManager.setProperty("exo.es.index.http.connections.max", "0"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticIndexingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.index.http.connections.max", ""); - } - - @Test - public void shouldUseConfiguredHttpMaxConnectionsPerRouteWhenConfiguredValueIsAPositiveNumber() { - //Given - PropertyManager.setProperty("exo.es.index.http.connections.max", "10"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticIndexingClient.getClientConnectionManager(); - - //Then - assertEquals(10, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.index.http.connections.max", ""); - } - - - private void initClientMock(Integer getOrHeadStatus, String getOrHeadContent, Integer postStatus, String postContent) throws IOException { - // Get request - final HttpResponse getOrHeadResponse = mock(HttpResponse.class); - StatusLine getOrHeadStatusLine = mock(StatusLine.class); - HttpEntity getOrHeadHttpEntity = mock(HttpEntity.class); - when(getOrHeadResponse.getStatusLine()).thenReturn(getOrHeadStatusLine); - when(getOrHeadStatusLine.getStatusCode()).thenReturn(getOrHeadStatus); - when(getOrHeadResponse.getEntity()).thenReturn(getOrHeadHttpEntity); - when(getOrHeadHttpEntity.getContent()).thenReturn(IOUtils.toInputStream(getOrHeadContent, "UTF-8")); - // Post request - final HttpResponse postResponse = mock(HttpResponse.class); - StatusLine postStatusLine = mock(StatusLine.class); - HttpEntity postHttpEntity = mock(HttpEntity.class); - when(postResponse.getStatusLine()).thenReturn(postStatusLine); - when(postStatusLine.getStatusCode()).thenReturn(postStatus); - when(postResponse.getEntity()).thenReturn(postHttpEntity); - when(postHttpEntity.getContent()).thenReturn(IOUtils.toInputStream(postContent, "UTF-8")); - // Mock setting - when(httpClient.execute(any(HttpRequestBase.class))).thenAnswer(new Answer() { - @Override - public HttpResponse answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - if (args[0] instanceof HttpGet || args[0] instanceof HttpHead) { - return getOrHeadResponse; - } - return postResponse; - } - }); - } -} - diff --git a/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticSearchingClientTest.java b/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticSearchingClientTest.java deleted file mode 100644 index 1d395e7389..0000000000 --- a/commons-search/src/test/java/org/exoplatform/commons/search/es/client/ElasticSearchingClientTest.java +++ /dev/null @@ -1,249 +0,0 @@ -/* -* Copyright (C) 2003-2015 eXo Platform SAS. -* -* This program is free software: you can redistribute it and/or modify -* it under the terms of the GNU Lesser General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU Lesser General Public License for more details. -* -* You should have received a copy of the GNU Lesser General Public License -* along with this program. If not, see http://www.gnu.org/licenses/ . -*/ -package org.exoplatform.commons.search.es.client; - -import org.apache.commons.io.IOUtils; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; -import org.exoplatform.commons.search.es.client.ElasticIndexingAuditTrail; -import org.exoplatform.commons.search.es.client.ElasticSearchingClient; -import org.exoplatform.commons.utils.PropertyManager; - -import org.junit.*; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.junit.MockitoJUnit; -import org.mockito.junit.MockitoRule; -import org.mockito.quality.Strictness; -import org.mockito.runners.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; - -import java.io.IOException; - -import static org.junit.Assert.assertEquals; -import static org.mockito.Mockito.*; - -/** - * Created by The eXo Platform SAS - * Author : Thibault Clement - * tclement@exoplatform.com - * 9/1/15 - */ -@RunWith(MockitoJUnitRunner.class) -public class ElasticSearchingClientTest { - private ElasticSearchingClient elasticSearchingClient; - @Mock - private HttpClient httpClient; - @Mock - private ElasticIndexingAuditTrail auditTrail; - @Captor - private ArgumentCaptor httpPostRequestCaptor; - - @Before - public void initMock() throws IOException { - MockitoAnnotations.initMocks(this); - PropertyManager.setProperty("exo.es.search.server.url", "http://127.0.0.1:9200"); - elasticSearchingClient = new ElasticSearchingClient(auditTrail); - elasticSearchingClient.client = httpClient; - elasticSearchingClient.resetMaxConnections(); - } - - - - @Test - public void sendSearchRequest_successRequest_callAuditTrailWithSuccessMessage() throws IOException { - //Given - String response = "{\n" + - " \"took\":7,\n" + - " \"timed_out\":false,\n" + - " \"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\n" + - " \"hits\":{\n" + - " \"total\":2,\n" + - " \"max_score\":1.0,\n" + - " \"hits\":[\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value1\" }},\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"3\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value3\" }}\n" + - " ]\n" + - " }\n" + - "}"; - initClientMock(200, response); - when(auditTrail.isFullLogEnabled()).thenReturn(true); - //When - elasticSearchingClient.sendRequest("mySearch", "test"); - //Then - verify(auditTrail).isFullLogEnabled(); - verify(auditTrail).logAcceptedSearchOperation(eq("search_type"), eq("test"), eq(HttpStatus.SC_OK), eq(response), anyLong()); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void sendSearchRequest_errorRequest_callAuditTrailWithErrorMessage() throws IOException { - //Given - String response = "{\n" + - " \"took\":7,\n" + - " \"timed_out\":false,\n" + - " \"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\n" + - " \"hits\":{\n" + - " \"total\":2,\n" + - " \"max_score\":1.0,\n" + - " \"hits\":[\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value1\" }},\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"3\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value3\" }}\n" + - " ]\n" + - " }\n" + - "}"; - initClientMock(404, response); - lenient().when(auditTrail.isFullLogEnabled()).thenReturn(true); - //When - elasticSearchingClient.sendRequest("mySearch", "test"); - //Then - verify(auditTrail).logRejectedSearchOperation(eq("search_type"), eq("test"), eq(HttpStatus.SC_NOT_FOUND), anyString(), anyLong()); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void sendSearchRequest_fullLogNotEnabledAndSuccessRequest_auditTrailNotCalled() throws IOException { - //Given - String response = "{\n" + - " \"took\":7,\n" + - " \"timed_out\":false,\n" + - " \"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\n" + - " \"hits\":{\n" + - " \"total\":2,\n" + - " \"max_score\":1.0,\n" + - " \"hits\":[\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value1\" }},\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"3\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value3\" }}\n" + - " ]\n" + - " }\n" + - "}"; - initClientMock(200, response); - when(auditTrail.isFullLogEnabled()).thenReturn(false); - //When - elasticSearchingClient.sendRequest("mySearch", "test"); - //Then - verify(auditTrail).isFullLogEnabled(); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void sendSearchRequest_fullLogNotEnabledAndErrorRequest_auditTrailCalled() throws IOException { - //Given - String response = "{\n" + - " \"took\":7,\n" + - " \"timed_out\":false,\n" + - " \"_shards\":{\"total\":5,\"successful\":5,\"failed\":0},\n" + - " \"hits\":{\n" + - " \"total\":2,\n" + - " \"max_score\":1.0,\n" + - " \"hits\":[\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"1\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value1\" }},\n" + - " {\"_index\":\"test\",\"_type\":\"type1\",\"_id\":\"3\",\"_score\":1.0,\"_source\":{ \"field1\" : \"value3\" }}\n" + - " ]\n" + - " }\n" + - "}"; - initClientMock(404, response); - lenient().when(auditTrail.isFullLogEnabled()).thenReturn(false); - //When - elasticSearchingClient.sendRequest("mySearch", "test"); - //Then - verify(auditTrail).logRejectedSearchOperation(eq("search_type"), eq("test"), eq(HttpStatus.SC_NOT_FOUND), anyString(), anyLong()); - verifyNoMoreInteractions(auditTrail); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenNoConfiguration() { - //Given - PropertyManager.setProperty("exo.es.search.http.connections.max", ""); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticSearchingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenConfiguredValueIsNotANumber() { - //Given - PropertyManager.setProperty("exo.es.search.http.connections.max", "string"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticSearchingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.search.http.connections.max", ""); - } - - @Test - public void shouldUseDefaultHttpMaxConnectionsPerRouteWhenConfiguredValueIsNotAPositiveNumber() { - //Given - PropertyManager.setProperty("exo.es.search.http.connections.max", "0"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticSearchingClient.getClientConnectionManager(); - - //Then - assertEquals(ElasticClient.DEFAULT_MAX_HTTP_POOL_CONNECTIONS, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.search.http.connections.max", ""); - } - - @Test - public void shouldUseConfiguredHttpMaxConnectionsPerRouteWhenConfiguredValueIsAPositiveNumber() { - //Given - PropertyManager.setProperty("exo.es.search.http.connections.max", "10"); - - //When - PoolingHttpClientConnectionManager clientConnectionManager = (PoolingHttpClientConnectionManager) elasticSearchingClient.getClientConnectionManager(); - - //Then - assertEquals(10, clientConnectionManager.getDefaultMaxPerRoute()); - - PropertyManager.setProperty("exo.es.search.http.connections.max", ""); - } - - private void initClientMock(Integer postStatus, String postContent) throws IOException { - // Post request - final HttpResponse postResponse = mock(HttpResponse.class); - StatusLine postStatusLine = mock(StatusLine.class); - HttpEntity postHttpEntity = mock(HttpEntity.class); - when(postResponse.getStatusLine()).thenReturn(postStatusLine); - when(postStatusLine.getStatusCode()).thenReturn(postStatus); - when(postResponse.getEntity()).thenReturn(postHttpEntity); - when(postHttpEntity.getContent()).thenReturn(IOUtils.toInputStream(postContent, "UTF-8")); - // Mock setting - when(httpClient.execute(any(HttpPost.class))).thenAnswer(new Answer() { - @Override - public HttpResponse answer(InvocationOnMock invocation) throws Throwable { - return postResponse; - } - }); - } -} - diff --git a/commons-testing/pom.xml b/commons-testing/pom.xml index 11ad8c522a..9db29eaf82 100644 --- a/commons-testing/pom.xml +++ b/commons-testing/pom.xml @@ -29,7 +29,7 @@ jar eXo PLF:: Commons - Testing - 0.28 + 0.31 diff --git a/commons-testing/src/main/java/org/exoplatform/commons/testing/mock/SimpleMockOrganizationService.java b/commons-testing/src/main/java/org/exoplatform/commons/testing/mock/SimpleMockOrganizationService.java index 88cd6ef824..b7853f4c69 100644 --- a/commons-testing/src/main/java/org/exoplatform/commons/testing/mock/SimpleMockOrganizationService.java +++ b/commons-testing/src/main/java/org/exoplatform/commons/testing/mock/SimpleMockOrganizationService.java @@ -12,7 +12,7 @@ import java.util.Iterator; import java.util.Set; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.exoplatform.commons.utils.ListAccess; import org.exoplatform.commons.utils.ListAccessImpl; import org.exoplatform.commons.utils.ObjectPageList;