package org.openlca.core.math;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.openlca.core.TestProcess;
import org.openlca.core.TestSystem;
import org.openlca.core.model.AllocationMethod;
import org.openlca.core.model.Process;
import org.openlca.core.model.ProductSystem;
import org.openlca.core.model.descriptors.FlowDescriptor;
import org.openlca.core.results.FullResultProvider;

public class AllocationTest {

	@Test
	public void testProducts() {
		AllocationMethod method = AllocationMethod.PHYSICAL;
		Process p = TestProcess
				.refProduct("p1", 1, "kg")
				.prodOut("p2", 3, "kg")
				.elemOut("CO2", 2, "kg")
				.alloc("p1", method, 0.25)
				.alloc("p2", method, 0.75)
				.get();
		checkIt(method, p);
	}

	@Test
	public void testWastes() {
		AllocationMethod method = AllocationMethod.PHYSICAL;
		Process p = TestProcess
				.refWaste("w1", 1, "kg")
				.wasteIn("w2", 3, "kg")
				.elemOut("CO2", 2, "kg")
				.alloc("w1", method, 0.25)
				.alloc("w2", method, 0.75)
				.get();
		checkIt(method, p);
	}

	private void checkIt(AllocationMethod method, Process p) {
		ProductSystem system = TestSystem.of(p).get();
		CalculationSetup setup = new CalculationSetup(system);
		setup.allocationMethod = method;
		FullResultProvider r = TestSystem.calculate(setup);
		assertEquals(1, r.getFlowDescriptors().size());
		FlowDescriptor co2 = r.getFlowDescriptors().iterator().next();
		assertEquals(0.5, r.getTotalFlowResult(co2).value, 1e-16);
	}

}

@@ -1049,16 +1049,634 @@
 p);%0A%09%7D%0A%0A
+%09@Test%0A%09public void testProductWaste() %7B%0A%09%09AllocationMethod method = AllocationMethod.PHYSICAL;%0A%09%09Process p = TestProcess%0A%09%09%09%09.refProduct(%22p1%22, 1, %22kg%22)%0A%09%09%09%09.wasteIn(%22w2%22, 3, %22kg%22)%0A%09%09%09%09.elemOut(%22CO2%22, 2, %22kg%22)%0A%09%09%09%09.alloc(%22p1%22, method, 0.25)%0A%09%09%09%09.alloc(%22w2%22, method, 0.75)%0A%09%09%09%09.get();%0A%09%09checkIt(method, p);%0A%09%7D%0A%0A%09@Test%0A%09public void testWasteProduct() %7B%0A%09%09AllocationMethod method = AllocationMethod.PHYSICAL;%0A%09%09Process p = TestProcess%0A%09%09%09%09.refWaste(%22w1%22, 1, %22kg%22)%0A%09%09%09%09.prodOut(%22p2%22, 3, %22kg%22)%0A%09%09%09%09.elemOut(%22CO2%22, 2, %22kg%22)%0A%09%09%09%09.alloc(%22w1%22, method, 0.25)%0A%09%09%09%09.alloc(%22p2%22, method, 0.75)%0A%09%09%09%09.get();%0A%09%09checkIt(method, p);%0A%09%7D%0A%0A
 %09private

test cases for mixed product output & waste input allocation