From d99556f466fc02337b381610ed7378450decbbd3 Mon Sep 17 00:00:00 2001 From: Cihat Keser Date: Sun, 18 Jan 2015 21:03:49 +0100 Subject: [PATCH] removed exception handling in some older test cases --- .../searchbox/core/BulkIntegrationTest.java | 356 ++++++++---------- .../searchbox/core/CountIntegrationTest.java | 68 ++-- .../io/searchbox/core/GetIntegrationTest.java | 33 +- .../indices/PutMappingIntegrationTest.java | 11 +- 4 files changed, 200 insertions(+), 268 deletions(-) diff --git a/jest/src/test/java/io/searchbox/core/BulkIntegrationTest.java b/jest/src/test/java/io/searchbox/core/BulkIntegrationTest.java index 99154c2b7..4b2d719f0 100644 --- a/jest/src/test/java/io/searchbox/core/BulkIntegrationTest.java +++ b/jest/src/test/java/io/searchbox/core/BulkIntegrationTest.java @@ -64,244 +64,192 @@ public void bulkOperationWithCustomGson() throws Exception { } @Test - public void bulkOperationWithIndex() { - try { - Map source = new HashMap(); - source.put("user", "kimchy"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndex() throws IOException { + Map source = new HashMap(); + source.put("user", "kimchy"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithDefaultIndexAndDefaultType() { - try { - Map source1 = new HashMap(); - source1.put("user name", "kimchy olga john doe"); - - Bulk bulk = new Bulk.Builder() - .defaultIndex("twitter") - .defaultType("tweet") - .addAction(new Index.Builder(source1).id("1").build()) - .build(); - executeTestCase(bulk); - - GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); - assertNotNull(getResponse); - assertEquals(new Gson().toJson(source1), getResponse.getSourceAsString()); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithDefaultIndexAndDefaultType() throws IOException { + Map source1 = new HashMap(); + source1.put("user name", "kimchy olga john doe"); + + Bulk bulk = new Bulk.Builder() + .defaultIndex("twitter") + .defaultType("tweet") + .addAction(new Index.Builder(source1).id("1").build()) + .build(); + executeTestCase(bulk); + + GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); + assertNotNull(getResponse); + assertEquals(new Gson().toJson(source1), getResponse.getSourceAsString()); } @Test - public void bulkOperationWithIndexWithSourceIncludingWhitespace() { - try { - Map source1 = new HashMap(); - source1.put("user name", "kimchy olga john doe"); - - String source2 = "{\"k e y\" : \" val v a l \" }"; - - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build()) - .addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build()) - .build(); - executeTestCase(bulk); - - GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); - assertNotNull(getResponse); - assertEquals(new Gson().toJson(source1), getResponse.getSourceAsString()); - - getResponse = client().get(new GetRequest("twitter", "tweet", "2")).actionGet(); - assertNotNull(getResponse); - assertEquals(source2, getResponse.getSourceAsString()); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexWithSourceIncludingWhitespace() throws IOException { + Map source1 = new HashMap(); + source1.put("user name", "kimchy olga john doe"); + + String source2 = "{\"k e y\" : \" val v a l \" }"; + + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build()) + .addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build()) + .build(); + executeTestCase(bulk); + + GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); + assertNotNull(getResponse); + assertEquals(new Gson().toJson(source1), getResponse.getSourceAsString()); + + getResponse = client().get(new GetRequest("twitter", "tweet", "2")).actionGet(); + assertNotNull(getResponse); + assertEquals(source2, getResponse.getSourceAsString()); } @Test - public void bulkOperationWithIndexWithSourceIncludingLineBreak() { - try { - Map source1 = new HashMap(); - source1.put("user name", "kimchy\nolga\njohn doe"); - - String source2 = "{\"k e y\" : \" val\nv a\r\nl \" }"; - - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build()) - .addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build()) - .build(); - - JestResult result = client.execute(bulk); - assertFalse(result.isSucceeded()); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexWithSourceIncludingLineBreak() throws IOException { + Map source1 = new HashMap(); + source1.put("user name", "kimchy\nolga\njohn doe"); + + String source2 = "{\"k e y\" : \" val\nv a\r\nl \" }"; + + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source1).index("twitter").type("tweet").id("1").build()) + .addAction(new Index.Builder(source2).index("twitter").type("tweet").id("2").build()) + .build(); + + JestResult result = client.execute(bulk); + assertFalse(result.isSucceeded()); } @Test - public void bulkOperationWithIndexWithParam() { - try { - Map source = new HashMap(); - source.put("user", "kimchy"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source) - .index("twitter") - .type("tweet") - .id("1") - .setParameter(Parameters.VERSION, 6) - .build()) - .build(); - - // should fail because version 6 does not exist yet - JestResult result = client.execute(bulk); - assertTrue(result.getErrorMessage(), result.isSucceeded()); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexWithParam() throws IOException { + Map source = new HashMap(); + source.put("user", "kimchy"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source) + .index("twitter") + .type("tweet") + .id("1") + .setParameter(Parameters.VERSION, 6) + .build()) + .build(); + + // should fail because version 6 does not exist yet + JestResult result = client.execute(bulk); + assertTrue(result.getErrorMessage(), result.isSucceeded()); } @Test - public void bulkOperationWithIndexAndUpdate() { - try { - String script = "{" + - " \"script\" : \"ctx._source.user += tag\"," + - " \"params\" : {" + - " \"tag\" : \"_osman\"" + - " }" + - "}"; - - Map source = new HashMap(); - source.put("user", "kimchy"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .addAction(new Update.Builder(StringUtils.chomp(script)).index("twitter").type("tweet").id("1").build()) - .build(); - executeTestCase(bulk); - - GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); - assertNotNull(getResponse); - assertEquals("{\"user\":\"kimchy_osman\"}", getResponse.getSourceAsString()); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexAndUpdate() throws IOException { + String script = "{" + + " \"script\" : \"ctx._source.user += tag\"," + + " \"params\" : {" + + " \"tag\" : \"_osman\"" + + " }" + + "}"; + + Map source = new HashMap(); + source.put("user", "kimchy"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .addAction(new Update.Builder(StringUtils.chomp(script)).index("twitter").type("tweet").id("1").build()) + .build(); + executeTestCase(bulk); + + GetResponse getResponse = client().get(new GetRequest("twitter", "tweet", "1")).actionGet(); + assertNotNull(getResponse); + assertEquals("{\"user\":\"kimchy_osman\"}", getResponse.getSourceAsString()); } @Test - public void bulkOperationWithIndexJsonSource() { - try { - String source = "{\"user\":\"super\"}"; - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexJsonSource() throws IOException { + String source = "{\"user\":\"super\"}"; + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithSingleDelete() { - try { - Bulk bulk = new Bulk.Builder() - .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithSingleDelete() throws IOException { + Bulk bulk = new Bulk.Builder() + .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithMultipleIndex() { - try { - Map source = new HashMap(); - source.put("user", "kimcy"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithMultipleIndex() throws IOException { + Map source = new HashMap(); + source.put("user", "kimcy"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithIndexCreateOpType() { - try { - Map source = new HashMap(); - source.put("user", "kimcy"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .addAction(new Index.Builder(source) - .index("twitter").type("tweet").id("1").setParameter(Parameters.OP_TYPE, "create").build()) - .build(); - JestResult result = executeTestCase(bulk); - - // second index request with create op type should fail because it's a duplicate of the first index request - assertNotNull( - result.getJsonObject().getAsJsonArray("items").get(1).getAsJsonObject().getAsJsonObject("create").get("error").getAsString() - ); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithIndexCreateOpType() throws IOException { + Map source = new HashMap(); + source.put("user", "kimcy"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .addAction(new Index.Builder(source) + .index("twitter").type("tweet").id("1").setParameter(Parameters.OP_TYPE, "create").build()) + .build(); + JestResult result = executeTestCase(bulk); + + // second index request with create op type should fail because it's a duplicate of the first index request + assertNotNull( + result.getJsonObject().getAsJsonArray("items").get(1).getAsJsonObject().getAsJsonObject("create").get("error").getAsString() + ); } @Test - public void bulkOperationWithMultipleDelete() { - try { - Bulk bulk = new Bulk.Builder() - .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) - .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithMultipleDelete() throws IOException { + Bulk bulk = new Bulk.Builder() + .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) + .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithMultipleIndexAndDelete() { - try { - Map source = new HashMap(); - source.put("field", "value"); - Bulk bulk = new Bulk.Builder() - .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) - .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) - .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) - .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) - .build(); - executeTestCase(bulk); - } catch (IOException e) { - fail("Failed during the bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithMultipleIndexAndDelete() throws IOException { + Map source = new HashMap(); + source.put("field", "value"); + Bulk bulk = new Bulk.Builder() + .addAction(new Index.Builder(source).index("twitter").type("tweet").id("1").build()) + .addAction(new Index.Builder(source).index("elasticsearch").type("jest").id("2").build()) + .addAction(new Delete.Builder("1").index("twitter").type("tweet").build()) + .addAction(new Delete.Builder("2").index("twitter").type("tweet").build()) + .build(); + executeTestCase(bulk); } @Test - public void bulkOperationWithSourceList() { - try { - TestArticleModel model1 = new TestArticleModel("tweet1"); - TestArticleModel model2 = new TestArticleModel("2", "tweet2"); - List modelList = Arrays.asList( - new Index.Builder(model1).build(), - new Index.Builder(model2).build() - ); - - Bulk bulk = new Bulk.Builder() - .defaultIndex("twitter") - .defaultType("tweet") - .addAction(modelList) - .build(); - executeTestCase(bulk); - } catch (Exception e) { - fail("Failed during bulk operation Exception:" + e.getMessage()); - } + public void bulkOperationWithSourceList() throws IOException { + TestArticleModel model1 = new TestArticleModel("tweet1"); + TestArticleModel model2 = new TestArticleModel("2", "tweet2"); + List modelList = Arrays.asList( + new Index.Builder(model1).build(), + new Index.Builder(model2).build() + ); + + Bulk bulk = new Bulk.Builder() + .defaultIndex("twitter") + .defaultType("tweet") + .addAction(modelList) + .build(); + executeTestCase(bulk); } private JestResult executeTestCase(Action action) throws RuntimeException, IOException { @@ -309,10 +257,10 @@ private JestResult executeTestCase(Action action) throws RuntimeException, IOExc assertTrue(result.getErrorMessage(), result.isSucceeded()); ((List) result.getValue("items")).get(0); if ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("index") != null) { - assertEquals("twitter",((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("index")).get("_index")); + assertEquals("twitter", ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("index")).get("_index")); } if ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("delete") != null) { - assertEquals("twitter",((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("delete")).get("_index")); + assertEquals("twitter", ((Map) ((Map) ((Map) ((Map) ((List) result.getValue("items")).get(0)))).get("delete")).get("_index")); } return result; } diff --git a/jest/src/test/java/io/searchbox/core/CountIntegrationTest.java b/jest/src/test/java/io/searchbox/core/CountIntegrationTest.java index 0fe06f5f0..2a2fbe44d 100644 --- a/jest/src/test/java/io/searchbox/core/CountIntegrationTest.java +++ b/jest/src/test/java/io/searchbox/core/CountIntegrationTest.java @@ -6,6 +6,8 @@ import org.junit.FixMethodOrder; import org.junit.Test; +import java.io.IOException; + /** * @author Dogukan Sonmez * @author cihat keser @@ -22,71 +24,59 @@ public void setup() { } @Test - public void countWithMultipleIndices() { + public void countWithMultipleIndices() throws IOException { String query = "{\n" + " \"query\" : {\n" + " \"term\" : { \"user\" : \"kimchy\" }\n" + " }\n" + "}"; - try { - CountResult result = client.execute(new Count.Builder() - .query(query) - .addIndex("cvbank") - .addIndex("office_docs") - .build()); - assertTrue(result.getErrorMessage(), result.isSucceeded()); - assertEquals(0.0, result.getCount(), DELTA); - } catch (Exception e) { - fail("Failed during the delete index with valid parameters. Exception:%s" + e.getMessage()); - } + CountResult result = client.execute(new Count.Builder() + .query(query) + .addIndex("cvbank") + .addIndex("office_docs") + .build()); + assertTrue(result.getErrorMessage(), result.isSucceeded()); + assertEquals(0.0, result.getCount(), DELTA); } @Test - public void countWithValidTermQuery1() { + public void countWithValidTermQuery1() throws IOException { String query = "{\n" + " \"query\" : {\n" + " \"term\" : { \"user\" : \"kimchy\" }\n" + " }\n" + "}"; - try { - CountResult result = client.execute(new Count.Builder().query(query).build()); - assertTrue(result.getErrorMessage(), result.isSucceeded()); - assertEquals(0.0, result.getCount(), DELTA); - } catch (Exception e) { - fail("Failed during the delete index with valid parameters. Exception:%s" + e.getMessage()); - } + CountResult result = client.execute(new Count.Builder().query(query).build()); + assertTrue(result.getErrorMessage(), result.isSucceeded()); + assertEquals(0.0, result.getCount(), DELTA); } @Test - public void countWithValidTermQuery2() { + public void countWithValidTermQuery2() throws IOException { String query = "{\n" + " \"query\" : {\n" + " \"term\" : { \"user\" : \"kimchy\" }\n" + " }\n" + "}"; - try { - Index index = new Index.Builder("{ \"user\":\"kimchy\" }") - .index("cvbank") - .type("candidate") - .refresh(true) - .build(); - client.execute(index); + Index index = new Index.Builder("{ \"user\":\"kimchy\" }") + .index("cvbank") + .type("candidate") + .refresh(true) + .build(); + client.execute(index); - Count count = new Count.Builder() - .query(query) - .addIndex("cvbank") - .addType("candidate") - .build(); + Count count = new Count.Builder() + .query(query) + .addIndex("cvbank") + .addType("candidate") + .build(); - CountResult result = client.execute(count); - assertTrue(result.getErrorMessage(), result.isSucceeded()); - assertEquals(1.0, result.getCount(), DELTA); - } catch (Exception e) { - fail("Failed during the delete index with valid parameters. Exception:" + e.getMessage()); - } + CountResult result = client.execute(count); + assertTrue(result.getErrorMessage(), result.isSucceeded()); + assertEquals(1.0, result.getCount(), DELTA); } } diff --git a/jest/src/test/java/io/searchbox/core/GetIntegrationTest.java b/jest/src/test/java/io/searchbox/core/GetIntegrationTest.java index 7ad990d18..ac6090712 100644 --- a/jest/src/test/java/io/searchbox/core/GetIntegrationTest.java +++ b/jest/src/test/java/io/searchbox/core/GetIntegrationTest.java @@ -10,6 +10,7 @@ import org.junit.Test; import java.io.IOException; +import java.util.concurrent.ExecutionException; /** * @author Dogukan Sonmez @@ -40,8 +41,8 @@ public void getWithSpecialCharacterInDocId() throws IOException { assertNotNull(indexResponse); JestResult result = client.execute(new Get.Builder("twitter", "asd/qwe") - .type("tweet") - .build() + .type("tweet") + .build() ); assertTrue(result.getErrorMessage(), result.isSucceeded()); } @@ -54,22 +55,18 @@ public void get() throws IOException { } @Test - public void getAsynchronously() { - try { - client.executeAsync(new Get.Builder("twitter", "1").type("tweet").build(), new JestResultHandler() { - @Override - public void completed(JestResult result) { - assertTrue(result.getErrorMessage(), result.isSucceeded()); - } - - @Override - public void failed(Exception ex) { - fail("failed execution of asynchronous get call"); - } - }); - } catch (Exception e) { - fail("Failed during the getting index with valid parameters. Exception:%s" + e.getMessage()); - } + public void getAsynchronously() throws InterruptedException, ExecutionException, IOException { + client.executeAsync(new Get.Builder("twitter", "1").type("tweet").build(), new JestResultHandler() { + @Override + public void completed(JestResult result) { + assertTrue(result.getErrorMessage(), result.isSucceeded()); + } + + @Override + public void failed(Exception ex) { + fail("failed execution of asynchronous get call"); + } + }); //wait for asynchronous call try { diff --git a/jest/src/test/java/io/searchbox/indices/PutMappingIntegrationTest.java b/jest/src/test/java/io/searchbox/indices/PutMappingIntegrationTest.java index 0c8afde32..943d40d6b 100644 --- a/jest/src/test/java/io/searchbox/indices/PutMappingIntegrationTest.java +++ b/jest/src/test/java/io/searchbox/indices/PutMappingIntegrationTest.java @@ -29,18 +29,15 @@ public void setup() { } @Test - public void testPutMapping() { + public void testPutMapping() throws IOException { PutMapping putMapping = new PutMapping.Builder( INDEX_NAME, INDEX_TYPE, "{ \"document\" : { \"properties\" : { \"message\" : {\"type\" : \"string\", \"store\" : \"yes\"} } } }" ).build(); - try { - JestResult result = client.execute(putMapping); - assertTrue(result.getErrorMessage(), result.isSucceeded()); - } catch (IOException e) { - fail("Test failed while executing creating index with default settings"); - } + + JestResult result = client.execute(putMapping); + assertTrue(result.getErrorMessage(), result.isSucceeded()); } @Test