Skip to content
Snippets Groups Projects
Commit 0f5198c2 authored by Jeff Burke's avatar Jeff Burke
Browse files

s1734: initial JSON reader writers

parent 5de755cb
No related branches found
No related tags found
No related merge requests found
Showing
with 659 additions and 56 deletions
......@@ -113,16 +113,15 @@
</target>
<!-- JAR files needed to run the test suite -->
<property name="gson" value="${ext.lib}/gson.jar" />
<property name="json" value="${ext.lib}/json.jar" />
<property name="easyMock" value="${ext.dev}/easymock.jar" />
<property name="junit" value="${ext.dev}/junit.jar" />
<property name="xmlunit" value="${ext.dev}/xmlunit.jar" />
<property name="xerces" value="${ext.lib}/xerces.jar" />
<property name="cglib" value="${ext.dev}/cglib.jar" />
<property name="objenesis" value="${ext.dev}/objenesis.jar" />
<property name="asm" value="${ext.dev}/asm.jar" />
<property name="testingJars" value="${jars}:${gson}:${easyMock}:${junit}:${xmlunit}:${xerces}:${cglib}:${asm}:${objenesis}" />
<property name="testingJars" value="${jars}:${json}:${easyMock}:${junit}:${xmlunit}:${cglib}:${asm}:${objenesis}" />
<target name="resources">
<copy todir="${build}/class">
......
......@@ -90,13 +90,14 @@
<property name="cadcUtil" value="${lib}/cadcUtil.jar" />
<property name="cadcRegistryClient" value="${lib}/cadcRegistryClient.jar" />
<property name="json" value="${ext.lib}/json.jar" />
<property name="jdom2" value="${ext.lib}/jdom2.jar" />
<property name="javacsv" value="${ext.lib}/javacsv.jar" />
<property name="log4j" value="${ext.lib}/log4j.jar" />
<property name="unboundid" value="${ext.lib}/unboundid-ldapsdk-se.jar" />
<property name="jars" value="${jdom2}:${log4j}:${javacsv}:${unboundid}:${cadcUtil}:${cadcRegistryClient}" />
<property name="jars" value="${json}:${jdom2}:${log4j}:${javacsv}:${unboundid}:${cadcUtil}:${cadcRegistryClient}" />
<target name="build" depends="compile">
<jar jarfile="${build}/lib/${project}.jar"
......@@ -116,4 +117,20 @@
<property name="testingJars" value="${build}/class:${jars}:${xerces}:${asm}:${cglib}:${easymock}:${junit}:${objenesis}" />
<target name="test" depends="compile,compile-test">
<echo message="Running test suite..." />
<junit printsummary="yes" haltonfailure="yes" fork="yes">
<classpath>
<pathelement path="${build}/class"/>
<pathelement path="${build}/test/class"/>
<pathelement path="${testingJars}"/>
</classpath>
<test name="ca.nrc.cadc.ac.json.GroupPropertyReaderWriterTest" />
<test name="ca.nrc.cadc.ac.json.UserDetailsReaderWriterTest" />
<test name="ca.nrc.cadc.ac.json.IdentityReaderWriterTest" />
<test name="ca.nrc.cadc.ac.json.UserReaderWriterTest" />
<formatter type="plain" usefile="false" />
</junit>
</target>
</project>
......@@ -97,6 +97,8 @@ public class Group
public String description;
public Date lastModified;
public Group() {}
/**
* Ctor.
*
......
......@@ -66,7 +66,7 @@
*
************************************************************************
*/
package ca.nrc.cadc.ac.xml;
package ca.nrc.cadc.ac;
import java.io.IOException;
......
......@@ -66,7 +66,7 @@
*
************************************************************************
*/
package ca.nrc.cadc.ac.xml;
package ca.nrc.cadc.ac;
import java.io.IOException;
......
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2014. (c) 2014.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 4 $
*
************************************************************************
*/
package ca.nrc.cadc.ac.json;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserDetails;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.URISyntaxException;
import java.security.Principal;
import java.util.Scanner;
public class UserReader
{
/**
* Construct a User from a InputStream.
*
* @param in InputStream.
* @return User User.
* @throws ReaderException
* @throws IOException
* @throws URISyntaxException
*/
public static User<? extends Principal> read(InputStream in)
throws ReaderException, IOException, URISyntaxException
{
if (in == null)
{
throw new IOException("stream closed");
}
Scanner s = new Scanner(in).useDelimiter("\\A");
String json = s.hasNext() ? s.next() : "";
return read(json);
}
/**
* Construct a User from a Reader.
*
* @param reader Reader.
* @return User User.
* @throws ReaderException
* @throws IOException
*/
public static User<? extends Principal> read(Reader reader)
throws ReaderException, IOException, URISyntaxException
{
if (reader == null)
{
throw new IllegalArgumentException("reader must not be null");
}
Scanner s = new Scanner(reader).useDelimiter("\\A");
String json = s.hasNext() ? s.next() : "";
return read(json);
}
/**
* Construct a User from an JSON String source.
*
* @param json String of JSON.
* @return User User.
* @throws ReaderException
* @throws IOException
* @throws URISyntaxException
*/
public static User<? extends Principal> read(String json)
throws ReaderException, IOException, URISyntaxException
{
if (json == null || json.isEmpty())
{
throw new IllegalArgumentException("JSON must not be null or empty");
}
// Create a JSONObject from the JSON
try
{
return parseUser(new JSONObject(json).getJSONObject("user"));
}
catch (JSONException e)
{
String error = "Unable to parse JSON to User because " +
e.getMessage();
throw new ReaderException(error, e);
}
}
protected static User<? extends Principal> parseUser(JSONObject userObject)
throws ReaderException
{
try
{
JSONObject userIDObject = userObject.getJSONObject("userID");
JSONObject userIDIdentityObject = userIDObject.getJSONObject("identity");
Principal userID = IdentityReader.read(userIDIdentityObject);
User<Principal> user = new User<Principal>(userID);
// identities
JSONArray identitiesArray = userObject.getJSONArray("identities");
for (int i = 0; i < identitiesArray.length(); i++)
{
JSONObject identitiesObject = identitiesArray.getJSONObject(i);
JSONObject identityObject = identitiesObject.getJSONObject(("identity"));
user.getIdentities().add(IdentityReader.read(identityObject));
}
// details
JSONArray detailsArray = userObject.getJSONArray("details");
for (int i = 0; i < detailsArray.length(); i++)
{
JSONObject detailsObject = detailsArray.getJSONObject(i);
JSONObject userDetailsObject = detailsObject.getJSONObject(UserDetails.NAME);
user.details.add(UserDetailsReader.read(userDetailsObject));
}
return user;
}
catch (JSONException e)
{
final String error = "Unable to parse JSON to User because " +
e.getMessage();
throw new ReaderException(error, e);
}
}
}
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2014. (c) 2014.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 4 $
*
************************************************************************
*/
package ca.nrc.cadc.ac.json;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserDetails;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.util.StringBuilderWriter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.security.Principal;
import java.util.Set;
public class UserWriter
{
/**
* Write a User as a JSON string to a StringBuilder.
*
* @param user User to write.
* @param builder StringBuilder to write to.
* @throws IOException if the writer fails to write.
* @throws WriterException
*/
public static void write(User<? extends Principal> user, StringBuilder builder)
throws IOException, WriterException
{
write(user, new StringBuilderWriter(builder));
}
/**
* Write a User as a JSON string to an OutputStream.
*
* @param user User to write.
* @param out OutputStream to write to.
* @throws IOException if the writer fails to write.
* @throws WriterException
*/
public static void write(User<? extends Principal> user, OutputStream out)
throws IOException, WriterException
{
OutputStreamWriter outWriter;
try
{
outWriter = new OutputStreamWriter(out, "UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new RuntimeException("UTF-8 encoding not supported", e);
}
write(user, new BufferedWriter(outWriter));
}
/**
* Write a User as a JSON string to a Writer.
*
* @param user User to write.
* @param writer Writer to write to.
* @throws IOException if the writer fails to write.
* @throws WriterException
*/
public static void write(User<? extends Principal> user, Writer writer)
throws IOException, WriterException
{
if (user == null)
{
throw new WriterException("null User");
}
try
{
getUserJSONObject(user).write(writer);
}
catch (JSONException e)
{
final String error = "Unable to create JSON for User " +
" because " + e.getMessage();
throw new WriterException(error, e);
}
}
/**
* Build a User JSONObject from a User.
*
* @param user User.
* @return JSONObject.
* @throws WriterException
*/
public static JSONObject getUserJSONObject(User<? extends Principal> user)
throws WriterException, JSONException
{
JSONObject userObject = new JSONObject();
JSONObject userIDIdentityObject = new JSONObject();
userIDIdentityObject.put("identity", IdentityWriter.write(user.getUserID()));
userObject.put("userID", userIDIdentityObject);
// identities
Set<Principal> identities = user.getIdentities();
if (!identities.isEmpty())
{
JSONArray identityArray = new JSONArray();
for (Principal identity : identities)
{
JSONObject identityObject = new JSONObject();
identityObject.put("identity" , IdentityWriter.write(identity));
identityArray.put(identityObject);
}
userObject.put("identities", identityArray);
}
// details
if (!user.details.isEmpty())
{
JSONArray detailsArray = new JSONArray();
Set<UserDetails> userDetails = user.details;
for (UserDetails userDetail : userDetails)
{
JSONObject detailsObject = new JSONObject();
detailsObject.put(UserDetails.NAME , UserDetailsWriter.write(userDetail));
detailsArray.put(detailsObject);
}
userObject.put("details", detailsArray);
}
return new JSONObject().put("user", userObject);
}
}
......@@ -68,6 +68,16 @@
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.AC;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.date.DateUtil;
import ca.nrc.cadc.xml.XmlUtil;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
......@@ -80,14 +90,6 @@ import java.text.DateFormat;
import java.text.ParseException;
import java.util.List;
import ca.nrc.cadc.ac.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import ca.nrc.cadc.date.DateUtil;
import ca.nrc.cadc.xml.XmlUtil;
public class GroupReader
{
......
......@@ -68,6 +68,19 @@
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.AC;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.GroupProperty;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.date.DateUtil;
import ca.nrc.cadc.util.StringBuilderWriter;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
......@@ -77,16 +90,6 @@ import java.io.Writer;
import java.security.Principal;
import java.text.DateFormat;
import ca.nrc.cadc.ac.*;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import ca.nrc.cadc.date.DateUtil;
import ca.nrc.cadc.util.StringBuilderWriter;
public class GroupWriter
{
/**
......
......@@ -69,6 +69,7 @@
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.xml.XmlUtil;
import java.io.IOException;
import java.io.InputStream;
......
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.util.StringBuilderWriter;
import java.io.BufferedWriter;
import java.io.IOException;
......
......@@ -68,8 +68,13 @@
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.xml.XmlUtil;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
......@@ -79,9 +84,6 @@ import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.Principal;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
public class UserReader
{
......@@ -185,7 +187,7 @@ public class UserReader
throw new ReaderException(error);
}
Principal userID = ca.nrc.cadc.ac.xml.IdentityReader.read(userIDIdentityElement);
Principal userID = IdentityReader.read(userIDIdentityElement);
User<Principal> user = new User<Principal>(userID);
......@@ -196,7 +198,7 @@ public class UserReader
List<Element> identityElements = identitiesElement.getChildren("identity");
for (Element identityElement : identityElements)
{
user.getIdentities().add(ca.nrc.cadc.ac.xml.IdentityReader.read(identityElement));
user.getIdentities().add(IdentityReader.read(identityElement));
}
}
......@@ -208,7 +210,7 @@ public class UserReader
List<Element> userDetailsElements = detailsElement.getChildren("userDetails");
for (Element userDetailsElement : userDetailsElements)
{
user.details.add(ca.nrc.cadc.ac.xml.UserDetailsReader.read(userDetailsElement));
user.details.add(UserDetailsReader.read(userDetailsElement));
}
}
......
......@@ -68,6 +68,7 @@
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserRequest;
import ca.nrc.cadc.xml.XmlUtil;
......
......@@ -70,6 +70,7 @@
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.UserRequest;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.util.StringBuilderWriter;
import org.jdom2.Document;
import org.jdom2.Element;
......
......@@ -70,7 +70,13 @@ package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.UserDetails;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.util.StringBuilderWriter;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
......@@ -79,10 +85,6 @@ import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.security.Principal;
import java.util.Set;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
public class UserWriter
{
......@@ -157,7 +159,7 @@ public class UserWriter
// userID element
Element userIDElement = new Element("userID");
userIDElement.addContent(ca.nrc.cadc.ac.xml.IdentityWriter.write(user.getUserID()));
userIDElement.addContent(IdentityWriter.write(user.getUserID()));
userElement.addContent(userIDElement);
// identities
......@@ -167,7 +169,7 @@ public class UserWriter
Element identitiesElement = new Element("identities");
for (Principal identity : identities)
{
identitiesElement.addContent(ca.nrc.cadc.ac.xml.IdentityWriter.write(identity));
identitiesElement.addContent(IdentityWriter.write(identity));
}
userElement.addContent(identitiesElement);
}
......@@ -179,7 +181,7 @@ public class UserWriter
Set<UserDetails> userDetails = user.details;
for (UserDetails userDetail : userDetails)
{
detailsElement.addContent(ca.nrc.cadc.ac.xml.UserDetailsWriter.write(userDetail));
detailsElement.addContent(UserDetailsWriter.write(userDetail));
}
userElement.addContent(detailsElement);
}
......
/*
************************************************************************
******************* CANADIAN ASTRONOMY DATA CENTRE *******************
************** CENTRE CANADIEN DE DONNÉES ASTRONOMIQUES **************
*
* (c) 2014. (c) 2014.
* Government of Canada Gouvernement du Canada
* National Research Council Conseil national de recherches
* Ottawa, Canada, K1A 0R6 Ottawa, Canada, K1A 0R6
* All rights reserved Tous droits réservés
*
* NRC disclaims any warranties, Le CNRC dénie toute garantie
* expressed, implied, or énoncée, implicite ou légale,
* statutory, of any kind with de quelque nature que ce
* respect to the software, soit, concernant le logiciel,
* including without limitation y compris sans restriction
* any warranty of merchantability toute garantie de valeur
* or fitness for a particular marchande ou de pertinence
* purpose. NRC shall not be pour un usage particulier.
* liable in any event for any Le CNRC ne pourra en aucun cas
* damages, whether direct or être tenu responsable de tout
* indirect, special or general, dommage, direct ou indirect,
* consequential or incidental, particulier ou général,
* arising from the use of the accessoire ou fortuit, résultant
* software. Neither the name de l'utilisation du logiciel. Ni
* of the National Research le nom du Conseil National de
* Council of Canada nor the Recherches du Canada ni les noms
* names of its contributors may de ses participants ne peuvent
* be used to endorse or promote être utilisés pour approuver ou
* products derived from this promouvoir les produits dérivés
* software without specific prior de ce logiciel sans autorisation
* written permission. préalable et particulière
* par écrit.
*
* This file is part of the Ce fichier fait partie du projet
* OpenCADC project. OpenCADC.
*
* OpenCADC is free software: OpenCADC est un logiciel libre ;
* you can redistribute it and/or vous pouvez le redistribuer ou le
* modify it under the terms of modifier suivant les termes de
* the GNU Affero General Public la “GNU Affero General Public
* License as published by the License” telle que publiée
* Free Software Foundation, par la Free Software Foundation
* either version 3 of the : soit la version 3 de cette
* License, or (at your option) licence, soit (à votre gré)
* any later version. toute version ultérieure.
*
* OpenCADC is distributed in the OpenCADC est distribué
* hope that it will be useful, dans l’espoir qu’il vous
* but WITHOUT ANY WARRANTY; sera utile, mais SANS AUCUNE
* without even the implied GARANTIE : sans même la garantie
* warranty of MERCHANTABILITY implicite de COMMERCIALISABILITÉ
* or FITNESS FOR A PARTICULAR ni d’ADÉQUATION À UN OBJECTIF
* PURPOSE. See the GNU Affero PARTICULIER. Consultez la Licence
* General Public License for Générale Publique GNU Affero
* more details. pour plus de détails.
*
* You should have received Vous devriez avoir reçu une
* a copy of the GNU Affero copie de la Licence Générale
* General Public License along Publique GNU Affero avec
* with OpenCADC. If not, see OpenCADC ; si ce n’est
* <http://www.gnu.org/licenses/>. pas le cas, consultez :
* <http://www.gnu.org/licenses/>.
*
* $Revision: 4 $
*
************************************************************************
*/
package ca.nrc.cadc.ac.json;
import ca.nrc.cadc.ac.PersonalDetails;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.auth.NumericPrincipal;
import org.apache.log4j.Logger;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.security.Principal;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
/**
*
* @author jburke
*/
public class UserReaderWriterTest
{
private static Logger log = Logger.getLogger(UserReaderWriterTest.class);
@Test
public void testReaderExceptions()
throws Exception
{
try
{
String s = null;
User<? extends Principal> u = UserReader.read(s);
fail("null String should throw IllegalArgumentException");
}
catch (IllegalArgumentException e) {}
try
{
InputStream in = null;
User<? extends Principal> u = UserReader.read(in);
fail("null InputStream should throw IOException");
}
catch (IOException e) {}
try
{
Reader r = null;
User<? extends Principal> u = UserReader.read(r);
fail("null Reader should throw IllegalArgumentException");
}
catch (IllegalArgumentException e) {}
}
@Test
public void testWriterExceptions()
throws Exception
{
try
{
UserWriter.write(null, new StringBuilder());
fail("null User should throw WriterException");
}
catch (WriterException e) {}
}
@Test
public void testReadWrite()
throws Exception
{
User<? extends Principal> expected = new User<Principal>(new HttpPrincipal("foo"));
expected.getIdentities().add(new NumericPrincipal(123l));
expected.details.add(new PersonalDetails("firstname", "lastname"));
StringBuilder json = new StringBuilder();
UserWriter.write(expected, json);
assertFalse(json.toString().isEmpty());
User<? extends Principal> actual = UserReader.read(json.toString());
assertNotNull(actual);
assertEquals(expected, actual);
}
}
......@@ -69,8 +69,13 @@
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.GroupProperty;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.util.Log4jInit;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.jdom2.Element;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
......@@ -82,6 +87,12 @@ public class GroupPropertyReaderWriterTest
{
private static Logger log = Logger.getLogger(GroupPropertyReaderWriterTest.class);
@BeforeClass
public static void setUpClass()
{
Log4jInit.setLevel("ca.nrc.cadc.ac.xml", Level.INFO);
}
@Test
public void testReaderExceptions()
throws Exception
......@@ -92,7 +103,7 @@ public class GroupPropertyReaderWriterTest
GroupProperty gp = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
fail("null element should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element = new Element("foo");
try
......@@ -100,7 +111,7 @@ public class GroupPropertyReaderWriterTest
GroupProperty gp = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
fail("element not named 'property' should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element = new Element("property");
try
......@@ -108,7 +119,7 @@ public class GroupPropertyReaderWriterTest
GroupProperty gp = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
fail("element without 'key' attribute should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element.setAttribute("key", "foo");
try
......@@ -116,7 +127,7 @@ public class GroupPropertyReaderWriterTest
GroupProperty gp = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
fail("element without 'type' attribute should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element.setAttribute("type", "Double");
try
......@@ -124,7 +135,7 @@ public class GroupPropertyReaderWriterTest
GroupProperty gp = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
fail("Unsupported 'type' should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
}
@Test
......@@ -136,7 +147,7 @@ public class GroupPropertyReaderWriterTest
Element element = ca.nrc.cadc.ac.xml.GroupPropertyWriter.write(null);
fail("null GroupProperty should throw WriterException");
}
catch (ca.nrc.cadc.ac.xml.WriterException e) {}
catch (WriterException e) {}
GroupProperty gp = new GroupProperty("key", new Double(1.0), true);
try
......@@ -153,20 +164,20 @@ public class GroupPropertyReaderWriterTest
{
// String type
GroupProperty expected = new GroupProperty("key", "value", true);
Element element = ca.nrc.cadc.ac.xml.GroupPropertyWriter.write(expected);
Element element = GroupPropertyWriter.write(expected);
assertNotNull(element);
GroupProperty actual = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
GroupProperty actual = GroupPropertyReader.read(element);
assertNotNull(actual);
assertEquals(expected, actual);
// Integer tuype
expected = new GroupProperty("key", new Integer(1), false);
element = ca.nrc.cadc.ac.xml.GroupPropertyWriter.write(expected);
element = GroupPropertyWriter.write(expected);
assertNotNull(element);
actual = ca.nrc.cadc.ac.xml.GroupPropertyReader.read(element);
actual = GroupPropertyReader.read(element);
assertNotNull(actual);
assertEquals(expected, actual);
......
......@@ -84,6 +84,7 @@ import javax.security.auth.x500.X500Principal;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.GroupProperty;
import ca.nrc.cadc.ac.User;
import ca.nrc.cadc.ac.WriterException;
import org.apache.log4j.Logger;
import org.junit.Test;
......@@ -137,7 +138,7 @@ public class GroupReaderWriterTest
ca.nrc.cadc.ac.xml.GroupWriter.write(null, new StringBuilder());
fail("null Group should throw WriterException");
}
catch (ca.nrc.cadc.ac.xml.WriterException e) {}
catch (WriterException e) {}
}
@Test
......
......@@ -75,6 +75,7 @@ import java.util.ArrayList;
import java.util.List;
import ca.nrc.cadc.ac.Group;
import ca.nrc.cadc.ac.WriterException;
import org.apache.log4j.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
......@@ -128,7 +129,7 @@ public class GroupsReaderWriterTest
ca.nrc.cadc.ac.xml.GroupsWriter.write(null, new StringBuilder());
fail("null Group should throw WriterException");
}
catch (ca.nrc.cadc.ac.xml.WriterException e) {}
catch (WriterException e) {}
}
@Test
......
......@@ -68,6 +68,8 @@
*/
package ca.nrc.cadc.ac.xml;
import ca.nrc.cadc.ac.ReaderException;
import ca.nrc.cadc.ac.WriterException;
import ca.nrc.cadc.auth.HttpPrincipal;
import ca.nrc.cadc.auth.NumericPrincipal;
import ca.nrc.cadc.auth.OpenIdPrincipal;
......@@ -97,7 +99,7 @@ public class IdentityReaderWriterTest
Principal p = ca.nrc.cadc.ac.xml.IdentityReader.read(element);
fail("null element should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element = new Element("foo");
try
......@@ -105,7 +107,7 @@ public class IdentityReaderWriterTest
Principal p = ca.nrc.cadc.ac.xml.IdentityReader.read(element);
fail("element not named 'identity' should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element = new Element("identity");
try
......@@ -113,7 +115,7 @@ public class IdentityReaderWriterTest
Principal p = ca.nrc.cadc.ac.xml.IdentityReader.read(element);
fail("element without 'type' attribute should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
element.setAttribute("type", "foo");
try
......@@ -121,7 +123,7 @@ public class IdentityReaderWriterTest
Principal p = ca.nrc.cadc.ac.xml.IdentityReader.read(element);
fail("element with unknown 'type' attribute should throw ReaderException");
}
catch (ca.nrc.cadc.ac.xml.ReaderException e) {}
catch (ReaderException e) {}
}
@Test
......@@ -133,7 +135,7 @@ public class IdentityReaderWriterTest
Element element = ca.nrc.cadc.ac.xml.IdentityWriter.write(null);
fail("null Identity should throw WriterException");
}
catch (ca.nrc.cadc.ac.xml.WriterException e) {}
catch (WriterException e) {}
Principal p = new JMXPrincipal("foo");
try
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment