11package com .baeldung .elasticsearch ;
22
3- import com .alibaba .fastjson .JSON ;
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .io .IOException ;
6+ import java .net .InetAddress ;
7+ import java .net .UnknownHostException ;
8+ import java .util .ArrayList ;
9+ import java .util .Arrays ;
10+ import java .util .Date ;
11+ import java .util .List ;
12+ import java .util .stream .Collectors ;
13+
14+ import org .elasticsearch .action .DocWriteResponse .Result ;
415import org .elasticsearch .action .delete .DeleteResponse ;
516import org .elasticsearch .action .index .IndexResponse ;
617import org .elasticsearch .action .search .SearchResponse ;
718import org .elasticsearch .action .search .SearchType ;
819import org .elasticsearch .client .Client ;
20+ import org .elasticsearch .common .settings .Settings ;
21+ import org .elasticsearch .common .transport .InetSocketTransportAddress ;
922import org .elasticsearch .common .xcontent .XContentBuilder ;
1023import org .elasticsearch .common .xcontent .XContentFactory ;
24+ import org .elasticsearch .common .xcontent .XContentType ;
1125import org .elasticsearch .index .query .QueryBuilders ;
12- import org .elasticsearch .node .Node ;
1326import org .elasticsearch .search .SearchHit ;
27+ import org .elasticsearch .transport .client .PreBuiltTransportClient ;
1428import org .junit .Before ;
1529import org .junit .Test ;
1630
17- import java .io .IOException ;
18- import java .util .ArrayList ;
19- import java .util .Arrays ;
20- import java .util .Date ;
21- import java .util .List ;
22- import java .util .stream .Collectors ;
23-
24- import static org .elasticsearch .node .NodeBuilder .nodeBuilder ;
25- import static org .junit .Assert .assertEquals ;
26- import static org .junit .Assert .assertTrue ;
31+ import com .alibaba .fastjson .JSON ;
2732
2833public class ElasticSearchManualTest {
2934 private List <Person > listOfPersons = new ArrayList <>();
3035 private Client client = null ;
3136
3237 @ Before
33- public void setUp () {
38+ public void setUp () throws UnknownHostException {
3439 Person person1 = new Person (10 , "John Doe" , new Date ());
3540 Person person2 = new Person (25 , "Janette Doe" , new Date ());
3641 listOfPersons .add (person1 );
3742 listOfPersons .add (person2 );
38- Node node = nodeBuilder ()
39- .clusterName ("elasticsearch" )
40- .client (true )
41- .node ();
42- client = node .client ();
43+
44+ client = new PreBuiltTransportClient (Settings .builder ().put ("client.transport.sniff" , true )
45+ .put ("cluster.name" ,"elasticsearch" ).build ())
46+ .addTransportAddress (new InetSocketTransportAddress (InetAddress .getByName ("127.0.0.1" ), 9300 ));
4347 }
4448
4549 @ Test
4650 public void givenJsonString_whenJavaObject_thenIndexDocument () {
4751 String jsonObject = "{\" age\" :20,\" dateOfBirth\" :1471466076564,\" fullName\" :\" John Doe\" }" ;
4852 IndexResponse response = client
4953 .prepareIndex ("people" , "Doe" )
50- .setSource (jsonObject )
54+ .setSource (jsonObject , XContentType . JSON )
5155 .get ();
5256 String index = response .getIndex ();
5357 String type = response .getType ();
54- assertTrue (response .isCreated ());
58+
59+ assertEquals (Result .CREATED , response .getResult ());
5560 assertEquals (index , "people" );
5661 assertEquals (type , "Doe" );
5762 }
@@ -61,13 +66,14 @@ public void givenDocumentId_whenJavaObject_thenDeleteDocument() {
6166 String jsonObject = "{\" age\" :10,\" dateOfBirth\" :1471455886564,\" fullName\" :\" Johan Doe\" }" ;
6267 IndexResponse response = client
6368 .prepareIndex ("people" , "Doe" )
64- .setSource (jsonObject )
69+ .setSource (jsonObject , XContentType . JSON )
6570 .get ();
6671 String id = response .getId ();
6772 DeleteResponse deleteResponse = client
6873 .prepareDelete ("people" , "Doe" , id )
6974 .get ();
70- assertTrue (deleteResponse .isFound ());
75+
76+ assertEquals (Result .DELETED ,deleteResponse .getResult ());
7177 }
7278
7379 @ Test
@@ -142,6 +148,7 @@ public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException {
142148 .prepareIndex ("people" , "Doe" )
143149 .setSource (builder )
144150 .get ();
145- assertTrue (response .isCreated ());
151+
152+ assertEquals (Result .CREATED , response .getResult ());
146153 }
147154}
0 commit comments