Browse Source

[Connectors API] Extend tests for update connector filtering (#105697)

Tim Grein 1 năm trước cách đây
mục cha
commit
d26214f4f3

+ 177 - 1
x-pack/plugin/ent-search/qa/rest/src/yamlRestTest/resources/rest-api-spec/test/entsearch/332_connector_update_filtering.yml

@@ -13,7 +13,7 @@ setup:
           is_native: false
           service_type: super-connector
 ---
-"Update Connector Filtering":
+"Update Connector Filtering with advanced snippet value array":
   - do:
       connector.update_filtering:
         connector_id: test-connector
@@ -107,6 +107,7 @@ setup:
 
   - match: { filtering.0.domain: DEFAULT }
   - match: { filtering.0.active.advanced_snippet.created_at: "2023-05-25T12:30:00.000Z" }
+  - match: { filtering.0.active.advanced_snippet.value.0.tables.0.: "some_table" }
   - match: { filtering.0.active.rules.0.id: "RULE-ACTIVE-0" }
   - match: { filtering.0.draft.rules.0.id: "RULE-DRAFT-0" }
 
@@ -115,6 +116,181 @@ setup:
   - match: { filtering.1.active.rules.0.id: "RULE-ACTIVE-1" }
   - match: { filtering.1.draft.rules.0.id: "RULE-DRAFT-1" }
 
+---
+"Update Connector Filtering with advanced snippet value object":
+  - do:
+      connector.update_filtering:
+        connector_id: test-connector
+        body:
+          filtering:
+            - active:
+                advanced_snippet:
+                  created_at: "2023-05-25T12:30:00.000Z"
+                  updated_at: "2023-05-25T12:30:00.000Z"
+                  value:
+                    some_filtering_key: "some_filtering_value"
+                rules:
+                  - created_at: "2023-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-ACTIVE-0
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2023-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+              domain: DEFAULT
+              draft:
+                advanced_snippet:
+                  created_at: "2023-05-25T12:30:00.000Z"
+                  updated_at: "2023-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2023-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-DRAFT-0
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2023-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+            - active:
+                advanced_snippet:
+                  created_at: "2021-05-25T12:30:00.000Z"
+                  updated_at: "2021-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2021-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-ACTIVE-1
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2021-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+              domain: TEST
+              draft:
+                advanced_snippet:
+                  created_at: "2021-05-25T12:30:00.000Z"
+                  updated_at: "2021-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2021-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-DRAFT-1
+                    order: 0
+                    policy: exclude
+                    rule: regex
+                    updated_at: "2021-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+
+  - match: { result: updated }
+
+  - do:
+      connector.get:
+        connector_id: test-connector
+
+  - match: { filtering.0.domain: DEFAULT }
+  - match: { filtering.0.active.advanced_snippet.created_at: "2023-05-25T12:30:00.000Z" }
+  - match: { filtering.0.active.advanced_snippet.value.some_filtering_key: "some_filtering_value" }
+  - match: { filtering.0.active.rules.0.id: "RULE-ACTIVE-0" }
+  - match: { filtering.0.draft.rules.0.id: "RULE-DRAFT-0" }
+
+  - match: { filtering.1.domain: TEST }
+  - match: { filtering.1.active.advanced_snippet.created_at: "2021-05-25T12:30:00.000Z" }
+  - match: { filtering.1.active.rules.0.id: "RULE-ACTIVE-1" }
+  - match: { filtering.1.draft.rules.0.id: "RULE-DRAFT-1" }
+
+---
+"Update Connector Filtering with value literal - Wrong advanced snippet value":
+  - do:
+      catch: "bad_request"
+      connector.update_filtering:
+        connector_id: test-connector
+        body:
+          filtering:
+            - active:
+                advanced_snippet:
+                  created_at: "2023-05-25T12:30:00.000Z"
+                  updated_at: "2023-05-25T12:30:00.000Z"
+                  value: "string literal"
+                rules:
+                  - created_at: "2023-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-ACTIVE-0
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2023-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+              domain: DEFAULT
+              draft:
+                advanced_snippet:
+                  created_at: "2023-05-25T12:30:00.000Z"
+                  updated_at: "2023-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2023-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-DRAFT-0
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2023-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+            - active:
+                advanced_snippet:
+                  created_at: "2021-05-25T12:30:00.000Z"
+                  updated_at: "2021-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2021-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-ACTIVE-1
+                    order: 0
+                    policy: include
+                    rule: regex
+                    updated_at: "2021-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+              domain: TEST
+              draft:
+                advanced_snippet:
+                  created_at: "2021-05-25T12:30:00.000Z"
+                  updated_at: "2021-05-25T12:30:00.000Z"
+                  value: {}
+                rules:
+                  - created_at: "2021-05-25T12:30:00.000Z"
+                    field: _
+                    id: RULE-DRAFT-1
+                    order: 0
+                    policy: exclude
+                    rule: regex
+                    updated_at: "2021-05-25T12:30:00.000Z"
+                    value: ".*"
+                validation:
+                  errors: []
+                  state: valid
+
 ---
 "Update Connector Filtering - Connector doesn't exist":
   - do:

+ 125 - 1
x-pack/plugin/ent-search/src/test/java/org/elasticsearch/xpack/application/connector/ConnectorFilteringTests.java

@@ -15,6 +15,7 @@ import org.elasticsearch.common.xcontent.XContentHelper;
 import org.elasticsearch.search.SearchModule;
 import org.elasticsearch.test.ESTestCase;
 import org.elasticsearch.xcontent.ToXContent;
+import org.elasticsearch.xcontent.XContentParseException;
 import org.elasticsearch.xcontent.XContentParser;
 import org.elasticsearch.xcontent.XContentType;
 import org.junit.Before;
@@ -110,7 +111,7 @@ public class ConnectorFilteringTests extends ESTestCase {
 
     }
 
-    public void testToXContent_WithAdvancedSnippetPopulated() throws IOException {
+    public void testToXContent_WithAdvancedSnippetPopulatedWithAValueArray() throws IOException {
         String content = XContentHelper.stripWhitespace("""
                 {
                     "active": {
@@ -177,6 +178,129 @@ public class ConnectorFilteringTests extends ESTestCase {
 
     }
 
+    public void testToXContent_WithAdvancedSnippetPopulatedWithAValueObject() throws IOException {
+        String content = XContentHelper.stripWhitespace("""
+                {
+                    "active": {
+                        "advanced_snippet": {
+                            "created_at": "2023-11-09T15:13:08.231Z",
+                            "updated_at": "2023-11-09T15:13:08.231Z",
+                            "value": {
+                                    "service": "Incident",
+                                    "query": "user_nameSTARTSWITHa"
+                            }
+                        },
+                        "rules": [
+                            {
+                                "created_at": "2023-11-09T15:13:08.231Z",
+                                "field": "_",
+                                "id": "DEFAULT",
+                                "order": 0,
+                                "policy": "include",
+                                "rule": "regex",
+                                "updated_at": "2023-11-09T15:13:08.231Z",
+                                "value": ".*"
+                            }
+                        ],
+                        "validation": {
+                            "errors": [],
+                            "state": "valid"
+                        }
+                    },
+                    "domain": "DEFAULT",
+                    "draft": {
+                        "advanced_snippet": {
+                            "created_at": "2023-11-09T15:13:08.231Z",
+                            "updated_at": "2023-11-09T15:13:08.231Z",
+                            "value": {}
+                        },
+                        "rules": [
+                            {
+                                "created_at": "2023-11-09T15:13:08.231Z",
+                                "field": "_",
+                                "id": "DEFAULT",
+                                "order": 0,
+                                "policy": "include",
+                                "rule": "regex",
+                                "updated_at": "2023-11-09T15:13:08.231Z",
+                                "value": ".*"
+                            }
+                        ],
+                        "validation": {
+                            "errors": [],
+                            "state": "valid"
+                        }
+                    }
+                }
+            """);
+
+        ConnectorFiltering filtering = ConnectorFiltering.fromXContentBytes(new BytesArray(content), XContentType.JSON);
+        boolean humanReadable = true;
+        BytesReference originalBytes = toShuffledXContent(filtering, XContentType.JSON, ToXContent.EMPTY_PARAMS, humanReadable);
+        ConnectorFiltering parsed;
+        try (XContentParser parser = createParser(XContentType.JSON.xContent(), originalBytes)) {
+            parsed = ConnectorFiltering.fromXContent(parser);
+        }
+        assertToXContentEquivalent(originalBytes, toXContent(parsed, XContentType.JSON, humanReadable), XContentType.JSON);
+
+    }
+
+    public void testToXContent_WithAdvancedSnippetPopulatedWithAValueLiteral_ExpectParseException() throws IOException {
+        String content = XContentHelper.stripWhitespace("""
+                {
+                    "active": {
+                        "advanced_snippet": {
+                            "created_at": "2023-11-09T15:13:08.231Z",
+                            "updated_at": "2023-11-09T15:13:08.231Z",
+                            "value": "string literal"
+                        },
+                        "rules": [
+                            {
+                                "created_at": "2023-11-09T15:13:08.231Z",
+                                "field": "_",
+                                "id": "DEFAULT",
+                                "order": 0,
+                                "policy": "include",
+                                "rule": "regex",
+                                "updated_at": "2023-11-09T15:13:08.231Z",
+                                "value": ".*"
+                            }
+                        ],
+                        "validation": {
+                            "errors": [],
+                            "state": "valid"
+                        }
+                    },
+                    "domain": "DEFAULT",
+                    "draft": {
+                        "advanced_snippet": {
+                            "created_at": "2023-11-09T15:13:08.231Z",
+                            "updated_at": "2023-11-09T15:13:08.231Z",
+                            "value": {}
+                        },
+                        "rules": [
+                            {
+                                "created_at": "2023-11-09T15:13:08.231Z",
+                                "field": "_",
+                                "id": "DEFAULT",
+                                "order": 0,
+                                "policy": "include",
+                                "rule": "regex",
+                                "updated_at": "2023-11-09T15:13:08.231Z",
+                                "value": ".*"
+                            }
+                        ],
+                        "validation": {
+                            "errors": [],
+                            "state": "valid"
+                        }
+                    }
+                }
+            """);
+
+        assertThrows(XContentParseException.class, () -> ConnectorFiltering.fromXContentBytes(new BytesArray(content), XContentType.JSON));
+    }
+
     private void assertTransportSerialization(ConnectorFiltering testInstance) throws IOException {
         ConnectorFiltering deserializedInstance = copyInstance(testInstance);
         assertNotSame(testInstance, deserializedInstance);