|
| 1 | +/* |
| 2 | + * Copyright 2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.core; |
| 17 | + |
| 18 | +import static org.elasticsearch.index.query.QueryBuilders.*; |
| 19 | +import static org.hamcrest.Matchers.*; |
| 20 | +import static org.hamcrest.core.Is.is; |
| 21 | +import static org.junit.Assert.*; |
| 22 | + |
| 23 | +import java.text.ParseException; |
| 24 | +import java.text.SimpleDateFormat; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import org.elasticsearch.action.search.SearchPhaseExecutionException; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.data.elasticsearch.core.facet.LogEntity; |
| 34 | +import org.springframework.data.elasticsearch.core.facet.LogEntityBuilder; |
| 35 | +import org.springframework.data.elasticsearch.core.query.IndexQuery; |
| 36 | +import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; |
| 37 | +import org.springframework.data.elasticsearch.core.query.SearchQuery; |
| 38 | +import org.springframework.test.context.ContextConfiguration; |
| 39 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 40 | + |
| 41 | +/** |
| 42 | + * DynamicMappingAndSettingTests |
| 43 | + * |
| 44 | + * @author Artur Konczak |
| 45 | + * @author Mohsin Husen |
| 46 | + */ |
| 47 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 48 | +@ContextConfiguration("classpath:elasticsearch-template-test.xml") |
| 49 | +public class DynamicMappingAndSettingTests { |
| 50 | + |
| 51 | + @Autowired |
| 52 | + private ElasticsearchTemplate template; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void before() throws ParseException { |
| 56 | + template.deleteIndex(LogEntity.class); |
| 57 | + template.createIndex(LogEntity.class); |
| 58 | + template.putMapping(LogEntity.class); |
| 59 | + |
| 60 | + SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| 61 | + IndexQuery indexQuery1 = new LogEntityBuilder("1").action("update").date(dateFormatter.parse("2013-10-18 18:01")).code(2) |
| 62 | + .ip("10.10.10.1").buildIndex(); |
| 63 | + |
| 64 | + IndexQuery indexQuery2 = new LogEntityBuilder("2").action("update").date(dateFormatter.parse("2013-10-19 18:02")).code(2) |
| 65 | + .ip("10.10.10.2").buildIndex(); |
| 66 | + |
| 67 | + IndexQuery indexQuery3 = new LogEntityBuilder("3").action("update").date(dateFormatter.parse("2013-10-19 18:03")).code(2) |
| 68 | + .ip("10.10.10.3").buildIndex(); |
| 69 | + |
| 70 | + IndexQuery indexQuery4 = new LogEntityBuilder("4").action("update").date(dateFormatter.parse("2013-10-19 18:04")).code(2) |
| 71 | + .ip("10.10.10.4").buildIndex(); |
| 72 | + |
| 73 | + template.bulkIndex(Arrays.asList(indexQuery1, indexQuery2, indexQuery3, indexQuery4)); |
| 74 | + template.refresh(LogEntity.class, true); |
| 75 | + } |
| 76 | + |
| 77 | + /* |
| 78 | + DATAES-66 |
| 79 | + */ |
| 80 | + @Test |
| 81 | + public void shouldIndexGivenLogEntityWithIPFieldType() throws ParseException { |
| 82 | + //when |
| 83 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 84 | + .withQuery(termQuery("ip", "10.10.10.1")).build(); |
| 85 | + |
| 86 | + List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class); |
| 87 | + //then |
| 88 | + assertThat(entities, is(notNullValue())); |
| 89 | + assertThat(entities.size(), is(1)); |
| 90 | + } |
| 91 | + |
| 92 | + /* |
| 93 | + DATAES-66 |
| 94 | + */ |
| 95 | + @Test(expected = SearchPhaseExecutionException.class) |
| 96 | + public void shouldThrowExceptionWhenInvalidIPGivenForSearchQuery() { |
| 97 | + //when |
| 98 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 99 | + .withQuery(termQuery("ip", "10.10.10")).build(); |
| 100 | + |
| 101 | + List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class); |
| 102 | + //then |
| 103 | + assertThat(entities, is(notNullValue())); |
| 104 | + assertThat(entities.size(), is(1)); |
| 105 | + } |
| 106 | + |
| 107 | + /* |
| 108 | + DATAES-66 |
| 109 | + */ |
| 110 | + @Test |
| 111 | + public void shouldReturnLogsForGivenIPRanges() { |
| 112 | + //when |
| 113 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 114 | + .withQuery(rangeQuery("ip").from("10.10.10.1").to("10.10.10.3")).build(); |
| 115 | + |
| 116 | + List<LogEntity> entities = template.queryForList(searchQuery, LogEntity.class); |
| 117 | + //then |
| 118 | + assertThat(entities, is(notNullValue())); |
| 119 | + assertThat(entities.size(), is(3)); |
| 120 | + } |
| 121 | +} |
0 commit comments