package org.openlca.core.libraries;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.nio.file.Files;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openlca.core.Tests;
import org.openlca.core.database.IDatabase;
import org.openlca.core.library.LibraryDir;
import org.openlca.core.library.Mounter;
import org.openlca.core.model.FlowProperty;
import org.openlca.core.model.UnitGroup;
import org.openlca.jsonld.ZipStore;
import org.openlca.jsonld.output.JsonExport;
import org.openlca.util.Dirs;

public class DepTest {

	private final IDatabase db = Tests.getDb();
	private  LibraryDir libDir;

	@Before
	public void setup() throws Exception {
		var dir = Files.createTempDirectory("olca_tests").toFile();
		libDir = new LibraryDir(dir);
	}

	@After
	public void tearDown() {
		db.clear();
		Dirs.delete(libDir.folder());
	}

	@Test
	public void testDep() throws Exception {

		var unitLib = libDir.create("units 1.0");
		var units = UnitGroup.of("Units of mass", "kg");
		try (var zip = ZipStore.open(new File(unitLib.folder(), "meta.zip"))) {
			var exp = new JsonExport(zip);
			exp.write(units);
		}

		var propsLib = libDir.create("props 1.0");
		propsLib.addDependency(unitLib);
		var prop = FlowProperty.of("Mass", units);
		try (var zip = ZipStore.open(new File(propsLib.folder(), "meta.zip"))) {
			var exp = new JsonExport(zip).withReferences(false);
			exp.write(prop);
		}

		Mounter.of(db, propsLib).run();
		assertTrue(db.getLibraries().contains("props 1.0"));
		var dbUnits = db.get(UnitGroup.class, units.refId);
		assertEquals(unitLib.name(), dbUnits.library);
		var dbMass = db.get(FlowProperty.class, prop.refId);
		assertEquals(propsLib.name(), dbMass.library);
	}
}

@@ -65,86 +65,11 @@
 ert.
-assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport java.io.File;
+*;%0A
 %0Aimp
@@ -92,16 +92,38 @@
 e.Files;
+%0Aimport java.util.Map;
 %0A%0Aimport
@@ -263,16 +263,57 @@
 tabase;%0A
+import org.openlca.core.library.Library;%0A
 import o
@@ -385,18 +385,110 @@
 ry.Mount
-er
+Action;%0Aimport org.openlca.core.library.Mounter;%0Aimport org.openlca.core.library.PreMountCheck
 ;%0Aimport
@@ -570,44 +570,8 @@
 up;%0A
-import org.openlca.jsonld.ZipStore;%0A
 impo
@@ -739,16 +739,123 @@
  libDir;
+%0A%09private Library unitLib;%0A%09private UnitGroup units;%0A%09private Library propsLib;%0A%09private FlowProperty prop;
 %0A%0A%09@Befo
@@ -995,152 +995,11 @@
 r);%0A
-%09%7D%0A%0A%09@After%0A%09public void tearDown() %7B%0A%09%09db.clear();%0A%09%09Dirs.delete(libDir.folder());%0A%09%7D%0A%0A%09@Test%0A%09public void testDep() throws Exception %7B%0A%0A%09%09var 
+%0A%09%09
 unit
@@ -1034,20 +1034,16 @@
 .0%22);%0A%09%09
-var 
 units = 
@@ -1100,60 +1100,28 @@
 p = 
-ZipStore.open(new File(unitLib.folder(), %22meta.zip%22)
+unitLib.openJsonZip(
 )) %7B
@@ -1183,20 +1183,16 @@
 %0A%09%09%7D%0A%0A%09%09
-var 
 propsLib
@@ -1259,20 +1259,16 @@
 Lib);%0A%09%09
-var 
 prop = F
@@ -1319,61 +1319,29 @@
 p = 
-ZipStore.open(new File(propsLib.folder(), %22meta.zip%22)
+propsLib.openJsonZip(
 )) %7B
@@ -1425,42 +1425,613 @@
 %09%09%7D%0A
-%0A%09%09Mounter.of(db, propsLib).run();
+%09%7D%0A%0A%09@After%0A%09public void tearDown() %7B%0A%09%09db.clear();%0A%09%09Dirs.delete(libDir.folder());%0A%09%7D%0A%0A%09@Test%0A%09public void testMountWithCheck() %7B%0A%09%09var checkResult = PreMountCheck.check(db, propsLib);%0A%09%09assertFalse(checkResult.isError());%0A%09%09Mounter.of(db, propsLib)%0A%09%09%09.applyDefaultsOf(checkResult)%0A%09%09%09.run();%0A%09%7D%0A%0A%09@Test%0A%09public void testMountWithActions() %7B%0A%09%09Mounter.of(db, propsLib)%0A%09%09%09.apply(Map.of(%0A%09%09%09%09propsLib, MountAction.ADD,%0A%09%09%09%09unitLib, MountAction.ADD))%0A%09%09%09.run();%0A%09%09checkState();%0A%09%7D%0A%0A%09@Test%0A%09public void testMountDefault() %7B%0A%09%09Mounter.of(db, propsLib).run();%0A%09%09checkState();%0A%09%7D%0A%0A%09private void checkState() %7B
 %0A%09%09a

test dependency mounting also with check-/mount-actions