github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchutil/json_reader_integration_test.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // 3 // The OpenSearch Contributors require contributions made to 4 // this file be licensed under the Apache-2.0 license or a 5 // compatible open source license. 6 // 7 // Modifications Copyright OpenSearch Contributors. See 8 // GitHub history for details. 9 10 // Licensed to Elasticsearch B.V. under one or more contributor 11 // license agreements. See the NOTICE file distributed with 12 // this work for additional information regarding copyright 13 // ownership. Elasticsearch B.V. licenses this file to you under 14 // the Apache License, Version 2.0 (the "License"); you may 15 // not use this file except in compliance with the License. 16 // You may obtain a copy of the License at 17 // 18 // http://www.apache.org/licenses/LICENSE-2.0 19 // 20 // Unless required by applicable law or agreed to in writing, 21 // software distributed under the License is distributed on an 22 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 23 // KIND, either express or implied. See the License for the 24 // specific language governing permissions and limitations 25 // under the License. 26 27 // +build integration 28 29 package opensearchutil_test 30 31 import ( 32 "strings" 33 "testing" 34 35 "github.com/opensearch-project/opensearch-go/v2" 36 "github.com/opensearch-project/opensearch-go/v2/opensearchapi" 37 "github.com/opensearch-project/opensearch-go/v2/opensearchutil" 38 ) 39 40 func TestJSONReaderIntegration(t *testing.T) { 41 t.Run("Index and search", func(t *testing.T) { 42 var ( 43 res *opensearchapi.Response 44 err error 45 ) 46 47 client, err := opensearch.NewDefaultClient() 48 if err != nil { 49 t.Fatalf("Error creating the client: %s\n", err) 50 } 51 52 client.Indices.Delete([]string{"test"}, client.Indices.Delete.WithIgnoreUnavailable(true)) 53 54 doc := struct { 55 Title string `json:"title"` 56 }{Title: "Foo Bar"} 57 58 res, err = client.Index("test", opensearchutil.NewJSONReader(&doc), client.Index.WithRefresh("true")) 59 if err != nil { 60 t.Fatalf("Error getting response: %s", err) 61 } 62 defer res.Body.Close() 63 64 if res.IsError() { 65 t.Fatalf("Error response: %s", res.String()) 66 } 67 68 query := map[string]interface{}{ 69 "query": map[string]interface{}{ 70 "match": map[string]interface{}{ 71 "title": "foo", 72 }, 73 }, 74 } 75 76 res, err = client.Search( 77 client.Search.WithIndex("test"), 78 client.Search.WithBody(opensearchutil.NewJSONReader(&query)), 79 client.Search.WithPretty(), 80 ) 81 if err != nil { 82 t.Fatalf("Error getting response: %s", err) 83 } 84 defer res.Body.Close() 85 86 if res.IsError() { 87 t.Errorf("Error response: %s", res) 88 } 89 90 if !strings.Contains(res.String(), "Foo Bar") { 91 t.Errorf("Unexpected response: %s", res) 92 } 93 }) 94 }