|
| 1 | +package com.meilisearch.sdk; |
| 2 | + |
| 3 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 4 | +import static org.hamcrest.Matchers.equalTo; |
| 5 | +import static org.hamcrest.Matchers.is; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | +import org.json.JSONObject; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +public class ExportRequestTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + void toStringSimpleExportIndexFilter() { |
| 16 | + ExportIndexFilter filter = ExportIndexFilter.builder().build(); |
| 17 | + JSONObject json = new JSONObject(filter.toString()); |
| 18 | + assertThat(json.has("overrideSettings"), is(false)); |
| 19 | + assertThat(json.has("filter"), is(false)); |
| 20 | + } |
| 21 | + |
| 22 | + @Test |
| 23 | + void toStringExportIndexFilterWithOverride() { |
| 24 | + ExportIndexFilter filter = ExportIndexFilter.builder().overrideSettings(true).build(); |
| 25 | + JSONObject json = new JSONObject(filter.toString()); |
| 26 | + assertThat(json.getBoolean("overrideSettings"), is(true)); |
| 27 | + assertThat(json.has("filter"), is(false)); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + void toStringExportIndexFilterWithFilter() { |
| 32 | + ExportIndexFilter filter = ExportIndexFilter.builder().filter("status = 'active'").build(); |
| 33 | + JSONObject json = new JSONObject(filter.toString()); |
| 34 | + assertThat(json.getString("filter"), is(equalTo("status = 'active'"))); |
| 35 | + assertThat(json.has("overrideSettings"), is(false)); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void toStringSimpleExportRequest() { |
| 40 | + ExportRequest request = |
| 41 | + ExportRequest.builder().url("http://localhost:7711").payloadSize("123 MiB").build(); |
| 42 | + JSONObject json = new JSONObject(request.toString()); |
| 43 | + assertThat(json.getString("url"), is(equalTo("http://localhost:7711"))); |
| 44 | + assertThat(json.getString("payloadSize"), is(equalTo("123 MiB"))); |
| 45 | + assertThat(json.has("apiKey"), is(false)); |
| 46 | + assertThat(json.has("indexes"), is(false)); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void toStringExportRequestWithIndexes() { |
| 51 | + Map<String, ExportIndexFilter> indexes = new HashMap<>(); |
| 52 | + indexes.put("*", ExportIndexFilter.builder().overrideSettings(true).build()); |
| 53 | + |
| 54 | + ExportRequest request = |
| 55 | + ExportRequest.builder() |
| 56 | + .url("http://localhost:7711") |
| 57 | + .payloadSize("123 MiB") |
| 58 | + .indexes(indexes) |
| 59 | + .build(); |
| 60 | + |
| 61 | + JSONObject json = new JSONObject(request.toString()); |
| 62 | + |
| 63 | + assertThat(json.getString("url"), is(equalTo("http://localhost:7711"))); |
| 64 | + assertThat(json.getString("payloadSize"), is(equalTo("123 MiB"))); |
| 65 | + assertThat(json.has("apiKey"), is(false)); |
| 66 | + JSONObject indexesJson = json.getJSONObject("indexes"); |
| 67 | + JSONObject starIndex = indexesJson.getJSONObject("*"); |
| 68 | + assertThat(starIndex.has("filter"), is(false)); |
| 69 | + assertThat(starIndex.getBoolean("overrideSettings"), is(true)); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void gettersExportRequest() { |
| 74 | + Map<String, ExportIndexFilter> indexes = new HashMap<>(); |
| 75 | + indexes.put( |
| 76 | + "myindex", |
| 77 | + ExportIndexFilter.builder().filter("id > 10").overrideSettings(false).build()); |
| 78 | + |
| 79 | + ExportRequest request = |
| 80 | + ExportRequest.builder() |
| 81 | + .url("http://localhost:7711") |
| 82 | + .apiKey("mykey") |
| 83 | + .payloadSize("50 MiB") |
| 84 | + .indexes(indexes) |
| 85 | + .build(); |
| 86 | + |
| 87 | + assertThat(request.getUrl(), is(equalTo("http://localhost:7711"))); |
| 88 | + assertThat(request.getApiKey(), is(equalTo("mykey"))); |
| 89 | + assertThat(request.getPayloadSize(), is(equalTo("50 MiB"))); |
| 90 | + assertThat(request.getIndexes().get("myindex").getFilter(), is(equalTo("id > 10"))); |
| 91 | + assertThat(request.getIndexes().get("myindex").isOverrideSettings(), is(false)); |
| 92 | + } |
| 93 | +} |
0 commit comments