diff --git a/src/main/java/com/meilisearch/sdk/GenericServiceTemplate.java b/src/main/java/com/meilisearch/sdk/GenericServiceTemplate.java deleted file mode 100644 index 7ca05015..00000000 --- a/src/main/java/com/meilisearch/sdk/GenericServiceTemplate.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.meilisearch.sdk; - -import com.meilisearch.sdk.exceptions.APIError; -import com.meilisearch.sdk.exceptions.MeiliSearchApiException; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.HttpClient; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.http.response.HttpResponse; -import com.meilisearch.sdk.json.JsonHandler; -import lombok.Getter; - -@Getter -public class GenericServiceTemplate implements ServiceTemplate { - private final AbstractHttpClient client; - private final JsonHandler processor; - private final RequestFactory requestFactory; - - /** - * @param client a {@link HttpClient} - * @param processor a {@link JsonHandler} - * @param requestFactory a {@link RequestFactory} - */ - public GenericServiceTemplate( - AbstractHttpClient client, JsonHandler processor, RequestFactory requestFactory) { - this.client = client; - this.processor = processor; - this.requestFactory = requestFactory; - } - - /** {@inheritDoc} */ - @Override - @SuppressWarnings("unchecked") - public T execute(HttpRequest request, Class targetClass, Class... parameter) - throws RuntimeException { - if (targetClass == null) { - return (T) makeRequest(request); - } - HttpResponse response = makeRequest(request); - if (response.getStatusCode() >= 400) { - APIError error = decodeResponse(response.getContent(), APIError.class); - throw new MeiliSearchRuntimeException(new MeiliSearchApiException(error)); - } - - return decodeResponse(response.getContent(), targetClass, parameter); - } - - private T decodeResponse(Object o, Class targetClass, Class... parameters) { - try { - return processor.decode(o, targetClass, parameters); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * Executes the given {@link HttpRequest} - * - * @param request the {@link HttpRequest} - * @return the HttpResponse - * @throws MeiliSearchRuntimeException in case there are Problems with the Request, including - * error codes - */ - private HttpResponse makeRequest(HttpRequest request) { - try { - switch (request.getMethod()) { - case GET: - return client.get(request); - case POST: - return client.post(request); - case PUT: - return client.put(request); - case DELETE: - return client.delete(request); - default: - throw new IllegalStateException("Unexpected value: " + request.getMethod()); - } - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } -} diff --git a/src/main/java/com/meilisearch/sdk/ServiceTemplate.java b/src/main/java/com/meilisearch/sdk/ServiceTemplate.java deleted file mode 100644 index 1f9d777c..00000000 --- a/src/main/java/com/meilisearch/sdk/ServiceTemplate.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.meilisearch.sdk; - -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.json.JsonHandler; - -/** - * A ServiceTemplate combines the HttpClient implementation with the JsonProcessor implementation - * and specifies how both work together. - */ -public interface ServiceTemplate { - - /** @return the wrapped HttpClient implementation */ - AbstractHttpClient getClient(); - - /** @return the wrapped JsonProcessor implementation */ - JsonHandler getProcessor(); - - /** @return the wrapped JsonProcessor implementation */ - RequestFactory getRequestFactory(); - - /** - * Executes the given request and deserializes the response - * - * @param request the HttpRequest to execute - * @param targetClass the Type of Object to deserialize - * @param parameter in case targetClass is a generic, parameter contains the specific types for - * the generic - * @param type of targetClass or {@link com.meilisearch.sdk.http.response.HttpResponse} when - * targetClass is null - * @return the deserialized response of type targetClass or the {@link - * com.meilisearch.sdk.http.response.HttpResponse} if targetClass is null - * @throws MeiliSearchRuntimeException as a wrapper of API or JSON exceptions - */ - T execute(HttpRequest request, Class targetClass, Class... parameter) - throws MeiliSearchRuntimeException; -} diff --git a/src/main/java/com/meilisearch/sdk/api/documents/DocumentHandler.java b/src/main/java/com/meilisearch/sdk/api/documents/DocumentHandler.java deleted file mode 100644 index ef649be4..00000000 --- a/src/main/java/com/meilisearch/sdk/api/documents/DocumentHandler.java +++ /dev/null @@ -1,272 +0,0 @@ -package com.meilisearch.sdk.api.documents; - -import com.meilisearch.sdk.ServiceTemplate; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import java.util.Collections; -import java.util.List; - -public class DocumentHandler { - private final ServiceTemplate serviceTemplate; - private final RequestFactory requestFactory; - private final String indexName; - private final Class indexModel; - - public DocumentHandler( - ServiceTemplate serviceTemplate, - RequestFactory requestFactory, - String indexName, - Class indexModel) { - this.serviceTemplate = serviceTemplate; - this.requestFactory = requestFactory; - this.indexName = indexName; - this.indexModel = indexModel; - } - - /** - * Retrieve a document with a specific identifier - * - * @param identifier the identifier of the document you are looking for - * @return the document specified by the identifier - * @throws com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException in case something went - * wrong (http error, json exceptions, etc) - */ - public T getDocument(String identifier) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents/" + identifier; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - indexModel); - } - - /** - * Retrieve a list of documents - * - * @return a list of Documents from the index. - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public List getDocuments() throws MeiliSearchRuntimeException { - return getDocuments(0); - } - - /** - * Retrieve a list of documents - * - * @param limit maximum number of documents to be returned - * @return a list of Documents from the index. - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public List getDocuments(int limit) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents"; - if (limit > 0) { - requestQuery += "?limit=" + limit; - } - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - List.class, - indexModel); - } - - /** - * Add or replace a document - * - * @param data an already serialized document - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task addDocuments(String data) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.POST, requestQuery, Collections.emptyMap(), data), - Task.class); - } - - /** - * Add or replace a batch of documents - * - * @param data a list of document objects - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task addDocuments(List data) throws MeiliSearchRuntimeException { - try { - String dataString = serviceTemplate.getProcessor().encode(data); - return addDocuments(dataString); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * Add or replace a document - * - * @param data the serialized document - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task replaceDocuments(String data) throws MeiliSearchRuntimeException { - return addDocuments(data); - } - - /** - * Add or replace a batch of documents - * - * @param data a list of document objects - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task replaceDocuments(List data) throws MeiliSearchRuntimeException { - try { - String dataString = serviceTemplate.getProcessor().encode(data); - return replaceDocuments(dataString); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * Add or update a document - * - * @param data the serialized document - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task updateDocuments(String data) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.PUT, requestQuery, Collections.emptyMap(), data), - Task.class); - } - - /** - * Add or update a document - * - * @param data a list of document objects - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task updateDocuments(List data) throws MeiliSearchRuntimeException { - try { - String dataString = serviceTemplate.getProcessor().encode(data); - return updateDocuments(dataString); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * Delete a document with a specific identifier - * - * @param identifier the id of the document - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task deleteDocument(String identifier) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents/" + identifier; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Delete a batch of documents - * - * @return an Task object with the taskId - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task deleteDocuments() throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/documents"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * @param q the Querystring - * @return a SearchResponse with the Hits represented by the mapped Class for this index - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public SearchResponse search(String q) throws MeiliSearchRuntimeException { - try { - String requestQuery = "/indexes/" + indexName + "/search"; - SearchRequest sr = new SearchRequest(q); - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, - requestQuery, - Collections.emptyMap(), - serviceTemplate.getProcessor().encode(sr)), - SearchResponse.class, - indexModel); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * @param sr SearchRequest - * @return a SearchResponse with the Hits represented by the mapped Class for this index - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public SearchResponse search(SearchRequest sr) throws MeiliSearchRuntimeException { - try { - String requestQuery = "/indexes/" + indexName + "/search"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, - requestQuery, - Collections.emptyMap(), - serviceTemplate.getProcessor().encode(sr)), - SearchResponse.class, - indexModel); - } catch (Exception e) { - throw new MeiliSearchRuntimeException(e); - } - } - - /** - * Retrieve an task with a specific taskUid - * - * @param taskUid the taskUid - * @return the task belonging to the taskUid - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task getTask(int taskUid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + indexName + "/tasks/" + taskUid; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - // Must be reviewed when resolving the issue #315 - // /** - // * Retrieve a list containing all the tasks - // * - // * @return a List of Tasks - // * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - // * exceptions, etc) - // */ - // public List getTasks() throws MeiliSearchRuntimeException { - // String requestQuery = "/indexes/" + indexName + "/tasks"; - // return serviceTemplate.execute( - // requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), - // null), - // List.class, - // Task.class); - // } -} diff --git a/src/main/java/com/meilisearch/sdk/api/documents/SearchRequest.java b/src/main/java/com/meilisearch/sdk/api/documents/SearchRequest.java deleted file mode 100644 index de71c568..00000000 --- a/src/main/java/com/meilisearch/sdk/api/documents/SearchRequest.java +++ /dev/null @@ -1,202 +0,0 @@ -package com.meilisearch.sdk.api.documents; - -import java.util.Collections; -import java.util.List; -import lombok.Getter; - -@Getter -public class SearchRequest { - private final String q; - private final int offset; - private final int limit; - private final String[] filter; - private final String[][] filterArray; - private final List attributesToRetrieve; - private final List attributesToCrop; - private final int cropLength; - private final String cropMarker; - private final String highlightPreTag; - private final String highlightPostTag; - private final List attributesToHighlight; - private final boolean matches; - private final List sort; - - public SearchRequest(String q) { - this(q, 0); - } - - public SearchRequest(String q, int offset) { - this(q, offset, 20); - } - - public SearchRequest(String q, int offset, int limit) { - this(q, offset, limit, Collections.singletonList("*")); - } - - public SearchRequest(String q, int offset, int limit, List attributesToRetrieve) { - this( - q, - offset, - limit, - attributesToRetrieve, - null, - 200, - null, - null, - null, - null, - null, - null, - false, - null); - } - - public SearchRequest( - String q, - int offset, - int limit, - List attributesToRetrieve, - List attributesToCrop, - int cropLength, - List attributesToHighlight, - String[] filter, - boolean matches, - List sort) { - this( - q, - offset, - limit, - attributesToRetrieve, - null, - 200, - null, - null, - null, - null, - filter, - null, - false, - null); - } - - public SearchRequest( - String q, - int offset, - int limit, - List attributesToRetrieve, - List attributesToCrop, - int cropLength, - List attributesToHighlight, - String[][] filterArray, - boolean matches, - List sort) { - this( - q, - offset, - limit, - attributesToRetrieve, - null, - 200, - null, - null, - null, - null, - null, - filterArray, - false, - null); - } - - public SearchRequest( - String q, - int offset, - int limit, - List attributesToRetrieve, - List attributesToCrop, - int cropLength, - String cropMarker, - String highlightPreTag, - String highlightPostTag, - List attributesToHighlight, - String[] filter, - boolean matches, - List sort) { - this( - q, - offset, - limit, - attributesToRetrieve, - null, - 200, - cropMarker, - highlightPreTag, - highlightPostTag, - null, - filter, - null, - false, - null); - } - - public SearchRequest( - String q, - int offset, - int limit, - List attributesToRetrieve, - List attributesToCrop, - int cropLength, - String cropMarker, - String highlightPreTag, - String highlightPostTag, - List attributesToHighlight, - String[][] filterArray, - boolean matches, - List sort) { - this( - q, - offset, - limit, - attributesToRetrieve, - null, - 200, - cropMarker, - highlightPreTag, - highlightPostTag, - null, - null, - filterArray, - false, - null); - } - - public SearchRequest( - String q, - int offset, - int limit, - List attributesToRetrieve, - List attributesToCrop, - int cropLength, - String cropMarker, - String highlightPreTag, - String highlightPostTag, - List attributesToHighlight, - String[] filter, - String[][] filterArray, - boolean matches, - List sort) { - this.q = q; - this.offset = offset; - this.limit = limit; - this.attributesToRetrieve = attributesToRetrieve; - this.attributesToCrop = attributesToCrop; - this.cropLength = cropLength; - this.cropMarker = cropMarker; - this.highlightPreTag = highlightPreTag; - this.highlightPostTag = highlightPostTag; - this.attributesToHighlight = attributesToHighlight; - this.filter = filter; - this.filterArray = filterArray; - this.matches = matches; - this.sort = sort; - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/documents/SearchResponse.java b/src/main/java/com/meilisearch/sdk/api/documents/SearchResponse.java deleted file mode 100644 index f94d9de0..00000000 --- a/src/main/java/com/meilisearch/sdk/api/documents/SearchResponse.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.meilisearch.sdk.api.documents; - -import java.util.List; -import lombok.Getter; - -@Getter -public class SearchResponse { - private List hits; - private int offset; - private int limit; - private int nbHits; - private boolean exhaustiveNbHits; - private int processingTimeMs; - private String query; -} diff --git a/src/main/java/com/meilisearch/sdk/api/documents/Task.java b/src/main/java/com/meilisearch/sdk/api/documents/Task.java deleted file mode 100644 index 3ea17cda..00000000 --- a/src/main/java/com/meilisearch/sdk/api/documents/Task.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.meilisearch.sdk.api.documents; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class Task { - private String status; - private int uid; - private String indexUid; - private String type; - private String duration; - private String enqueuedAt; - private String startedAt; - private String finishedAt; -} diff --git a/src/main/java/com/meilisearch/sdk/api/index/Index.java b/src/main/java/com/meilisearch/sdk/api/index/Index.java deleted file mode 100644 index 42b72bfe..00000000 --- a/src/main/java/com/meilisearch/sdk/api/index/Index.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.meilisearch.sdk.api.index; - -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class Index { - private String uid; - private String primaryKey; - private String createdAt; - private String updatedAt; -} diff --git a/src/main/java/com/meilisearch/sdk/api/index/IndexHandler.java b/src/main/java/com/meilisearch/sdk/api/index/IndexHandler.java deleted file mode 100644 index 718da1ff..00000000 --- a/src/main/java/com/meilisearch/sdk/api/index/IndexHandler.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.meilisearch.sdk.api.index; - -import com.meilisearch.sdk.ServiceTemplate; -import com.meilisearch.sdk.api.documents.Task; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import com.meilisearch.sdk.http.response.HttpResponse; -import java.util.Collections; -import java.util.HashMap; - -public class IndexHandler { - private final ServiceTemplate serviceTemplate; - private final RequestFactory requestFactory; - private final SettingsHandler settingsHandler; - - public IndexHandler( - ServiceTemplate serviceTemplate, - RequestFactory requestFactory, - SettingsHandler settingsHandler) { - this.serviceTemplate = serviceTemplate; - this.requestFactory = requestFactory; - this.settingsHandler = settingsHandler; - } - - public IndexHandler(ServiceTemplate serviceTemplate, RequestFactory requestFactory) - throws MeiliSearchRuntimeException { - this(serviceTemplate, requestFactory, new SettingsHandler(serviceTemplate, requestFactory)); - } - - /** - * @param uid the uid of the index to be created - * @return an {@link Index} Object - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Index createIndex(String uid) throws MeiliSearchRuntimeException { - return this.createIndex(uid, null); - } - - /** - * @param uid the indexname - * @param primaryKey the primaryKey for that index - * @return the newly created index - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Index createIndex(String uid, String primaryKey) throws MeiliSearchRuntimeException { - HashMap body = new HashMap<>(); - body.put("uid", uid); - if (primaryKey != null) body.put("primaryKey", primaryKey); - return serviceTemplate.execute( - requestFactory.create(HttpMethod.POST, "/indexes", Collections.emptyMap(), body), - Index.class); - } - - /** - * @param uid the indexname - * @return the index - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Index getIndex(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Index.class); - } - - /** - * @return an array of all indexes - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Index[] getAllIndexes() throws MeiliSearchRuntimeException { - String requestQuery = "/indexes"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Index[].class); - } - - /** - * @param uid the indexname - * @param primaryKey the primaryKey for that index - * @return the updated index - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Index updateIndex(String uid, String primaryKey) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid; - HashMap body = new HashMap<>(); - body.put("primaryKey", primaryKey); - return serviceTemplate.execute( - requestFactory.create(HttpMethod.PUT, requestQuery, Collections.emptyMap(), body), - Index.class); - } - - /** - * @param uid the indexname - * @return true if the index was deleted, otherwise false - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public boolean deleteIndex(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid; - return ((HttpResponse) - serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, - requestQuery, - Collections.emptyMap(), - null), - null)) - .getStatusCode() - == 204; - } - - /** - * @param index the indexname - * @return the index settings - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Settings getSettings(String index) throws MeiliSearchRuntimeException { - return settingsHandler.getSettings(index); - } - - /** - * @param index the indexname - * @param settings the new Settings - * @return the updated settings - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task updateSettings(String index, Settings settings) throws MeiliSearchRuntimeException { - return settingsHandler.updateSettings(index, settings); - } - - /** - * @param index the indexname - * @return the settings - * @throws MeiliSearchRuntimeException in case something went wrong (http error, json - * exceptions, etc) - */ - public Task resetSettings(String index) throws MeiliSearchRuntimeException { - return settingsHandler.resetSettings(index); - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/index/Settings.java b/src/main/java/com/meilisearch/sdk/api/index/Settings.java deleted file mode 100644 index 03314dcb..00000000 --- a/src/main/java/com/meilisearch/sdk/api/index/Settings.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.meilisearch.sdk.api.index; - -import com.meilisearch.sdk.Index; -import com.meilisearch.sdk.TypoTolerance; -import java.util.HashMap; -import lombok.Getter; -import lombok.Setter; - -/** - * Data Structure for the Settings in an {@link Index} - * - *

Refer https://docs.meilisearch.com/references/settings.html - */ -@Getter -@Setter -public class Settings { - @Getter @Setter private HashMap synonyms; - @Getter @Setter private String[] stopWords; - @Getter @Setter private String[] rankingRules; - @Getter @Setter private String[] filterableAttributes; - @Getter @Setter private String distinctAttribute; - @Getter @Setter private String[] searchableAttributes; - @Getter @Setter private String[] displayedAttributes; - @Getter @Setter private String[] sortableAttributes; - @Getter @Setter private TypoTolerance typoTolerance; - - /** Empty SettingsRequest constructor */ - public Settings() {} -} diff --git a/src/main/java/com/meilisearch/sdk/api/index/SettingsHandler.java b/src/main/java/com/meilisearch/sdk/api/index/SettingsHandler.java deleted file mode 100644 index a389a0c5..00000000 --- a/src/main/java/com/meilisearch/sdk/api/index/SettingsHandler.java +++ /dev/null @@ -1,420 +0,0 @@ -package com.meilisearch.sdk.api.index; - -import com.meilisearch.sdk.ServiceTemplate; -import com.meilisearch.sdk.api.documents.Task; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import java.util.Collections; -import java.util.Map; - -public class SettingsHandler { - private final ServiceTemplate serviceTemplate; - private final RequestFactory requestFactory; - - public SettingsHandler(ServiceTemplate serviceTemplate, RequestFactory requestFactory) { - this.serviceTemplate = serviceTemplate; - this.requestFactory = requestFactory; - } - - /** - * Gets the settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#get-settings - * - * @param uid Index identifier - * @return settings of a given uid as String - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#update-settings - * - * @param uid Index identifier - * @param settings the data that contains the new settings - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateSettings(String uid, Settings settings) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, requestQuery, Collections.emptyMap(), settings), - Task.class); - } - - /** - * Resets the settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#reset-settings - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Gets the ranking rules settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#get-settings - * - * @param uid Index identifier - * @return ranking rules settings of a given uid as String - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getRankingRulesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/ranking-rules"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the ranking rules settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#update-settings - * - * @param uid Index identifier - * @param rankingRulesSettings the data that contains the new ranking rules settings - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateRankingRulesSettings(String uid, String[] rankingRulesSettings) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/ranking-rules"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, - requestQuery, - Collections.emptyMap(), - rankingRulesSettings), - Task.class); - } - - /** - * Resets the ranking rules settings of a given index Refer - * https://docs.meilisearch.com/references/settings.html#reset-settings - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetRankingRulesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/ranking-rules"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Gets the synonyms settings of a given index Refer - * https://docs.meilisearch.com/reference/api/synonyms.html#get-synonyms - * - * @param uid Index identifier - * @return a Map that contains all synonyms and their associated words - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getSynonymsSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/synonyms"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the synonyms settings of a given index Refer - * https://docs.meilisearch.com/reference/api/synonyms.html#update-synonyms - * - * @param uid Index identifier - * @param synonyms a Map that contains the new synonyms settings - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateSynonymsSettings(String uid, Map synonyms) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/synonyms"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, requestQuery, Collections.emptyMap(), synonyms), - Task.class); - } - - /** - * Resets the synonyms settings of a given index Refer - * https://docs.meilisearch.com/reference/api/synonyms.html#reset-synonyms - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetSynonymsSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/synonyms"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Gets the stop-words settings of the index Refer - * https://docs.meilisearch.com/reference/api/stop_words.html#get-stop-words - * - * @param uid Index identifier - * @return An array of strings that contains the stop-words. - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getStopWordsSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/stop-words"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the stop-words settings of the index Refer - * https://docs.meilisearch.com/reference/api/stop_words.html#update-stop-words - * - * @param uid Index identifier - * @param stopWords the data that contains the new ranking rules settings - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateStopWordsSettings(String uid, String[] stopWords) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/stop-words"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, requestQuery, Collections.emptyMap(), stopWords), - Task.class); - } - - /** - * Resets the stop-words settings of the index Refer - * https://docs.meilisearch.com/reference/api/stop_words.html#reset-stop-words - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetStopWordsSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/stop-words"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Get the searchable attributes of an index. - * https://docs.meilisearch.com/reference/api/searchable_attributes.html#get-searchable-attributes - * - * @param uid Index identifier - * @return searchable attributes of a given uid as String - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getSearchableAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/searchable-attributes"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the searchable attributes an index Refer - * https://docs.meilisearch.com/reference/api/searchable_attributes.html#update-searchable-attributes - * - * @param uid Index identifier - * @param searchableAttributes the data that contains the new ranking rules settings - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateSearchableAttributesSettings(String uid, String[] searchableAttributes) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/searchable-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, - requestQuery, - Collections.emptyMap(), - searchableAttributes), - Task.class); - } - - /** - * Reset the searchable attributes of the index to the default value. - * https://docs.meilisearch.com/reference/api/searchable_attributes.html#reset-searchable-attributes - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetSearchableAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/searchable-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Get the display attributes of an index. - * https://docs.meilisearch.com/reference/api/displayed_attributes.html#get-displayed-attributes - * - * @param uid Index identifier - * @return display attributes settings of a given uid as String - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getDisplayedAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/displayed-attributes"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the display attributes of an index Refer - * https://docs.meilisearch.com/reference/api/displayed_attributes.html#update-displayed-attributes - * - * @param uid Index identifier - * @param displayAttributes An array of strings that contains attributes of an index to display - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateDisplayedAttributesSettings(String uid, String[] displayAttributes) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/displayed-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, requestQuery, Collections.emptyMap(), displayAttributes), - Task.class); - } - - /** - * Reset the displayed attributes of the index to the default value. - * https://docs.meilisearch.com/reference/api/displayed_attributes.html#reset-displayed-attributes - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetDisplayedAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/displayed-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Get an index's filterableAttributes. - * https://docs.meilisearch.com/reference/api/filterable_attributes.html#get-filterable-attributes - * - * @param uid Index identifier - * @return filterable attributes settings of a given uid as String - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Settings getFilterableAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/filterable-attributes"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates an index's filterable attributes list. This will re-index all documents in the index. - * https://docs.meilisearch.com/reference/api/filterable_attributes.html#update-filterable-attributes - * - * @param uid Index identifier - * @param filterableAttributes An array of strings containing the attributes that can be used as - * filters at query time. - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task updateFilterableAttributesSettings(String uid, String[] filterableAttributes) - throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/filterable-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, - requestQuery, - Collections.emptyMap(), - filterableAttributes), - Task.class); - } - - /** - * Reset an index's filterable attributes list back to its default value. - * https://docs.meilisearch.com/reference/api/filterable_attributes.html#reset-filterable-attributes - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws MeiliSearchRuntimeException if something goes wrong - */ - public Task resetFilterableAttributesSettings(String uid) throws MeiliSearchRuntimeException { - String requestQuery = "/indexes/" + uid + "/settings/filterable-attributes"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } - - /** - * Get the distinct attribute field of an index. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#get-distinct-attribute - * - * @param uid Index identifier - * @return distinct attribute field of a given uid as String - * @throws Exception if something goes wrong - */ - public Settings getDistinctAttributeSettings(String uid) throws Exception { - String requestQuery = "/indexes/" + uid + "/settings/distinct-attribute"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Settings.class); - } - - /** - * Updates the distinct attribute field of an index. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#update-distinct-attribute - * - * @param uid Index identifier - * @param distinctAttribute A String: the field name. - * @return uid is the id of the task - * @throws Exception if something goes wrong - */ - public Task updateDistinctAttributeSettings(String uid, String distinctAttribute) - throws Exception { - String requestQuery = "/indexes/" + uid + "/settings/distinct-attribute"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.POST, requestQuery, Collections.emptyMap(), distinctAttribute), - Task.class); - } - - /** - * Reset the distinct attribute field of an index to its default value. - * https://docs.meilisearch.com/reference/api/distinct_attribute.html#reset-distinct-attribute - * - * @param uid Index identifier - * @return uid is the id of the task - * @throws Exception if something goes wrong - */ - public Task resetDistinctAttributeSettings(String uid) throws Exception { - String requestQuery = "/indexes/" + uid + "/settings/distinct-attribute"; - return serviceTemplate.execute( - requestFactory.create( - HttpMethod.DELETE, requestQuery, Collections.emptyMap(), null), - Task.class); - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/instance/IndexStats.java b/src/main/java/com/meilisearch/sdk/api/instance/IndexStats.java deleted file mode 100644 index 27b22ae9..00000000 --- a/src/main/java/com/meilisearch/sdk/api/instance/IndexStats.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.meilisearch.sdk.api.instance; - -import java.util.Map; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class IndexStats { - private long numberOfDocuments; - private boolean isIndexing; - private Map fieldDistribution; - - public IndexStats() {} - - public IndexStats( - long numberOfDocuments, boolean isIndexing, Map fieldDistribution) { - this.numberOfDocuments = numberOfDocuments; - this.isIndexing = isIndexing; - this.fieldDistribution = fieldDistribution; - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/instance/InstanceHandler.java b/src/main/java/com/meilisearch/sdk/api/instance/InstanceHandler.java deleted file mode 100644 index 2e77725f..00000000 --- a/src/main/java/com/meilisearch/sdk/api/instance/InstanceHandler.java +++ /dev/null @@ -1,63 +0,0 @@ -package com.meilisearch.sdk.api.instance; - -import com.meilisearch.sdk.ServiceTemplate; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class InstanceHandler { - private final ServiceTemplate serviceTemplate; - private final RequestFactory requestFactory; - - public InstanceHandler(ServiceTemplate serviceTemplate, RequestFactory requestFactory) { - this.serviceTemplate = serviceTemplate; - this.requestFactory = requestFactory; - } - - /** @return a map with health status of Meilisearch */ - public Map health() { - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, "/health", Collections.emptyMap(), null), - HashMap.class); - } - - /** @return true if everything is ok, false if Meilisearch is in maintenance mode */ - public boolean isHealthy() { - try { - this.health(); - return true; - } catch (MeiliSearchRuntimeException e) { - return false; - } - } - - /** @return a map with version information of Meilisearch */ - public Map getVersion() { - try { - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, "/version", Collections.emptyMap(), null), - HashMap.class, - String.class, - String.class); - } catch (MeiliSearchRuntimeException e) { - return Collections.emptyMap(); - } - } - - public IndexStats getStats(String index) { - String requestQuery = index + "/stats"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - IndexStats.class); - } - - public Stats getStats() { - String requestQuery = "/stats"; - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, requestQuery, Collections.emptyMap(), null), - Stats.class); - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/instance/Stats.java b/src/main/java/com/meilisearch/sdk/api/instance/Stats.java deleted file mode 100644 index 3d6599cb..00000000 --- a/src/main/java/com/meilisearch/sdk/api/instance/Stats.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.meilisearch.sdk.api.instance; - -import java.util.Date; -import java.util.Map; -import lombok.Getter; -import lombok.Setter; - -@Getter -@Setter -public class Stats { - private long databaseSize; - private Date lastUpdate; - private Map indexes; - - public Stats() {} - - public Stats(long databaseSize, Date lastUpdate, Map indexes) { - this.databaseSize = databaseSize; - this.lastUpdate = lastUpdate; - this.indexes = indexes; - } -} diff --git a/src/main/java/com/meilisearch/sdk/api/keys/KeysHandler.java b/src/main/java/com/meilisearch/sdk/api/keys/KeysHandler.java deleted file mode 100644 index 57ef3d38..00000000 --- a/src/main/java/com/meilisearch/sdk/api/keys/KeysHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.meilisearch.sdk.api.keys; - -import com.meilisearch.sdk.ServiceTemplate; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpMethod; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -public class KeysHandler { - private final ServiceTemplate serviceTemplate; - private final RequestFactory requestFactory; - - public KeysHandler(ServiceTemplate serviceTemplate, RequestFactory requestFactory) { - this.serviceTemplate = serviceTemplate; - this.requestFactory = requestFactory; - } - - /** - * @return the public and private keys in a map - * @throws com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException in case some error happens - */ - public Map get() throws MeiliSearchRuntimeException { - return serviceTemplate.execute( - requestFactory.create(HttpMethod.GET, "/keys", Collections.emptyMap(), null), - HashMap.class, - String.class, - String.class); - } -} diff --git a/src/test/java/com/meilisearch/sdk/GenericServiceTemplateTest.java b/src/test/java/com/meilisearch/sdk/GenericServiceTemplateTest.java deleted file mode 100644 index 41d9f16f..00000000 --- a/src/test/java/com/meilisearch/sdk/GenericServiceTemplateTest.java +++ /dev/null @@ -1,255 +0,0 @@ -package com.meilisearch.sdk; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.request.BasicHttpRequest; -import com.meilisearch.sdk.http.request.HttpMethod; -import com.meilisearch.sdk.http.response.BasicHttpResponse; -import com.meilisearch.sdk.json.JsonHandler; -import java.util.Collections; -import java.util.Map; -import org.junit.jupiter.api.Test; - -class GenericServiceTemplateTest { - - private final AbstractHttpClient client = mock(AbstractHttpClient.class); - private final JsonHandler handler = mock(JsonHandler.class); - private final GenericServiceTemplate classToTest = - new GenericServiceTemplate(client, handler, new BasicRequestFactory(handler)); - - @Test - void getClient() { - assertEquals(client, classToTest.getClient()); - } - - @Test - void getProcessor() { - assertEquals(handler, classToTest.getProcessor()); - } - - @Test - void executeGet() throws Exception { - when(client.get(any(BasicHttpRequest.class))) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }); - BasicHttpRequest request = - new BasicHttpRequest(HttpMethod.GET, "/path", Collections.emptyMap(), null); - MockHttpResponse response = classToTest.execute(request, null); - assertEquals(request.getPath(), response.getRequestPath()); - assertEquals(request.getHeaders(), response.getHeaders()); - } - - @Test - void executePost() throws Exception { - when(client.post(any())) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }); - BasicHttpRequest fullRequest = - new BasicHttpRequest(HttpMethod.POST, "/path", Collections.emptyMap(), "content"); - MockHttpResponse response = classToTest.execute(fullRequest, null); - assertEquals(fullRequest.getPath(), response.getRequestPath()); - assertEquals(fullRequest.getContent(), response.getContent()); - assertEquals(fullRequest.getHeaders(), response.getHeaders()); - - MockHttpRequest contentlessRequest = - new MockHttpRequest(HttpMethod.POST, "/path", Collections.emptyMap(), null); - response = classToTest.execute(contentlessRequest, null); - assertEquals(contentlessRequest.getPath(), response.getRequestPath()); - assertNull(response.getContent()); - assertEquals(contentlessRequest.getHeaders(), response.getHeaders()); - } - - @Test - void executePut() throws Exception { - when(client.put(any())) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }); - BasicHttpRequest fullRequest = - new BasicHttpRequest(HttpMethod.PUT, "/path", Collections.emptyMap(), "content"); - MockHttpResponse response = classToTest.execute(fullRequest, null); - assertEquals(fullRequest.getPath(), response.getRequestPath()); - assertEquals(fullRequest.getContent(), response.getContent()); - assertEquals(fullRequest.getHeaders(), response.getHeaders()); - - MockHttpRequest contentlessRequest = - new MockHttpRequest(HttpMethod.PUT, "/path", Collections.emptyMap(), null); - response = classToTest.execute(contentlessRequest, null); - assertEquals(contentlessRequest.getPath(), response.getRequestPath()); - assertNull(response.getContent()); - assertEquals(contentlessRequest.getHeaders(), response.getHeaders()); - } - - @Test - void executeDelete() throws Exception { - when(client.delete(any())) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }) - .thenAnswer( - invocationOnMock -> { - BasicHttpRequest request = invocationOnMock.getArgument(0); - return new MockHttpResponse( - request.getPath(), request.getHeaders(), request.getContent()); - }); - BasicHttpRequest fullRequest = - new BasicHttpRequest(HttpMethod.DELETE, "/path", Collections.emptyMap(), "content"); - MockHttpResponse response = classToTest.execute(fullRequest, null); - assertEquals(fullRequest.getPath(), response.getRequestPath()); - assertNotNull(response.getContent()); - assertEquals(fullRequest.getHeaders(), response.getHeaders()); - - MockHttpRequest contentlessRequest = - new MockHttpRequest(HttpMethod.DELETE, "/path", Collections.emptyMap(), null); - response = classToTest.execute(contentlessRequest, null); - assertEquals(contentlessRequest.getPath(), response.getRequestPath()); - assertNull(response.getContent()); - assertEquals(contentlessRequest.getHeaders(), response.getHeaders()); - } - - @Test - void defaultBranch() { - assertThrows( - MeiliSearchRuntimeException.class, - () -> - classToTest.execute( - new MockHttpRequest(HttpMethod.HEAD, null, null, null), null)); - } - - public static class MockHttpRequest extends BasicHttpRequest { - private HttpMethod method; - private String path; - - private Map headers; - private String content; - - public MockHttpRequest( - HttpMethod method, String path, Map headers, String content) { - this.method = method; - this.path = path; - this.headers = headers; - this.content = content; - } - - @Override - public HttpMethod getMethod() { - return method; - } - - @Override - public void setMethod(HttpMethod method) { - this.method = method; - } - - @Override - public String getPath() { - return path; - } - - @Override - public void setPath(String path) { - this.path = path; - } - - @Override - public Map getHeaders() { - return headers; - } - - @Override - public void setHeaders(Map headers) { - this.headers = headers; - } - - @Override - public boolean hasContent() { - return this.content == null || "".equals(this.content); - } - - @Override - public String getContent() { - return this.content; - } - - @Override - public byte[] getContentAsBytes() { - return new byte[0]; - } - } - - public static class MockHttpResponse extends BasicHttpResponse { - - private final String requestPath; - private final Map requestHeaders; - private final String requestBody; - - public MockHttpResponse( - String requestPath, Map requestHeaders, String requestBody) { - super(requestHeaders, 200, requestBody); - this.requestPath = requestPath; - this.requestHeaders = requestHeaders; - this.requestBody = requestBody; - } - - @Override - public Map getHeaders() { - return requestHeaders; - } - - @Override - public int getStatusCode() { - return 0; - } - - @Override - public boolean hasContent() { - return requestBody == null || "".equals(requestBody); - } - - @Override - public String getContent() { - return requestBody; - } - - @Override - public byte[] getContentAsBytes() { - return new byte[0]; - } - - public String getRequestPath() { - return requestPath; - } - } -} diff --git a/src/test/java/com/meilisearch/sdk/api/documents/DocumentHandlerTest.java b/src/test/java/com/meilisearch/sdk/api/documents/DocumentHandlerTest.java deleted file mode 100644 index f9681e4d..00000000 --- a/src/test/java/com/meilisearch/sdk/api/documents/DocumentHandlerTest.java +++ /dev/null @@ -1,216 +0,0 @@ -package com.meilisearch.sdk.api.documents; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.meilisearch.sdk.GenericServiceTemplate; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.http.response.BasicHttpResponse; -import com.meilisearch.sdk.json.JacksonJsonHandler; -import com.meilisearch.sdk.json.JsonHandler; -import com.meilisearch.sdk.utils.Movie; -import java.util.Collections; -import java.util.List; -import org.junit.jupiter.api.Test; - -class DocumentHandlerTest { - - private final AbstractHttpClient client = mock(AbstractHttpClient.class); - private final JsonHandler jsonHandler = new JacksonJsonHandler(new ObjectMapper()); - private final RequestFactory requestFactory = new BasicRequestFactory(jsonHandler); - private final GenericServiceTemplate serviceTemplate = - new GenericServiceTemplate(client, jsonHandler, requestFactory); - private final DocumentHandler classToTest = - new DocumentHandler(serviceTemplate, requestFactory, "movies", Movie.class); - - @Test - void getDocument() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"id\":25684,\"title\":\"American Ninja 5\",\"poster\":\"https://image.tmdb.org/t/p/w1280/iuAQVI4mvjI83wnirpD8GVNRVuY.jpg\",\"overview\":\"When a scientists daughter is kidnapped, American Ninja, attempts to find her, but this time he teams up with a youngster he has trained in the ways of the ninja.\",\"release_date\":\"1993-01-01\"}")); - Movie movie = classToTest.getDocument("25684"); - assertNotNull(movie); - assertEquals("25684", movie.getId()); - assertEquals("American Ninja 5", movie.getTitle()); - assertEquals( - "https://image.tmdb.org/t/p/w1280/iuAQVI4mvjI83wnirpD8GVNRVuY.jpg", - movie.getPoster()); - assertEquals( - "When a scientists daughter is kidnapped, American Ninja, attempts to find her, but this time he teams up with a youngster he has trained in the ways of the ninja.", - movie.getOverview()); - assertEquals("1993-01-01", movie.getRelease_date()); - } - - @Test - void getDocuments() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "[{\"id\":25684,\"release_date\":\"1993-01-01\",\"poster\":\"https://image.tmdb.org/t/p/w1280/iuAQVI4mvjI83wnirpD8GVNRVuY.jpg\",\"title\":\"American Ninja 5\",\"overview\":\"When a scientists daughter is kidnapped, American Ninja, attempts to find her, but this time he teams up with a youngster he has trained in the ways of the ninja.\"},{\"id\":468219,\"title\":\"Dead in a Week (Or Your Money Back)\",\"release_date\":\"2018-09-12\",\"poster\":\"https://image.tmdb.org/t/p/w1280/f4ANVEuEaGy2oP5M0Y2P1dwxUNn.jpg\",\"overview\":\"William has failed to kill himself so many times that he outsources his suicide to aging assassin Leslie. But with the contract signed and death assured within a week (or his money back), William suddenly discovers reasons to live... However Leslie is under pressure from his boss to make sure the contract is completed.\"}]")); - List movies = classToTest.getDocuments(); - assertNotNull(movies); - assertEquals(2, movies.size()); - Movie movie = movies.get(0); - assertEquals("25684", movie.getId()); - assertEquals("American Ninja 5", movie.getTitle()); - assertEquals( - "https://image.tmdb.org/t/p/w1280/iuAQVI4mvjI83wnirpD8GVNRVuY.jpg", - movie.getPoster()); - assertEquals( - "When a scientists daughter is kidnapped, American Ninja, attempts to find her, but this time he teams up with a youngster he has trained in the ways of the ninja.", - movie.getOverview()); - assertEquals("1993-01-01", movie.getRelease_date()); - } - - @Test - void addAndReplaceDocument() throws Exception { - when(client.post(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, "{\"uid\":1}")); - Movie movie = - new Movie( - "287947", - "Shazam", - "https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg", - "A boy is given the ability to become an adult superhero in times of need with a single magic word.", - "2019-03-23", - "English", - "Action", - "Comedy", - "Fantasy"); - Task task = classToTest.replaceDocuments(Collections.singletonList(movie)); - assertNotNull(task); - assertEquals(1, task.getUid()); - } - - @Test - void addAndUpdateDocument() throws Exception { - when(client.put(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, "{\"uid\":1}")); - Task task = - classToTest.updateDocuments( - "[{\"id\":287947,\"title\":\"Shazam\",\"poster\":\"https://image.tmdb.org/t/p/w1280/xnopI5Xtky18MPhK40cZAGAOVeV.jpg\",\"overview\":\"A boy is given the ability to become an adult superhero in times of need with a single magic word.\",\"release_date\":\"2019-03-23\"}]"); - assertNotNull(task); - assertEquals(1, task.getUid()); - } - - @Test - void deleteDocument() throws Exception { - when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, "{\"uid\": 1}")); - assertEquals(1, classToTest.deleteDocument("123").getUid()); - when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, null)); - assertThrows(MeiliSearchRuntimeException.class, () -> classToTest.deleteDocument("123")); - when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, "")); - assertThrows(MeiliSearchRuntimeException.class, () -> classToTest.deleteDocument("123")); - } - - @Test - void deleteDocuments() throws Exception { - when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 200, "{\"uid\": 1}")); - assertEquals(1, classToTest.deleteDocuments().getUid()); - } - - @Test - void search() throws Exception { - when(client.post(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"hits\":[{\"id\":\"2770\",\"title\":\"American Pie 2\",\"poster\":\"https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg\",\"overview\":\"The whole gang are back and as close as ever. They decide to get even closer by spending the summer together at a beach house. They decide to hold the biggest...\",\"release_date\":997405200},{\"id\":\"190859\",\"title\":\"American Sniper\",\"poster\":\"https://image.tmdb.org/t/p/w1280/svPHnYE7N5NAGO49dBmRhq0vDQ3.jpg\",\"overview\":\"U.S. Navy SEAL Chris Kyle takes his sole mission—protect his comrades—to heart and becomes one of the most lethal snipers in American history. His pinpoint accuracy not only saves countless lives but also makes him a prime...\",\"release_date\":1418256000}],\"offset\":0,\"limit\":20,\"nbHits\":1,\"exhaustiveNbHits\":false,\"processingTimeMs\":2,\"query\":\"test120232\"}")); - SearchResponse movieSearchResponse = classToTest.search("American Pie"); - assertNotNull(movieSearchResponse); - assertNotNull(movieSearchResponse.getHits()); - assertEquals(2, movieSearchResponse.getHits().size()); - assertEquals("American Pie 2", movieSearchResponse.getHits().get(0).getTitle()); - assertEquals("2770", movieSearchResponse.getHits().get(0).getId()); - assertEquals( - "https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg", - movieSearchResponse.getHits().get(0).getPoster()); - assertEquals("997405200", movieSearchResponse.getHits().get(0).getRelease_date()); - } - - @Test - void testSearch() throws Exception { - when(client.post(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"hits\":[{\"id\":\"2770\",\"title\":\"American Pie 2\",\"poster\":\"https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg\",\"overview\":\"The whole gang are back and as close as ever. They decide to get even closer by spending the summer together at a beach house. They decide to hold the biggest...\",\"release_date\":997405200},{\"id\":\"190859\",\"title\":\"American Sniper\",\"poster\":\"https://image.tmdb.org/t/p/w1280/svPHnYE7N5NAGO49dBmRhq0vDQ3.jpg\",\"overview\":\"U.S. Navy SEAL Chris Kyle takes his sole mission—protect his comrades—to heart and becomes one of the most lethal snipers in American history. His pinpoint accuracy not only saves countless lives but also makes him a prime...\",\"release_date\":1418256000}],\"offset\":0,\"limit\":20,\"nbHits\":1,\"exhaustiveNbHits\":false,\"processingTimeMs\":2,\"query\":\"test120232\"}")); - SearchResponse movieSearchResponse = - classToTest.search(new SearchRequest("american pie")); - assertNotNull(movieSearchResponse); - assertNotNull(movieSearchResponse.getHits()); - assertEquals(2, movieSearchResponse.getHits().size()); - assertEquals("American Pie 2", movieSearchResponse.getHits().get(0).getTitle()); - assertEquals("2770", movieSearchResponse.getHits().get(0).getId()); - assertEquals( - "https://image.tmdb.org/t/p/w1280/q4LNgUnRfltxzp3gf1MAGiK5LhV.jpg", - movieSearchResponse.getHits().get(0).getPoster()); - assertEquals("997405200", movieSearchResponse.getHits().get(0).getRelease_date()); - } - - @Test - void getTask() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"status\":\"succeeded\",\"uid\": 1,\"indexUid\":\"AddDocuments\",\"type\":\"documentAddition\",\"duration\":\"PT0.021985708S\",\"enqueuedAt\":\"2022-01-11T13:54:18.408270424Z\",\"startedAt\":\"2022-01-11T13:54:18.411897715Z\",\"finishedAt\":\"2022-01-11T13:54:18.430256132Z\"}")); - Task task = classToTest.getTask(1); - assertNotNull(task); - assertEquals("succeeded", task.getStatus()); - assertEquals(1, task.getUid()); - assertEquals("documentAddition", task.getType()); - assertEquals("PT0.021985708S", task.getDuration()); - assertEquals("2022-01-11T13:54:18.408270424Z", task.getEnqueuedAt()); - assertEquals("2022-01-11T13:54:18.411897715Z", task.getStartedAt()); - assertEquals("2022-01-11T13:54:18.430256132Z", task.getFinishedAt()); - } - - // Must be reviewed when resolving the issue #315 - // @Test - // void getTasks() throws Exception { - // when(client.get(any(HttpRequest.class))) - // .thenAnswer( - // invocation -> - // new BasicHttpResponse( - // null, - // 200, - // "\"results\":[{\"status\":\"succeeded\",\"uid\": - // 1,\"indexUid\":\"AddDocuments\",\"type\":\"documentAddition\",\"duration\":\"PT0.021985708S\",\"enqueuedAt\":\"2022-01-11T13:54:18.408270424Z\",\"startedAt\":\"2022-01-11T13:54:18.411897715Z\",\"finishedAt\":\"2022-01-11T13:54:18.430256132Z\"}]")); - // // - // "[{\"status\":\"processed\",\"uid\":1,\"type\":{\"name\":\"DocumentsAddition\",\"number\":4},\"duration\":0.076980613,\"enqueuedAt\":\"2019-12-07T21:16:09.623944Z\",\"processedAt\":\"2019-12-07T21:16:09.703509Z\"}]")); - // List tasks = classToTest.getTasks(); - // assertNotNull(tasks); - // assertEquals(1, tasks.size()); - // Task task = tasks.get(0); - // assertEquals("succeeded", task.getStatus()); - // assertEquals(1, task.getUid()); - // assertEquals("DocumentsAddition", task.getType()); - // assertEquals(0.076980613, task.getDuration()); - // assertEquals("2019-12-07T21:16:09.623944Z", task.getEnqueuedAt()); - // assertEquals("2022-01-11T13:54:18.411897715Z", task.getStartedAt()); - // assertEquals("2022-01-11T13:54:18.430256132Z", task.getFinishedAt()); - // } -} diff --git a/src/test/java/com/meilisearch/sdk/api/index/IndexHandlerTest.java b/src/test/java/com/meilisearch/sdk/api/index/IndexHandlerTest.java deleted file mode 100644 index d122c5d9..00000000 --- a/src/test/java/com/meilisearch/sdk/api/index/IndexHandlerTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.meilisearch.sdk.api.index; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.mock; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.meilisearch.sdk.GenericServiceTemplate; -import com.meilisearch.sdk.api.documents.Task; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.BasicHttpRequest; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.http.response.BasicHttpResponse; -import com.meilisearch.sdk.json.JacksonJsonHandler; -import com.meilisearch.sdk.json.JsonHandler; -import java.util.ArrayDeque; -import java.util.Deque; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -class IndexHandlerTest { - - private final AbstractHttpClient client = mock(AbstractHttpClient.class); - private final JsonHandler jsonHandler = new JacksonJsonHandler(new ObjectMapper()); - private final SettingsHandler settingsService = mock(SettingsHandler.class); - private final RequestFactory requestFactory = new BasicRequestFactory(jsonHandler); - private final GenericServiceTemplate serviceTemplate = - new GenericServiceTemplate(client, jsonHandler, requestFactory); - private final IndexHandler classToTest = - new IndexHandler(serviceTemplate, requestFactory, settingsService); - - @Test - void create() throws Exception { - Mockito.when(client.post(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"uid\":\"movies\",\"primaryKey\":\"id\",\"createdAt\":\"2019-11-20T09:40:33.711476Z\",\"updatedAt\":\"2019-11-20T09:40:33.711476Z\"}")); - Index index = classToTest.createIndex("movies"); - assertEquals("movies", index.getUid()); - assertEquals("id", index.getPrimaryKey()); - } - - @Test - void createWithPrimaryKey() throws Exception { - Mockito.when(client.post(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"uid\":\"movies\",\"primaryKey\":\"movie_id\",\"createdAt\":\"2019-11-20T09:40:33.711476Z\",\"updatedAt\":\"2019-11-20T09:40:33.711476Z\"}")); - Index index = classToTest.createIndex("movies", "movie_id"); - assertEquals("movies", index.getUid()); - assertEquals("movie_id", index.getPrimaryKey()); - } - - @Test - void get() throws Exception { - Mockito.when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"uid\":\"movies\",\"primaryKey\":\"movie_id\",\"createdAt\":\"2019-11-20T09:40:33.711476Z\",\"updatedAt\":\"2019-11-20T09:40:33.711476Z\"}")); - Index index = classToTest.getIndex("movies"); - assertEquals("movies", index.getUid()); - assertEquals("movie_id", index.getPrimaryKey()); - } - - @Test - void getAll() throws Exception { - Mockito.when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "[{\"uid\":\"movies\",\"primaryKey\":\"movie_id\",\"createdAt\":\"2019-11-20T09:40:33.711476Z\",\"updatedAt\":\"2019-11-20T09:40:33.711476Z\"}]")); - Index[] index = classToTest.getAllIndexes(); - assertEquals(1, index.length); - assertEquals("movies", index[0].getUid()); - assertEquals("movie_id", index[0].getPrimaryKey()); - } - - @Test - void update() throws Exception { - Deque deque = new ArrayDeque<>(); - Mockito.when(client.put(any(HttpRequest.class))) - .then( - invocationOnMock -> { - deque.add((BasicHttpRequest) invocationOnMock.getArguments()[0]); - return new BasicHttpResponse( - null, - 200, - "{\"uid\":\"movies\",\"primaryKey\":\"movie_id\",\"createdAt\":\"2019-11-20T09:40:33.711476Z\",\"updatedAt\":\"2019-11-20T09:40:33.711476Z\"}"); - }); - Index index = classToTest.updateIndex("movies", "movie_id"); - assertEquals("movies", index.getUid()); - assertEquals("movie_id", index.getPrimaryKey()); - } - - @Test - void delete() throws Exception { - Mockito.when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 204, "")); - assertTrue(classToTest.deleteIndex("movies")); - Mockito.when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 404, "")); - assertFalse(classToTest.deleteIndex("movies")); - Mockito.when(client.delete(any(HttpRequest.class))) - .thenAnswer(invocation -> new BasicHttpResponse(null, 100, "")); - assertFalse(classToTest.deleteIndex("movies")); - } - - @Test - void settings() { - Settings dummySettings = new Settings(); - dummySettings.setDistinctAttribute("test"); - Task dummyTask = new Task(); - Mockito.when(settingsService.getSettings(any())).thenReturn(dummySettings); - Mockito.when(settingsService.resetSettings(any())).thenReturn(dummyTask); - Mockito.when(settingsService.updateSettings(any(), any())).thenReturn(dummyTask); - - assertThat(classToTest.getSettings("test"), is(equalTo(dummySettings))); - assertThat(classToTest.resetSettings("test"), is(equalTo(dummyTask))); - assertThat(classToTest.updateSettings("test", dummySettings), is(equalTo(dummyTask))); - } -} diff --git a/src/test/java/com/meilisearch/sdk/api/instance/InstanceHandlerTest.java b/src/test/java/com/meilisearch/sdk/api/instance/InstanceHandlerTest.java deleted file mode 100644 index 254ca5ea..00000000 --- a/src/test/java/com/meilisearch/sdk/api/instance/InstanceHandlerTest.java +++ /dev/null @@ -1,120 +0,0 @@ -package com.meilisearch.sdk.api.instance; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.hasItems; -import static org.hamcrest.Matchers.is; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -import com.meilisearch.sdk.GenericServiceTemplate; -import com.meilisearch.sdk.exceptions.MeiliSearchRuntimeException; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.http.response.BasicHttpResponse; -import com.meilisearch.sdk.json.JacksonJsonHandler; -import com.meilisearch.sdk.json.JsonHandler; -import java.util.Map; -import org.junit.jupiter.api.Test; - -class InstanceHandlerTest { - - private final AbstractHttpClient client = mock(AbstractHttpClient.class); - private final JsonHandler handler = new JacksonJsonHandler(); - private final RequestFactory requestFactory = new BasicRequestFactory(handler); - private final GenericServiceTemplate serviceTemplate = - new GenericServiceTemplate(client, handler, requestFactory); - InstanceHandler classToTest = new InstanceHandler(serviceTemplate, requestFactory); - - @Test - void isHealthy() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse(null, 200, "{\"status\":\"available\"}")) - .thenThrow(MeiliSearchRuntimeException.class); - assertTrue(classToTest.isHealthy()); - assertFalse(classToTest.isHealthy()); - } - - @Test - void health() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse(null, 200, "{\"status\":\"available\"}")) - .thenThrow(MeiliSearchRuntimeException.class); - Map health = classToTest.health(); - assertNotNull(health); - assertTrue(health.containsKey("status")); - assertEquals("available", health.get("status")); - } - - @Test - void version() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"commitSha\":\"b46889b5f0f2f8b91438a08a358ba8f05fc09fc1\",\"commitDate\":\"2019-11-15T09:51:54.278247+00:00\",\"pkgVersion\":\"0.1.1\"}")) - .thenThrow(MeiliSearchRuntimeException.class); - Map version = classToTest.getVersion(); - assertNotNull(version); - assertTrue(version.containsKey("commitSha")); - assertEquals("b46889b5f0f2f8b91438a08a358ba8f05fc09fc1", version.get("commitSha")); - assertTrue(version.containsKey("commitDate")); - assertEquals("2019-11-15T09:51:54.278247+00:00", version.get("commitDate")); - assertTrue(version.containsKey("pkgVersion")); - assertEquals("0.1.1", version.get("pkgVersion")); - version = classToTest.getVersion(); - assertNotNull(version); - assertEquals(0, version.size()); - } - - @Test - void fullStats() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"databaseSize\": 7701901318,\"lastUpdate\": \"2019-11-15T11:15:22.092896Z\",\"indexes\": {\"movies\": {\"numberOfDocuments\": 19654,\"isIndexing\": false,\"fieldDistribution\": {\"poster\": 19654,\"overview\": 19654,\"title\": 19654,\"id\": 19654,\"release_date\": 19654}},\"rangemovies\": {\"numberOfDocuments\": 19654,\"isIndexing\": false,\"fieldDistribution\": {\"overview\": 19654,\"id\": 19654,\"title\": 19654}}}}")) - .thenThrow(MeiliSearchRuntimeException.class); - Stats stats = classToTest.getStats(); - assertThat(stats.getDatabaseSize(), is(7701901318L)); - assertThat(stats.getLastUpdate().toInstant().toEpochMilli(), is(1573816522092L)); - assertThat(stats.getIndexes().keySet(), hasItems("movies", "rangemovies")); - IndexStats movies = stats.getIndexes().get("movies"); - assertThat(movies.getNumberOfDocuments(), is(19654L)); - assertThat(movies.isIndexing(), is(false)); - assertThat(movies.getFieldDistribution().keySet(), hasItems("overview", "id", "title")); - assertThat(movies.getFieldDistribution().get("overview"), is(19654)); - assertThat(movies.getFieldDistribution().get("id"), is(19654)); - assertThat(movies.getFieldDistribution().get("title"), is(19654)); - } - - @Test - void singleStats() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"numberOfDocuments\": 19654,\"isIndexing\": false,\"fieldDistribution\": {\"poster\": 19654,\"release_date\": 19654,\"title\": 19654,\"id\": 19654,\"overview\": 19654}}")) - .thenThrow(MeiliSearchRuntimeException.class); - IndexStats stats = classToTest.getStats("index"); - assertThat(stats.getNumberOfDocuments(), is(19654L)); - assertThat(stats.isIndexing(), is(false)); - assertThat( - stats.getFieldDistribution().keySet(), - hasItems("overview", "id", "title", "release_date", "poster")); - assertThat(stats.getFieldDistribution().get("overview"), is(19654)); - assertThat(stats.getFieldDistribution().get("id"), is(19654)); - assertThat(stats.getFieldDistribution().get("title"), is(19654)); - } -} diff --git a/src/test/java/com/meilisearch/sdk/api/keys/KeysHandlerTest.java b/src/test/java/com/meilisearch/sdk/api/keys/KeysHandlerTest.java deleted file mode 100644 index 176f8235..00000000 --- a/src/test/java/com/meilisearch/sdk/api/keys/KeysHandlerTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.meilisearch.sdk.api.keys; - -import static org.junit.jupiter.api.Assertions.assertIterableEquals; -import static org.mockito.Mockito.*; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.meilisearch.sdk.GenericServiceTemplate; -import com.meilisearch.sdk.http.AbstractHttpClient; -import com.meilisearch.sdk.http.factory.BasicRequestFactory; -import com.meilisearch.sdk.http.factory.RequestFactory; -import com.meilisearch.sdk.http.request.HttpRequest; -import com.meilisearch.sdk.http.response.BasicHttpResponse; -import com.meilisearch.sdk.json.JacksonJsonHandler; -import com.meilisearch.sdk.json.JsonHandler; -import java.util.Map; -import java.util.stream.Collectors; -import java.util.stream.Stream; -import org.junit.jupiter.api.Test; - -class KeysHandlerTest { - private final AbstractHttpClient client = mock(AbstractHttpClient.class); - private final JsonHandler processor = new JacksonJsonHandler(new ObjectMapper()); - private final RequestFactory requestFactory = new BasicRequestFactory(processor); - private final GenericServiceTemplate serviceTemplate = - new GenericServiceTemplate(client, processor, requestFactory); - private final KeysHandler classToTest = new KeysHandler(serviceTemplate, requestFactory); - - @Test - void get() throws Exception { - when(client.get(any(HttpRequest.class))) - .thenAnswer( - invocation -> - new BasicHttpResponse( - null, - 200, - "{\"private\":\"8c222193c4dff5a19689d637416820bc623375f2ad4c31a2e3a76e8f4c70440d\",\"public\":\"948413b6667024a0704c2023916c21eaf0a13485a586c43e4d2df520852a4fb8\"}")); - Map keys = classToTest.get(); - assertIterableEquals( - Stream.of("private", "public").collect(Collectors.toList()), keys.keySet()); - } -}