/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.    
 */
package org.apache.openjpa.sdo;

import org.apache.openjpa.sdo.ImplHelper;

import test.common.AbstractSDOTest;

import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Type;
import commonj.sdo.helper.DataFactory;

/**
 * Tests to read a SDO meta-model from XML Schema and create instances
 * according to the meta-model.
 * 
 * @author Pinaki Poddar
 * 
 */
public class TestSDO extends AbstractSDOTest {
	
	public void testPackage() {
		Type addressType = getType("USAddress");
		assertNotNull(addressType);
		String pkg = ImplHelper.getPackageName(addressType);
		assertEquals("example.domain",pkg);
	}
	
	public void testInstanceClassIsNullForUserType() {
		Type addressType = getType("USAddress");
		assertFalse(addressType.isDataType());
		assertNull(addressType.getInstanceClass());
	}
	
	public void testIdentityType() {
		Type poType = getType("PurchaseOrderType");
		assertNotNull(poType);
		Property id2 = ImplHelper.getIdentityProperty(poType);
		assertNotNull(id2);
		assertTrue(ImplHelper.isIdentity(id2));
	}

	
	/**
	 * Create a SDO MetaData Model from a XML Schema and populate instances.
	 * Assumes 'po.xsd' be available in classpath as resource.
	 * 
	 */
	public void testCreateModel() {
		DataObject purchaseOrder = DataFactory.INSTANCE.create(
				getType("PurchaseOrderType"));

		purchaseOrder.setString("orderDate", "1999-10-20");

		DataObject shipTo = purchaseOrder.createDataObject("shipTo");
		shipTo.set("country", "US");
		shipTo.set("name", "Alice Smith");
		shipTo.set("street", "123 Maple Street");
		shipTo.set("city", "Mill Valley");
		shipTo.set("state", "CA");
		shipTo.setString("zip", "90952");
		DataObject billTo = purchaseOrder.createDataObject("billTo");
		billTo.set("country", "US");
		billTo.set("name", "Robert Smith");
		billTo.set("street", "8 Oak Avenue");
		billTo.set("city", "Mill Valley");
		billTo.set("state", "PA");
		billTo.setString("zip", "95819");
		purchaseOrder.set("comment", "Hurry, my lawn is going wild!");

		DataObject items = purchaseOrder.createDataObject("items");

		DataObject item1 = items.createDataObject("item");
		item1.set("partNum", "872-AA");
		item1.set("productName", "Lawnmower");
		item1.setInt("quantity", 1);
		item1.setString("USPrice", "148.95");
		item1.set("comment", "Confirm this is electric");

		DataObject item2 = items.createDataObject("item");
		item2.set("partNum", "926-AA");
		item2.set("productName", "Baby Monitor");
		item2.setInt("quantity", 1);
		item2.setString("USPrice", "39.98");
		item2.setString("shipDate", "1999-05-21");
		
		assertEquals("1999-10-20", purchaseOrder.get("orderDate"));
		assertEquals("Alice Smith", purchaseOrder.getDataObject("shipTo").getString("name"));
		
//		DelegatingDataObject delegate = new DelegatingDataObject(purchaseOrder);
//		assertEquals("1999-10-20", delegate.get("orderDate"));
//		assertEquals("Alice Smith", delegate.getDataObject("shipTo").getString("name"));
//		try {
//			OutputStream stream = System.err;
//			XMLHelper.INSTANCE.save(purchaseOrder, SDO_MODEL_NAMESPACE,
//					"purchaseOrder", stream);
//		} catch (IOException e) {
//			e.printStackTrace();
//			fail();
//		}
	}

}
