Commit 23f0ff82 authored by Ricco Førgaard's avatar Ricco Førgaard

Removed some external dependencies in tests. Now all tests but...

Removed some external dependencies in tests. Now all tests but OaiPmhServletTest should be independant of the API and a running Nesstar server.
Managed to get log4j going when running tests from Maven which makes the output a lot cleaner.
parent 87265caf
......@@ -102,5 +102,14 @@
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>${basedir}</directory>
<filtering>false</filtering>
<includes>
<include>*.txt</include>
</includes>
</resource>
</resources>
</build>
</project>
\ No newline at end of file
......@@ -4,15 +4,12 @@ import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Date;
import java.util.Map;
import nesstar.api.common.EGMSResource;
import org.apache.log4j.Logger;
import com.nesstar.oaipmh.NesstarServer;
import com.nesstar.oaipmh.controller.OAIRequest;
import com.nesstar.oaipmh.controller.RequestParser;
import com.nesstar.oaipmh.exceptions.IdDoesNotExistException;
import com.nesstar.api.NesstarObject;
......@@ -32,7 +29,7 @@ public class GetRecordResult extends OAIResult {
private void fetchResource() throws Exception {
try {
URL url = new URL(identifier);
payload = NesstarServer.getNesstarDB().lookup(url);
payload = retriveResourceFromServer(url);
if (payload == null) {
logger.fatal("Resource does not exist: " + identifier);
throw new IdDoesNotExistException(identifier);
......@@ -66,4 +63,8 @@ public class GetRecordResult extends OAIResult {
public boolean isEGMS() {
return payload instanceof EGMSResource;
}
protected NesstarObject retriveResourceFromServer(URL url) throws IOException, Exception {
return NesstarServer.getNesstarDB().lookup(url);
}
}
package com.nesstar.oaipmh.result;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import com.nesstar.api.*;
import com.nesstar.api.Catalog;
import com.nesstar.api.NesstarList;
import com.nesstar.api.Study;
import com.nesstar.oaipmh.NesstarServer;
import com.nesstar.oaipmh.util.Properties;
......@@ -52,13 +54,13 @@ public class IdentifyResult extends OAIResult {
public Date getEarliestDateStamp() throws Exception {
//TODO: caching her? JA! Kanskje tilogmed sjekk ved server startup...
if (earliestDate == null) {
NesstarList<Catalog> catalogs = NesstarServer.getServer().getCatalogs();
List<Catalog> catalogs = getCatalogsFromServer();
searchCatalogAndSetEarliestDate(catalogs);
}
return earliestDate;
}
protected void searchCatalogAndSetEarliestDate(NesstarList<Catalog> catalogs) {
protected void searchCatalogAndSetEarliestDate(List<Catalog> catalogs) {
if (catalogs.size() == 0) {
return;
}
......@@ -94,4 +96,14 @@ public class IdentifyResult extends OAIResult {
public String getCompression() {
return compression;
}
protected List<Catalog> getCatalogsFromServer() {
List<Catalog> catalogs = new ArrayList<Catalog>();
try {
catalogs = NesstarServer.getServer().getCatalogs();
} catch (Exception e) {
logger.error(e);
}
return catalogs;
}
}
......@@ -27,7 +27,7 @@ public class ListIdentifiersResult extends OAIResult {
}
}
private List<Study> getAllStudies() throws Exception {
protected List<Study> getAllStudies() throws Exception {
return NesstarServer.getServer().getStudies();
}
......
......@@ -27,7 +27,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
}
protected MetadataType buildMetadata() throws Exception {
study = (Study) ((GetRecordResult) result).getNesstarObject();
study = getStudyFromResult();
xmlDocument = getDDIdocumentFromStudy(study);
MetadataType metadataType = new MetadataType();
metadataType.setAny(xmlDocument.getDocumentElement());
......@@ -37,7 +37,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
protected Document getDDIdocumentFromStudy(Study study) throws Exception {
Document document = null;
try {
InputStream stream = study.getDDI().getStream();
InputStream stream = getInputStreamFromStudy(study);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(stream);
......@@ -54,4 +54,11 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
return document;
}
protected Study getStudyFromResult() {
return (Study) ((GetRecordResult) result).getNesstarObject();
}
protected InputStream getInputStreamFromStudy(Study study) throws IOException {
return study.getDDI().getStream();
}
}
......@@ -117,9 +117,9 @@ public abstract class OAIXmlBuilder {
return dateToXMLGregorianCalendar(today);
}
protected XMLGregorianCalendar dateToXMLGregorianCalendar(Date today) {
protected XMLGregorianCalendar dateToXMLGregorianCalendar(Date someDate) {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone(TIME_ZONE));
calendar.setTime(today);
calendar.setTime(someDate);
XMLGregorianCalendar xmlCalendar = null;
try {
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
......
# This sets the global logging level and specifies the appenders
log4j.rootLogger=rolling, myConsoleAppender
# settings for the console appender
log4j.appender.myConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.myConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsoleAppender.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
log4j.rootLogger=rolling
log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=${jboss.server.log.dir}/oaipmh.log
log4j.appender.rolling.MaxFileSize=1024KB
log4j.appender.rolling.MaxBackupIndex=7
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
......@@ -145,7 +145,7 @@ public class OaiPmhServletTest {
assertEquals("en", language);
String relation = engine.evaluate("//OAI-PMH/GetRecord/record/metadata/dc/relation", document);
assertEquals("relation not implemented", relation); //TODO
assertEquals("", relation); //TODO Relation not yet implemented
String coverage = engine.evaluate("//OAI-PMH/GetRecord/record/metadata/dc/coverage", document);
assertEquals("Thu Jan 01 00:00:00 CET 1987", coverage);
......
package com.nesstar.oaipmh.mock;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.EnumSet;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import com.nesstar.api.DdiElement;
import com.nesstar.api.DdiList;
import com.nesstar.api.FileFormat;
import com.nesstar.api.ResultStream;
import com.nesstar.api.VarGroup;
import com.nesstar.api.Variable;
import com.nesstar.api.analysis.Metric;
import com.nesstar.api.analysis.Slice;
import com.nesstar.api.filter.CaseSubset;
public class StudyMock implements com.nesstar.api.Study {
public String getLabel() {
return "mock label";
}
public Date timeStamp() {
final GregorianCalendar calendar = new GregorianCalendar(2011, Calendar.JUNE, 24, 11, 49, 23);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
return calendar.getTime();
}
public URL toURL() {
return null;
}
@Override
public DdiElement getElement(String path) {
return null;
}
@Override
public DdiList<Variable> getVariables() {
return null;
}
@Override
public DdiList<VarGroup> getVarGroups() {
return null;
}
@Override
public ResultStream download(FileFormat format, CaseSubset filter,
Variable... varSubset) throws IOException {
return null;
}
@Override
public ResultStream getDDI() throws IOException {
File ddixmlfile = new File("./src/test/resources/data/ddi.xml");
InputStream inputStream = new FileInputStream(ddixmlfile);
return new ResultStream("text/xml", inputStream);
}
@Override
public Slice tabulate(CaseSubset filter, Variable measureVar,
Variable[] weightVars, EnumSet<Metric> metrics, Variable... breakVars)
throws IOException {
return null;
}
}
package com.nesstar.oaipmh.xml;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import javax.xml.datatype.XMLGregorianCalendar;
import org.junit.Test;
import com.nesstar.oaipmh.result.GetRecordResult;
import com.nesstar.oaipmh.xml.oaipmh.HeaderType;
import com.nesstar.oaipmh.xml.oaipmh.RecordType;
public class DdiRecordBuilderTest extends GetRecordXmlBuilderTest {
private static final String TIMESTAMP = "2011-06-24T11:49:23+0000";
@Test
public void buildDDiXML() throws Exception {
GetRecordResult result = initResult("http://nesstar-nightly.nsd.uib.no:80/obj/fStudy/demo!gor");
String identifier = "http://example.com/identifier";
GetRecordResult result = initResult(identifier);
DdiRecordBuilder builder = new DdiRecordBuilder(result);
assertNotNull(builder.buildHeader());
assertNotNull(builder.buildRecord());
HeaderType headerType = builder.buildHeader();
assertEquals(identifier, headerType.getIdentifier());
assertEquals(TIMESTAMP, headerType.getDatestamp());
RecordType recordType = builder.buildRecord();
assertNotNull(recordType.getMetadata());
builder.buildXML();
XMLGregorianCalendar date = builder.getResponseDate();
assertEquals(2011, date.getYear());
assertEquals(6, date.getMonth());
assertEquals(24, date.getDay());
assertEquals(11, date.getHour());
assertEquals(49, date.getMinute());
assertEquals(23, date.getSecond());
}
}
package com.nesstar.oaipmh.xml;
import java.io.IOException;
import java.net.URL;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.Map;
import java.util.TimeZone;
import org.junit.Before;
import com.nesstar.api.NesstarObject;
import com.nesstar.oaipmh.mock.ServletConfigMock;
import com.nesstar.oaipmh.mock.StudyMock;
import com.nesstar.oaipmh.controller.GetRecordController;
import com.nesstar.oaipmh.result.GetRecordResult;
import com.nesstar.oaipmh.util.Properties;
......@@ -17,19 +25,19 @@ public abstract class GetRecordXmlBuilderTest {
Properties.init(new ServletConfigMock());
}
protected GetRecordResult initResult(String metadataPrefix) throws Exception {
GetRecordResult result = new GetRecordResult(metadataPrefix);
result.setVerb("GetRecord");
return result;
protected GetRecordResult initResult(String identifier) throws Exception {
GetRecordResult result = new GetRecordResult(identifier) {
protected NesstarObject retriveResourceFromServer(URL url) throws IOException, Exception {
return (NesstarObject) new StudyMock();
}
protected GetRecordController initRequest(String metadataPrefix) throws Exception {
Map<String, String[]> arguments = new Hashtable<String, String[]>();
arguments.put("verb", new String[] { "GetRecord" } );
arguments.put("metadataPrefix", new String[] { metadataPrefix } );
arguments.put("identifier", new String[] { "http://nesstar-nightly.nsd.uib.no:80/obj/fStudy/demo!gor" } );
//arguments.put("identifier", new String[] { "http://129.177.92.208:8800/obj/fStudy/demo-gor" } );
return new GetRecordController(arguments);
public Date getResponseDate() {
final GregorianCalendar calendar = new GregorianCalendar(2011, Calendar.JUNE, 24, 11, 49, 23);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
return calendar.getTime();
}
};
result.setVerb("GetRecord");
return result;
}
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment