|
| 1 | +package org.typesense.api; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.AfterEach; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 7 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 8 | +import org.junit.jupiter.api.BeforeEach; |
| 9 | +import org.junit.jupiter.api.Test; |
| 10 | +import org.typesense.model.AnalyticsRuleDeleteResponse; |
| 11 | +import org.typesense.model.AnalyticsRuleParameters; |
| 12 | +import org.typesense.model.AnalyticsRuleParametersDestination; |
| 13 | +import org.typesense.model.AnalyticsRuleParametersSource; |
| 14 | +import org.typesense.model.AnalyticsRuleSchema; |
| 15 | +import org.typesense.model.AnalyticsRuleUpsertSchema; |
| 16 | +import org.typesense.model.AnalyticsRulesRetrieveSchema; |
| 17 | + |
| 18 | +public class AnalyticsRulesTest { |
| 19 | + |
| 20 | + private Client client; |
| 21 | + private Helper helper; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + void setUp() throws Exception { |
| 25 | + helper = new Helper(); |
| 26 | + client = helper.getClient(); |
| 27 | + helper.teardown(); |
| 28 | + helper.createTestCollection(); |
| 29 | + helper.createTestQueryCollection(); |
| 30 | + } |
| 31 | + |
| 32 | + @AfterEach |
| 33 | + void tearDown() throws Exception { |
| 34 | + helper.teardown(); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + void testCreate() throws Exception { |
| 39 | + AnalyticsRuleSchema analyticsRuleSchema = new AnalyticsRuleSchema(); |
| 40 | + analyticsRuleSchema.setName("nohits-queries"); |
| 41 | + analyticsRuleSchema.setType(AnalyticsRuleSchema.TypeEnum.NOHITS_QUERIES); |
| 42 | + analyticsRuleSchema.setParams(new AnalyticsRuleParameters() |
| 43 | + .source(new AnalyticsRuleParametersSource() |
| 44 | + .collections(Arrays.asList("books"))) |
| 45 | + .destination(new AnalyticsRuleParametersDestination() |
| 46 | + .collection("queries"))); |
| 47 | + |
| 48 | + AnalyticsRuleSchema result = this.client.analytics().rules().create(analyticsRuleSchema); |
| 49 | + assertNotNull(result); |
| 50 | + assertEquals("nohits-queries", result.getName()); |
| 51 | + |
| 52 | + AnalyticsRuleParameters params = result.getParams(); |
| 53 | + assertNotNull(params); |
| 54 | + |
| 55 | + assertNotNull(params.getSource()); |
| 56 | + assertEquals(Arrays.asList("books"), params.getSource().getCollections()); |
| 57 | + |
| 58 | + assertNotNull(params.getDestination()); |
| 59 | + assertEquals("queries", params.getDestination().getCollection()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testUpsert() throws Exception { |
| 64 | + AnalyticsRuleUpsertSchema analyticsRuleSchema = new AnalyticsRuleUpsertSchema() |
| 65 | + .type(AnalyticsRuleUpsertSchema.TypeEnum.NOHITS_QUERIES) |
| 66 | + .params(new AnalyticsRuleParameters() |
| 67 | + .source(new AnalyticsRuleParametersSource() |
| 68 | + .collections(Arrays.asList("books"))) |
| 69 | + .destination(new AnalyticsRuleParametersDestination() |
| 70 | + .collection("queries"))); |
| 71 | + |
| 72 | + AnalyticsRuleSchema result = this.client.analytics().rules().upsert("nohits-queries", analyticsRuleSchema); |
| 73 | + assertNotNull(result); |
| 74 | + assertEquals("nohits-queries", result.getName()); |
| 75 | + |
| 76 | + AnalyticsRuleParameters params = result.getParams(); |
| 77 | + assertNotNull(params); |
| 78 | + |
| 79 | + assertNotNull(params.getSource()); |
| 80 | + assertEquals(Arrays.asList("books"), params.getSource().getCollections()); |
| 81 | + |
| 82 | + assertNotNull(params.getDestination()); |
| 83 | + assertEquals("queries", params.getDestination().getCollection()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void testRetrieve() throws Exception { |
| 88 | + helper.createTestAnalyticsRule(); |
| 89 | + AnalyticsRuleSchema result = this.client.analytics().rules("analytics-rule").retrieve(); |
| 90 | + |
| 91 | + assertNotNull(result); |
| 92 | + assertEquals("analytics-rule", result.getName()); |
| 93 | + |
| 94 | + AnalyticsRuleParameters params = result.getParams(); |
| 95 | + assertNotNull(params); |
| 96 | + |
| 97 | + assertNotNull(params.getSource()); |
| 98 | + assertEquals(Arrays.asList("books"), params.getSource().getCollections()); |
| 99 | + |
| 100 | + assertNotNull(params.getDestination()); |
| 101 | + assertEquals("queries", params.getDestination().getCollection()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void testRetrieveAll() throws Exception { |
| 106 | + helper.createTestAnalyticsRule(); |
| 107 | + AnalyticsRulesRetrieveSchema result = this.client.analytics().rules().retrieve(); |
| 108 | + |
| 109 | + assertNotNull(result); |
| 110 | + assertEquals("analytics-rule", result.getRules().get(0).getName()); |
| 111 | + assertEquals(1, result.getRules().size()); |
| 112 | + |
| 113 | + AnalyticsRuleParameters params = result.getRules().get(0).getParams(); |
| 114 | + assertNotNull(params); |
| 115 | + |
| 116 | + assertNotNull(params.getSource()); |
| 117 | + assertEquals(Arrays.asList("books"), params.getSource().getCollections()); |
| 118 | + |
| 119 | + assertNotNull(params.getDestination()); |
| 120 | + assertEquals("queries", params.getDestination().getCollection()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testDelete() throws Exception { |
| 125 | + helper.createTestAnalyticsRule(); |
| 126 | + AnalyticsRuleDeleteResponse result = this.client.analytics().rules("analytics-rule").delete(); |
| 127 | + |
| 128 | + assertNotNull(result); |
| 129 | + assertEquals("analytics-rule", result.getName()); |
| 130 | + } |
| 131 | +} |
0 commit comments