66
77import com .meilisearch .integration .classes .AbstractIT ;
88import com .meilisearch .sdk .exceptions .MeilisearchException ;
9+ import com .meilisearch .sdk .json .GsonJsonHandler ;
910import com .meilisearch .sdk .model .DynamicSearchRule ;
1011import com .meilisearch .sdk .model .DynamicSearchRulesQuery ;
1112import com .meilisearch .sdk .model .Results ;
1213import com .meilisearch .sdk .model .TaskInfo ;
1314import java .util .Collections ;
1415import java .util .List ;
1516import java .util .Map ;
17+ import org .json .JSONObject ;
1618import org .junit .jupiter .api .AfterAll ;
1719import org .junit .jupiter .api .BeforeEach ;
1820import org .junit .jupiter .api .Tag ;
@@ -33,14 +35,17 @@ static void cleanMeilisearch() {
3335 }
3436
3537 private DynamicSearchRule buildTestRule (String uid ) {
36- Map <String , Object > queryCondition = Map .of ("scope" , " query" , "isEmpty" , true );
38+ Map <String , Object > queryCondition = Map .of ("query" , Map . of ( "isEmpty" , true ) );
3739 Map <String , Object > action =
3840 Map .of (
3941 "selector" , Map .of ("indexUid" , "products" ),
4042 "action" , Map .of ("type" , "pin" , "position" , 1 ));
4143
42- return new DynamicSearchRule (
43- uid , "Test rule" , 5 , true , List .of (queryCondition ), List .of (action ));
44+ return new DynamicSearchRule (uid , "Test rule" , 5 , true , queryCondition , List .of (action ));
45+ }
46+
47+ private Map <String , Object > buildFilterCondition (String color ) {
48+ return Map .of ("filter" , Map .of ("values" , Map .of ("color" , color , "category" , "shirt" )));
4449 }
4550
4651 @ Test
@@ -53,8 +58,44 @@ public void testUpdateDynamicSearchRule() throws MeilisearchException {
5358 DynamicSearchRule fetched = client .getDynamicSearchRule ("test-rule" );
5459 assertThat (fetched .getUid (), equalTo ("test-rule" ));
5560 assertThat (fetched .getDescription (), equalTo ("Test rule" ));
56- assertThat (fetched .getPriority (), equalTo (5 ));
61+ assertThat (fetched .getPrecedence (), equalTo (5 ));
5762 assertThat (fetched .isActive (), equalTo (true ));
63+ assertThat (fetched .getLastUpdatedAt (), notNullValue ());
64+ }
65+
66+ @ Test
67+ public void testCreateAndUpdateDynamicSearchRuleWithFilterCondition ()
68+ throws MeilisearchException {
69+ DynamicSearchRule rule = buildTestRule ("test-filter" );
70+ rule .setConditions (buildFilterCondition ("red" ));
71+
72+ TaskInfo createTask = client .updateDynamicSearchRule ("test-filter" , rule );
73+ client .waitForTask (createTask .getTaskUid ());
74+
75+ DynamicSearchRule created = client .getDynamicSearchRule ("test-filter" );
76+ JSONObject createdConditions =
77+ new JSONObject (new GsonJsonHandler ().encode (created .getConditions ()));
78+ assertThat (
79+ createdConditions
80+ .getJSONObject ("filter" )
81+ .getJSONObject ("values" )
82+ .getString ("color" ),
83+ equalTo ("red" ));
84+
85+ rule .setConditions (buildFilterCondition ("blue" ));
86+ TaskInfo updateTask = client .updateDynamicSearchRule ("test-filter" , rule );
87+ client .waitForTask (updateTask .getTaskUid ());
88+
89+ DynamicSearchRule updated = client .getDynamicSearchRule ("test-filter" );
90+ JSONObject updatedConditions =
91+ new JSONObject (new GsonJsonHandler ().encode (updated .getConditions ()));
92+ assertThat (
93+ updatedConditions
94+ .getJSONObject ("filter" )
95+ .getJSONObject ("values" )
96+ .getString ("color" ),
97+ equalTo ("blue" ));
98+ assertThat (updated .getLastUpdatedAt (), notNullValue ());
5899 }
59100
60101 @ Test
@@ -79,6 +120,7 @@ public void testListDynamicSearchRules() throws MeilisearchException {
79120 client .listDynamicSearchRules (new DynamicSearchRulesQuery ());
80121
81122 assertThat (results .getResults ().length >= 2 , equalTo (true ));
123+ assertThat (results .getResults ()[0 ].getLastUpdatedAt (), notNullValue ());
82124 }
83125
84126 @ Test
0 commit comments