/* Copyright (C) 2022  Egon Willighagen <egonw@users.sf.net>
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This program 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 Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.bitbucket.nanojava.data;

import java.io.ByteArrayOutputStream;

import org.bitbucket.nanojava.data.measurement.EndPoints;
import org.bitbucket.nanojava.data.measurement.ErrorlessMeasurementValue;
import org.bitbucket.nanojava.inchi.NInChIGenerator;
import org.bitbucket.nanojava.io.CDKSerializer;
import org.junit.Assert;
import org.junit.Test;
import org.xmlcml.cml.element.CMLMoleculeList;

import com.github.jqudt.onto.units.LengthUnit;

import nu.xom.Document;
import nu.xom.Serializer;

public class NanoInChIExamplesTest {

	@Test
	public void figureEightLeft() throws Exception {
		Material material = MaterialBuilder.type("METALOXIDE")
			.label("silica nanoparticles with gold coating")
			.componentFromSMILES(1, "O=[Si]=O", "SPHERE", "AMORPHOUS", new ErrorlessMeasurementValue(EndPoints.DIAMETER, 20, LengthUnit.NM))
			.componentFromSMILES(2, "[Au]", "SHELL", new ErrorlessMeasurementValue(EndPoints.THICKNESS, 2, LengthUnit.NM))
			.asMaterial();

		String nanoInChI = NInChIGenerator.generator(material);
		Assert.assertEquals("InChI=1A/Au/msh/s2t-9!O2Si/c1-3-2/msp/s20d-9/k000/y2&1", nanoInChI);
	}

	@Test
	public void simpleGoldParticle() throws Exception {
		Material material = MaterialBuilder.type("METAL")
			.componentFromSMILES(1, "[Au]", "SPHERE", new ErrorlessMeasurementValue(EndPoints.DIAMETER, 3.0, LengthUnit.NM))
			.asMaterial();
		String nanoInChI = NInChIGenerator.generator(material);
		Assert.assertEquals("InChI=1A/Au/msp/s3d-9/y1", nanoInChI);
	}

	private String asIndentedString(CMLMoleculeList cmlMaterial) throws Exception {
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		Serializer xomSerializer = new Serializer(output, "UTF-8");
		xomSerializer.setIndent(2);
		xomSerializer.write(new Document(cmlMaterial));
		return output.toString();
	}

}

@@ -1040,29 +1040,41 @@
 ava.
-inchi.NInChIGenerator
+data.measurement.MeasurementValue
 ;%0Aim
@@ -1106,22 +1106,27 @@
 va.i
-o.CDKSerialize
+nchi.NInChIGenerato
 r;%0Ai
@@ -1651,24 +1651,24 @@
 thUnit.NM))%0A
-
 %09%09%09.componen
@@ -1942,16 +1942,602 @@
 I);%0A%09%7D%0A%0A
+%09@Test%0A%09public void figureEightLeft_WithErrors() throws Exception %7B%0A%09%09Material material = MaterialBuilder.type(%22METALOXIDE%22)%0A%09%09%09.label(%22silica nanoparticles with gold coating%22)%0A%09%09%09.componentFromSMILES(1, %22O=%5BSi%5D=O%22, %22SPHERE%22, %22AMORPHOUS%22, new MeasurementValue(EndPoints.DIAMETER, 20, 3, LengthUnit.NM))%0A%09%09%09.componentFromSMILES(2, %22%5BAu%5D%22, %22SHELL%22, new MeasurementValue(EndPoints.THICKNESS, 2, 0.5, LengthUnit.NM))%0A%09%09%09.asMaterial();%0A%0A%09%09String nanoInChI = NInChIGenerator.generator(material);%0A%09%09Assert.assertEquals(%22InChI=1A/Au/msh/s2t-9!O2Si/c1-3-2/msp/s20d-9/k000/y2&1%22, nanoInChI);%0A%09%7D%0A%0A
 %09@Test%0A%09

Test if error info is handled correctly (ignored)