package org.openlca.core.database.usage;


import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openlca.core.Tests;
import org.openlca.core.database.IDatabase;
import org.openlca.core.model.Exchange;
import org.openlca.core.model.ImpactCategory;
import org.openlca.core.model.ImpactFactor;
import org.openlca.core.model.Process;
import org.openlca.core.model.RootEntity;
import org.openlca.core.model.SocialIndicator;
import org.openlca.core.model.Unit;
import org.openlca.core.model.UnitGroup;
import org.openlca.core.model.descriptors.Descriptor;

public class UnitUsageSearchTest {

	private final IDatabase db = Tests.getDb();
	private UnitUsageSearch search;
	private UnitGroup group;
	private Unit unit;

	@Before
	public void setup() {
		group = db.insert(UnitGroup.of("Units of mass", "kg"));
		unit = group.referenceUnit;
		search = new UnitUsageSearch(db, unit);
	}

	@After
	public void tearDown() {
		db.delete(group);
	}

	@Test
	public void testFindNoUsage() {
		var models = search.run();
		Assert.assertNotNull(models);
		Assert.assertTrue(models.isEmpty());
	}

	@Test
	public void testFindInImpactCategories() {
		var impact = ImpactCategory.of("my impact");
		var factor = new ImpactFactor();
		factor.unit = unit;
		impact.impactFactors.add(factor);
		check(impact);
	}

	@Test
	public void testFindInProcesses() {
		var process = new Process();
		var exchange = new Exchange();
		exchange.unit = unit;
		process.exchanges.add(exchange);
		check(process);
	}

	@Test
	public void testFindInSocialIndicator() {
		var indicator = new SocialIndicator();
		indicator.name = "indicator";
		indicator.activityUnit = unit;
		check(indicator);
	}

	private void check(RootEntity e) {
		db.insert(e);
		var results = search.run();
		db.delete(e);
		var expected = Descriptor.of(e);
		Assert.assertEquals(1, results.size());
		Assert.assertEquals(expected, results.get(0));
	}

}

@@ -248,16 +248,58 @@
 change;%0A
+import org.openlca.core.model.FlowResult;%0A
 import o
@@ -419,16 +419,139 @@
 rocess;%0A
+import org.openlca.core.model.Project;%0Aimport org.openlca.core.model.ProjectVariant;%0Aimport org.openlca.core.model.Result;%0A
 import o
@@ -1727,151 +1727,532 @@
 ndIn
-SocialIndicator() %7B%0A%09%09var indicator = new SocialIndicator();%0A%09%09indicator.name = %22indicator%22;%0A%09%09indicator.activityUnit = unit;%0A%09%09check(indicator
+Result() %7B%0A%09%09var result = new Result();%0A%09%09var flowResult = new FlowResult();%0A%09%09flowResult.unit = unit;%0A%09%09result.flowResults.add(flowResult);%0A%09%09check(result);%0A%09%7D%0A%0A%09@Test%0A%09public void testFindInSocialIndicator() %7B%0A%09%09var indicator = new SocialIndicator();%0A%09%09indicator.name = %22indicator%22;%0A%09%09indicator.activityUnit = unit;%0A%09%09check(indicator);%0A%09%7D%0A%0A%09@Test%0A%09public void testFindInProject() %7B%0A%09%09var project = new Project();%0A%09%09var variant = new ProjectVariant();%0A%09%09variant.unit = unit;%0A%09%09project.variants.add(variant);%0A%09%09check(project
 );%0A%09

add more tests