/*
 * Copyright 2016 EPAM Systems
 *
 *
 * This file is part of EPAM Report Portal.
 * https://github.com/reportportal/commons-dao
 *
 * Report Portal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Report Portal is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Report Portal.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.epam.ta.reportportal.database.search;

import com.epam.ta.reportportal.database.entity.item.TestItem;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.data.mongodb.core.query.Criteria;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

import static org.hamcrest.Matchers.is;

/**
 * @author Andrei Varabyeu
 */
public class ConditionBuilderTest {

	@Test(expected = IllegalArgumentException.class)
	public void checkEmptyValue() {
		FilterCondition.builder()
				.withCondition(Condition.EQUALS)
				.withSearchCriteria("xxx").build();
	}

	@Test(expected = IllegalArgumentException.class)
	public void checkEmptyCondition() {
		FilterCondition.builder()
				.withValue("hello")
				.withSearchCriteria("xxx").build();
	}

	@Test
	public void checkConditionPositive() {
		FilterCondition condition = FilterCondition.builder().withValue("hello").withSearchCriteria("xxx").withCondition(Condition.EQUALS)
				.build();
		Assert.assertThat("Incorrect condition", condition.getCondition(), is(Condition.EQUALS));
		Assert.assertThat("Incorrect value", condition.getValue(), is("hello"));
		Assert.assertThat("Incorrect search criteria", condition.getSearchCriteria(), is("xxx"));
	}


	@Test
	public void checkEquals() {
		FilterCondition condition = FilterCondition.builder().eq("xxx", "hello").build();
		Assert.assertThat("Incorrect condition", condition.getCondition(), is(Condition.EQUALS));
		Assert.assertThat("Incorrect value", condition.getValue(), is("hello"));
		Assert.assertThat("Incorrect search criteria", condition.getSearchCriteria(), is("xxx"));
	}

	@Test
	public void checkDateStartingFrom() {

		FilterCondition condition = FilterCondition.builder()
				.withSearchCriteria("start_time")
				.withValue( "0;1439;-6")
				.withCondition(Condition.BETWEEN)
				.build();
		Assert.assertThat("Incorrect condition", condition.getCondition(), is(Condition.BETWEEN));

		Criteria criteria = new Criteria();
		condition.getCondition().addCondition(criteria, condition,
				CriteriaMapFactory.DEFAULT_INSTANCE_SUPPLIER.get().getCriteriaMap(TestItem.class).getCriteriaHolder("start_time"));

		long expected = LocalDate.now().atStartOfDay(ZoneId.of("UTC")).plusHours(6).toEpochSecond();
		long calculated = ((Date) (criteria.getCriteriaObject().get("$gte"))).toInstant().getEpochSecond();
		Assert.assertThat("Incorrect between range processing", calculated, is(expected));

	}

}
@@ -1021,25 +1021,22 @@
 va.time.
-LocalDate
+ZoneId
 ;%0Aimport
@@ -1042,34 +1042,41 @@
 t java.time.Zone
-I
 d
+DateTime
 ;%0Aimport java.ut
@@ -1304,29 +1304,24 @@
 on.builder()
-%0A%09%09%09%09
 .withConditi
@@ -1340,21 +1340,16 @@
 .EQUALS)
-%0A%09%09%09%09
 .withSea
@@ -1490,29 +1490,24 @@
 on.builder()
-%0A%09%09%09%09
 .withValue(%22
@@ -1513,21 +1513,16 @@
 %22hello%22)
-%0A%09%09%09%09
 .withSea
@@ -1647,32 +1647,37 @@
 dition.builder()
+%0A%09%09%09%09
 .withValue(%22hell
@@ -1671,32 +1671,37 @@
 thValue(%22hello%22)
+%0A%09%09%09%09
 .withSearchCrite
@@ -1710,16 +1710,21 @@
 a(%22xxx%22)
+%0A%09%09%09%09
 .withCon
@@ -2025,17 +2025,16 @@
 ));%0A%09%7D%0A%0A
-%0A
 %09@Test%0A%09
@@ -2564,17 +2564,16 @@
 thValue(
- 
 %220;1439;
@@ -2801,16 +2801,21 @@
 ndition(
+%0A%09%09%09%09
 criteria
@@ -2815,17 +2815,21 @@
 riteria,
- 
+%0A%09%09%09%09
 conditio
@@ -2948,16 +2948,19 @@
 t_time%22)
+%0A%09%09
 );%0A%0A%09%09lo
@@ -2973,16 +2973,54 @@
 ected = 
+ZonedDateTime.now(ZoneId.of(%22UTC%22)).to
 LocalDat
@@ -3020,20 +3020,16 @@
 ocalDate
-.now
 ().atSta
@@ -3277,8 +3277,9 @@
 ;%0A%0A%09%7D%0A%0A%7D
+%0A

fix unit test