# COSMOS Developer's Guide

### Testing the Query Service

The query service can be tested independently from the user interface, and in an automated fashion using JUnit tests. See Running Student Teacher MDR in eclipse for an alternate method for testing an MDR.

The code included in this section can easily be incorporated in a JUnit to automatically test the results against an expected outcome. Follow the instructions below to test the query service.

1. Create a query request under `org.eclipse.cosmos.example.mdr/src` called teaches-relationship.xml.
2. Create a new class called QueryLauncher under the same package as the handlers (i.e. `org.eclipse.cosmos.example.mdr.handlers`).

Use the content below for the query request. This file queries for all students that are taught by teacher staff01.

    <?xml version="1.0" encoding="UTF-8"?>

    <!-- This query selects all students taught by the teacher with -->
    <!-- the id: "staff01" -->
    <s:query xmlns:s="http://cmdbf.org/schema/1-0-0/datamodel">
    	<s:itemTemplate id="teacher">
    		<s:instanceIdConstraint>
    			<s:instanceId>
    				<s:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</s:mdrId>
    				<s:localId>staff01</s:localId>
    			</s:instanceId>
    		</s:instanceIdConstraint>
    	</s:itemTemplate>

    	<s:itemTemplate id="students">
    		<s:recordConstraint>
    			<s:recordType namespace="" localName="student"/>
    		</s:recordConstraint>
    	</s:itemTemplate>

    	<s:relationshipTemplate id="reference">
    		<s:sourceTemplate ref="teacher"/>
    		<s:targetTemplate ref="students"/>
    	</s:relationshipTemplate>
    </s:query

Here is the content for QueryLauncher. The main method simply uses the content of teaches-relationship.xml to submit a CMDBf query:

    package org.eclipse.cosmos.example.mdr.handlers;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URISyntaxException;
    import java.util.Hashtable;
    import java.util.Map;

    import javax.xml.parsers.ParserConfigurationException;

    import org.eclipse.cosmos.dc.provisional.cmdbf.services.common.CMDBfServiceException;
    import org.eclipse.cosmos.dc.provisional.cmdbf.services.query.service.impl.CMDBfQueryOperation;
    import org.eclipse.cosmos.dc.provisional.cmdbf.services.query.transform.QueryOutputTransformer;
    import org.eclipse.cosmos.dc.provisional.cmdbf.services.query.transform.response.artifacts.IQueryResult;
    import org.xml.sax.SAXException;

    /**
     * The main class used to run the CMDBf queries.  A set of query
     * files exist under the source directory.  Adjust the queryFile field to
     * run the correct CMDBf query.
     */
    public class QueryLauncher
    {
    	private static final String queryFile = "teaches-relationship.xml";

    	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, IllegalArgumentException, URISyntaxException, CMDBfServiceException
    	{
    		// Run the query
    		InputStream query = QueryLauncher.class.getClassLoader().getResourceAsStream(queryFile);
    		CMDBfQueryOperation queryOperation = new CMDBfQueryOperation(QueryHandlerFactory.getInstance(), null);
    		Map<String, Object> init = new Hashtable<String, Object>();
    		init.put(ICMDBfSampleConstants.DATA_PROVIDER, new XMLRepository());
    		queryOperation.initialize(init);
    		IQueryResult result = queryOperation.execute(query);

    		// Transform and output the query
    		InputStream resultStream = QueryOutputTransformer.transform(result);
    		byte[] buffer = new byte[1024];
    		while (resultStream.available() > 0)
    		{
    			int chars = resultStream.read(buffer);
    			System.out.print(new String(buffer, 0, chars));
    		}
    	}

    }

Once executed, the output of the query should be printed out to the console. The output will look something similar to the following code:.

    <cmdbf:queryResult xmlns:cmdbf="http://cmdbf.org/schema/1-0-0/datamodel">
    	<cmdbf:nodes templateId="teacher">
    		<cmdbf:item>
    			<cmdbf:record xmlns="http://school">

    				<teacher>
    				 <identity firstName="Dawn" lastName="Johnson" id="staff01"/>
    				</teacher>

    				<cmdbf:recordMetadata>
    					<cmdbf:recordId>staff01</cmdbf:recordId>
    				</cmdbf:recordMetadata>
    			</cmdbf:record>
    			<cmdbf:instanceId>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>staff01</cmdbf:localId>
    			</cmdbf:instanceId>
    		</cmdbf:item>
    	</cmdbf:nodes>
    	<cmdbf:nodes templateId="students">
    		<cmdbf:item>
    			<cmdbf:record xmlns="http://school">

    				<student>
    				 <identity firstName="Mike" lastName="Lee" id="03"/>
    				</student>

    				<cmdbf:recordMetadata>
    					<cmdbf:recordId>03</cmdbf:recordId>
    				</cmdbf:recordMetadata>
    			</cmdbf:record>
    			<cmdbf:instanceId>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>03</cmdbf:localId>
    			</cmdbf:instanceId>
    		</cmdbf:item>
    		<cmdbf:item>
    			<cmdbf:record xmlns="http://school">

    				<student>
    				 <identity firstName="Bob" lastName="Davidson" id="01"/>
    				</student>

    				<cmdbf:recordMetadata>
    					<cmdbf:recordId>01</cmdbf:recordId>
    				</cmdbf:recordMetadata>
    			</cmdbf:record>
    			<cmdbf:instanceId>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>01</cmdbf:localId>
    			</cmdbf:instanceId>
    		</cmdbf:item>
    	</cmdbf:nodes>
    	<cmdbf:edges templateId="reference">
    		<cmdbf:relationship>
    			<cmdbf:source>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>staff01</cmdbf:localId>
    			</cmdbf:source>
    			<cmdbf:target>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>03</cmdbf:localId>
    			</cmdbf:target>
    			<cmdbf:record xmlns="http://school">

    				<class name="Economics" courseCode="ECM01">
    				 <students>
    				  <enrolledStudent idRef="01"/>
    				  <enrolledStudent idRef="03"/>
    				 </students>
    				 <teacher idRef="staff01"/>
    				</class>

    				<cmdbf:recordMetadata>
    					<cmdbf:recordId>ECM01</cmdbf:recordId>
    				</cmdbf:recordMetadata>
    			</cmdbf:record>
    			<cmdbf:instanceId>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>ECM01</cmdbf:localId>
    			</cmdbf:instanceId>
    		</cmdbf:relationship>
    		<cmdbf:relationship>
    			<cmdbf:source>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>staff01</cmdbf:localId>
    			</cmdbf:source>
    			<cmdbf:target>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>01</cmdbf:localId>
    			</cmdbf:target>
    			<cmdbf:record xmlns="http://school">

    				<class name="Economics" courseCode="ECM01">
    				 <students>
    				  <enrolledStudent idRef="01"/>
    				  <enrolledStudent idRef="03"/>
    				 </students>
    				 <teacher idRef="staff01"/>
    				</class>

    				<cmdbf:recordMetadata>
    					<cmdbf:recordId>ECM01</cmdbf:recordId>
    				</cmdbf:recordMetadata>
    			</cmdbf:record>
    			<cmdbf:instanceId>
    				<cmdbf:mdrId>org.eclipse.cosmos.samples.cmdbf.XMLRepository</cmdbf:mdrId>
    				<cmdbf:localId>ECM01</cmdbf:localId>
    			</cmdbf:instanceId>
    		</cmdbf:relationship>
    	</cmdbf:edges>
    </cmdbf:queryResult>

[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]