<commit_before>package li.strolch.search;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static li.strolch.agent.api.StrolchAgent.getUniqueId;
import static li.strolch.model.ModelGenerator.*;
import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonObject;
import li.strolch.RuntimeMock;
import li.strolch.agent.ParallelTests;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.*;
import li.strolch.model.activity.Activity;
import li.strolch.model.json.StrolchRootElementToJsonVisitor;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.utils.collections.DateRange;
import li.strolch.utils.iso8601.ISO8601FormatFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StrolchSearchTest {

	public static final Logger logger = LoggerFactory.getLogger(ParallelTests.class);

	private static final String TARGET_PATH = "target/" + StrolchSearchTest.class.getSimpleName();
	private static final String SOURCE_PATH = "src/test/resources/transienttest";

	private static RuntimeMock runtimeMock;
	private static Certificate cert;

	@BeforeClass
	public static void beforeClass() {
		runtimeMock = new RuntimeMock(TARGET_PATH, SOURCE_PATH).mockRuntime();
		runtimeMock.startContainer();
		cert = runtimeMock.getPrivilegeHandler().authenticate("test", "test".toCharArray());

		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);
		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			{
				Resource ball = createResource("the-id", "Yellow Ball", "Ball");

				ParameterBag bag = new ParameterBag("parameters", "Parameters", "Parameters");
				ball.addParameterBag(bag);

				bag.addParameter(new StringParameter("status", "Status", "bla"));
				bag.addParameter(new StringParameter("color", "Color", "yellow"));

				tx.add(ball);
			}

			{
				Resource ball = createResource(getUniqueId(), "Green Ball", "Ball");

				ParameterBag bag = new ParameterBag("parameters", "Parameters", "Parameters");
				ball.addParameterBag(bag);

				bag.addParameter(new StringParameter("status", "Status", "STATUS"));
				bag.addParameter(new StringParameter("color", "Color", "green"));

				tx.add(ball);
			}

			String id;
			id = "ggg";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "ccc";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "aaa";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "bbb";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "ddd";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));

			tx.commitOnClose();
		}
	}

	@AfterClass
	public static void afterClass() {
		if (cert != null)
			runtimeMock.getPrivilegeHandler().invalidate(cert);
		if (runtimeMock != null)
			runtimeMock.destroyRuntime();
	}

	@Test
	public void shouldSearchResources() {

		StrolchRootElementToJsonVisitor toJsonVisitor = new StrolchRootElementToJsonVisitor();

		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);

		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			List<JsonObject> result = new BallSearch("the-id", "STATUS", "yellow")
					// do search, returns SearchResult
					.search(tx)
					// transform, either:
					.asResources() //  asOrders(), asActivities() or map to something entirely different:
					.map(a -> a.accept(toJsonVisitor))
					// finishing as toList, toSet, toPaging
					.toList();

			assertEquals(2, result.size());
		}
	}

	@Test
	public void shouldSearchOrders() {
		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);
		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			List<Order> result = new StrolchSearch() {
				@Override
				public StrolchSearch prepare() {

					DateRange dateRange = new DateRange() //
							.from(ISO8601FormatFactory.getInstance().parseDate("2012-01-01T00:00:00.000+01:00"), true)
							.to(ISO8601FormatFactory.getInstance().parseDate("2013-01-01T00:00:00.000+01:00"), true);

					return orders() //
							.where(date(isEqualTo(new Date(1384929777699L))) //
									.or(state(isEqualTo(State.CREATED)) //
											.and(param(BAG_ID, PARAM_STRING_ID, isEqualTo("Strolch"))) //
											.and(param(BAG_ID, PARAM_DATE_ID, inRange(dateRange)))));
				}
			} //
					.search(tx) //
					.asOrders() //
					.toList();

			assertEquals(2, result.size());
		}
	}
<commit_msg>[Fix] Fixed broken test<commit_after>package li.strolch.search;

import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static li.strolch.agent.api.StrolchAgent.getUniqueId;
import static li.strolch.model.ModelGenerator.*;
import static org.junit.Assert.assertEquals;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.google.gson.JsonObject;
import li.strolch.RuntimeMock;
import li.strolch.agent.ParallelTests;
import li.strolch.agent.api.StrolchRealm;
import li.strolch.model.*;
import li.strolch.model.activity.Activity;
import li.strolch.model.json.StrolchRootElementToJsonVisitor;
import li.strolch.model.parameter.StringParameter;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.utils.collections.DateRange;
import li.strolch.utils.iso8601.ISO8601FormatFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StrolchSearchTest {

	public static final Logger logger = LoggerFactory.getLogger(ParallelTests.class);

	private static final String TARGET_PATH = "target/" + StrolchSearchTest.class.getSimpleName();
	private static final String SOURCE_PATH = "src/test/resources/transienttest";

	private static RuntimeMock runtimeMock;
	private static Certificate cert;

	@BeforeClass
	public static void beforeClass() {
		runtimeMock = new RuntimeMock(TARGET_PATH, SOURCE_PATH).mockRuntime();
		runtimeMock.startContainer();
		cert = runtimeMock.getPrivilegeHandler().authenticate("test", "test".toCharArray());

		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);
		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			{
				Resource ball = createResource("the-id", "Yellow Ball", "Ball");

				ParameterBag bag = new ParameterBag("parameters", "Parameters", "Parameters");
				ball.addParameterBag(bag);

				bag.addParameter(new StringParameter("status", "Status", "bla"));
				bag.addParameter(new StringParameter("color", "Color", "yellow"));

				tx.add(ball);
			}

			{
				Resource ball = createResource(getUniqueId(), "Green Ball", "Ball");

				ParameterBag bag = new ParameterBag("parameters", "Parameters", "Parameters");
				ball.addParameterBag(bag);

				bag.addParameter(new StringParameter("status", "Status", "STATUS"));
				bag.addParameter(new StringParameter("color", "Color", "green"));

				tx.add(ball);
			}

			String id;
			id = "ggg";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "ccc";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "aaa";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "bbb";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));
			id = "ddd";
			tx.add(createOrder(id, id.toUpperCase(), "SortingType"));

			tx.commitOnClose();
		}
	}

	@AfterClass
	public static void afterClass() {
		if (cert != null)
			runtimeMock.getPrivilegeHandler().invalidate(cert);
		if (runtimeMock != null)
			runtimeMock.destroyRuntime();
	}

	@Test
	public void shouldSearchResources() {

		StrolchRootElementToJsonVisitor toJsonVisitor = new StrolchRootElementToJsonVisitor();

		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);

		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			List<JsonObject> result = new BallSearch("the-id", "STATUS", "yellow")
					// do search, returns SearchResult
					.search(tx)
					// transform, either:
					.asResources() //  asOrders(), asActivities() or map to something entirely different:
					.map(a -> a.accept(toJsonVisitor))
					// finishing as toList, toSet, toPaging
					.toList();

			assertEquals(2, result.size());
		}
	}

	@Test
	public void shouldSearchOrders() {
		StrolchRealm realm = runtimeMock.getAgent().getContainer().getRealm(cert);
		try (StrolchTransaction tx = realm.openTx(cert, ParallelTests.class)) {

			List<Order> result = new StrolchSearch() {
				@Override
				public StrolchSearch prepare() {

					DateRange dateRange = new DateRange() //
							.from(ISO8601FormatFactory.getInstance().parseDate("2012-01-01T00:00:00.000+01:00"), true)
							.to(ISO8601FormatFactory.getInstance().parseDate("2013-01-01T00:00:00.000+01:00"), true);

					return orders() //
							.where(date(isEqualTo(new Date(1384929777699L))) //
									.or(state(isEqualTo(State.CREATED)) //
											.and(param(BAG_ID, PARAM_STRING_ID, isEqualTo("Strolch"))) //
											.and(param(BAG_ID, PARAM_DATE_ID, inRange(dateRange)))));
				}
			} //
					.search(tx) //
					.asOrders() //
					.toList();

			assertEquals(7, result.size());
		}
	}

{'fraction_non_alphanumeric': 0.14132266217354675, 'fraction_numerical': 0.015796124684077505, 'mean_word_length': 4.566822977725674, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1891934', 'n_tokens_mistral': 3569, 'n_tokens_neox': 3034, 'n_words': 573}