From 0307803316a3c2acc49545a4a235799be321ff75 Mon Sep 17 00:00:00 2001 From: Mohsin Husen Date: Wed, 9 Apr 2014 15:56:35 +0100 Subject: [PATCH] DATAES-66 - Add Ip support to FieldType added test cases for FieldType.Ip --- .../core/DynamicMappingAndSettingTests.java | 121 ++++++++++++++++++ .../elasticsearch/core/facet/LogEntity.java | 18 ++- .../core/facet/LogEntityBuilder.java | 5 + 3 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/springframework/data/elasticsearch/core/DynamicMappingAndSettingTests.java diff --git a/src/test/java/org/springframework/data/elasticsearch/core/DynamicMappingAndSettingTests.java b/src/test/java/org/springframework/data/elasticsearch/core/DynamicMappingAndSettingTests.java new file mode 100644 index 000000000..6fc4b4a40 --- /dev/null +++ b/src/test/java/org/springframework/data/elasticsearch/core/DynamicMappingAndSettingTests.java @@ -0,0 +1,121 @@ +/* + * Copyright 2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.elasticsearch.core; + +import static org.elasticsearch.index.query.QueryBuilders.*; +import static org.hamcrest.Matchers.*; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.*; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.List; + +import org.elasticsearch.action.search.SearchPhaseExecutionException; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.elasticsearch.core.facet.LogEntity; +import org.springframework.data.elasticsearch.core.facet.LogEntityBuilder; +import org.springframework.data.elasticsearch.core.query.IndexQuery; +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; +import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * DynamicMappingAndSettingTests + * + * @author Artur Konczak + * @author Mohsin Husen + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("classpath:elasticsearch-template-test.xml") +public class DynamicMappingAndSettingTests { + + @Autowired + private ElasticsearchTemplate template; + + @Before + public void before() throws ParseException { + template.deleteIndex(LogEntity.class); + template.createIndex(LogEntity.class); + template.putMapping(LogEntity.class); + + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); + IndexQuery indexQuery1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse("2013-10-18 18:01")).code(2) + .ip("10.10.10.1").buildIndex(); + + IndexQuery indexQuery2 = new LogEntityBuilder("2").action("update").date(dateFormatter.parse("2013-10-19 18:02")).code(2) + .ip("10.10.10.2").buildIndex(); + + IndexQuery indexQuery3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse("2013-10-19 18:03")).code(2) + .ip("10.10.10.3").buildIndex(); + + IndexQuery indexQuery4 = new LogEntityBuilder("4").action("update").date(dateFormatter.parse("2013-10-19 18:04")).code(2) + .ip("10.10.10.4").buildIndex(); + + template.bulkIndex(Arrays.asList(indexQuery1, indexQuery2, indexQuery3, indexQuery4)); + template.refresh(LogEntity.class, true); + } + + /* + DATAES-66 + */ + @Test + public void shouldIndexGivenLogEntityWithIPFieldType() throws ParseException { + //when + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(termQuery("ip", "10.10.10.1")).build(); + + List entities = template.queryForList(searchQuery, LogEntity.class); + //then + assertThat(entities, is(notNullValue())); + assertThat(entities.size(), is(1)); + } + + /* + DATAES-66 + */ + @Test(expected = SearchPhaseExecutionException.class) + public void shouldThrowExceptionWhenInvalidIPGivenForSearchQuery() { + //when + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(termQuery("ip", "10.10.10")).build(); + + List entities = template.queryForList(searchQuery, LogEntity.class); + //then + assertThat(entities, is(notNullValue())); + assertThat(entities.size(), is(1)); + } + + /* + DATAES-66 + */ + @Test + public void shouldReturnLogsForGivenIPRanges() { + //when + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(rangeQuery("ip").from("10.10.10.1").to("10.10.10.3")).build(); + + List entities = template.queryForList(searchQuery, LogEntity.class); + //then + assertThat(entities, is(notNullValue())); + assertThat(entities.size(), is(3)); + } +} diff --git a/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntity.java b/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntity.java index 1e9e55873..abe9e179e 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntity.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntity.java @@ -15,6 +15,8 @@ */ package org.springframework.data.elasticsearch.core.facet; +import static org.springframework.data.elasticsearch.annotations.FieldType.*; + import java.text.SimpleDateFormat; import java.util.Date; @@ -22,7 +24,6 @@ import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.DateFormat; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; -import org.springframework.data.elasticsearch.annotations.FieldType; /** * Simple type to test facets @@ -31,7 +32,7 @@ import org.springframework.data.elasticsearch.annotations.FieldType; * @author Mohsin Husen */ -@Document(indexName = "logs", type = "log", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory") +@Document(indexName = "test-log-index", type = "test-log-type", shards = 1, replicas = 0, refreshInterval = "-1", indexStoreType = "memory") public class LogEntity { private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm"); @@ -43,7 +44,10 @@ public class LogEntity { private long sequenceCode; - @Field(type = FieldType.Date, format = DateFormat.basic_date_time) + @Field(type = Ip) + private String ip; + + @Field(type = Date, format = DateFormat.basic_date_time) private Date date; private LogEntity() { @@ -81,6 +85,14 @@ public class LogEntity { this.sequenceCode = sequenceCode; } + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + public Date getDate() { return date; } diff --git a/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntityBuilder.java b/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntityBuilder.java index b92dd772f..65032e4dc 100644 --- a/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntityBuilder.java +++ b/src/test/java/org/springframework/data/elasticsearch/core/facet/LogEntityBuilder.java @@ -49,6 +49,11 @@ public class LogEntityBuilder { return this; } + public LogEntityBuilder ip(String ip) { + result.setIp(ip); + return this; + } + public LogEntity build() { return result; }