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 @@ ...@@ -102,5 +102,14 @@
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
<resources>
<resource>
<directory>${basedir}</directory>
<filtering>false</filtering>
<includes>
<include>*.txt</include>
</includes>
</resource>
</resources>
</build> </build>
</project> </project>
\ No newline at end of file
...@@ -4,15 +4,12 @@ import java.io.IOException; ...@@ -4,15 +4,12 @@ import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Date; import java.util.Date;
import java.util.Map;
import nesstar.api.common.EGMSResource; import nesstar.api.common.EGMSResource;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.nesstar.oaipmh.NesstarServer; 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.oaipmh.exceptions.IdDoesNotExistException;
import com.nesstar.api.NesstarObject; import com.nesstar.api.NesstarObject;
...@@ -32,7 +29,7 @@ public class GetRecordResult extends OAIResult { ...@@ -32,7 +29,7 @@ public class GetRecordResult extends OAIResult {
private void fetchResource() throws Exception { private void fetchResource() throws Exception {
try { try {
URL url = new URL(identifier); URL url = new URL(identifier);
payload = NesstarServer.getNesstarDB().lookup(url); payload = retriveResourceFromServer(url);
if (payload == null) { if (payload == null) {
logger.fatal("Resource does not exist: " + identifier); logger.fatal("Resource does not exist: " + identifier);
throw new IdDoesNotExistException(identifier); throw new IdDoesNotExistException(identifier);
...@@ -66,4 +63,8 @@ public class GetRecordResult extends OAIResult { ...@@ -66,4 +63,8 @@ public class GetRecordResult extends OAIResult {
public boolean isEGMS() { public boolean isEGMS() {
return payload instanceof EGMSResource; return payload instanceof EGMSResource;
} }
protected NesstarObject retriveResourceFromServer(URL url) throws IOException, Exception {
return NesstarServer.getNesstarDB().lookup(url);
}
} }
package com.nesstar.oaipmh.result; package com.nesstar.oaipmh.result;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import org.apache.log4j.Logger; 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.NesstarServer;
import com.nesstar.oaipmh.util.Properties; import com.nesstar.oaipmh.util.Properties;
...@@ -52,13 +54,13 @@ public class IdentifyResult extends OAIResult { ...@@ -52,13 +54,13 @@ public class IdentifyResult extends OAIResult {
public Date getEarliestDateStamp() throws Exception { public Date getEarliestDateStamp() throws Exception {
//TODO: caching her? JA! Kanskje tilogmed sjekk ved server startup... //TODO: caching her? JA! Kanskje tilogmed sjekk ved server startup...
if (earliestDate == null) { if (earliestDate == null) {
NesstarList<Catalog> catalogs = NesstarServer.getServer().getCatalogs(); List<Catalog> catalogs = getCatalogsFromServer();
searchCatalogAndSetEarliestDate(catalogs); searchCatalogAndSetEarliestDate(catalogs);
} }
return earliestDate; return earliestDate;
} }
protected void searchCatalogAndSetEarliestDate(NesstarList<Catalog> catalogs) { protected void searchCatalogAndSetEarliestDate(List<Catalog> catalogs) {
if (catalogs.size() == 0) { if (catalogs.size() == 0) {
return; return;
} }
...@@ -94,4 +96,14 @@ public class IdentifyResult extends OAIResult { ...@@ -94,4 +96,14 @@ public class IdentifyResult extends OAIResult {
public String getCompression() { public String getCompression() {
return compression; 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 { ...@@ -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(); return NesstarServer.getServer().getStudies();
} }
......
...@@ -27,7 +27,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder { ...@@ -27,7 +27,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
} }
protected MetadataType buildMetadata() throws Exception { protected MetadataType buildMetadata() throws Exception {
study = (Study) ((GetRecordResult) result).getNesstarObject(); study = getStudyFromResult();
xmlDocument = getDDIdocumentFromStudy(study); xmlDocument = getDDIdocumentFromStudy(study);
MetadataType metadataType = new MetadataType(); MetadataType metadataType = new MetadataType();
metadataType.setAny(xmlDocument.getDocumentElement()); metadataType.setAny(xmlDocument.getDocumentElement());
...@@ -37,7 +37,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder { ...@@ -37,7 +37,7 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
protected Document getDDIdocumentFromStudy(Study study) throws Exception { protected Document getDDIdocumentFromStudy(Study study) throws Exception {
Document document = null; Document document = null;
try { try {
InputStream stream = study.getDDI().getStream(); InputStream stream = getInputStreamFromStudy(study);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder(); DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(stream); document = builder.parse(stream);
...@@ -54,4 +54,11 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder { ...@@ -54,4 +54,11 @@ public class DdiRecordBuilder extends GetRecordXmlBuilder {
return document; 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 { ...@@ -117,9 +117,9 @@ public abstract class OAIXmlBuilder {
return dateToXMLGregorianCalendar(today); return dateToXMLGregorianCalendar(today);
} }
protected XMLGregorianCalendar dateToXMLGregorianCalendar(Date today) { protected XMLGregorianCalendar dateToXMLGregorianCalendar(Date someDate) {
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone(TIME_ZONE)); GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone(TIME_ZONE));
calendar.setTime(today); calendar.setTime(someDate);
XMLGregorianCalendar xmlCalendar = null; XMLGregorianCalendar xmlCalendar = null;
try { try {
xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar); xmlCalendar = DatatypeFactory.newInstance().newXMLGregorianCalendar(calendar);
......
# This sets the global logging level and specifies the appenders # This sets the global logging level and specifies the appenders
log4j.rootLogger=rolling, myConsoleAppender log4j.rootLogger=rolling
# 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.appender.rolling=org.apache.log4j.RollingFileAppender log4j.appender.rolling=org.apache.log4j.RollingFileAppender
log4j.appender.rolling.File=${jboss.server.log.dir}/oaipmh.log log4j.appender.rolling.File=${jboss.server.log.dir}/oaipmh.log
log4j.appender.rolling.MaxFileSize=1024KB log4j.appender.rolling.MaxFileSize=1024KB
log4j.appender.rolling.MaxBackupIndex=7 log4j.appender.rolling.MaxBackupIndex=7
log4j.appender.rolling.layout=org.apache.log4j.PatternLayout log4j.appender.rolling.layout=org.apache.log4j.PatternLayout
log4j.appender.rolling.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n log4j.appender.rolling.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
\ No newline at end of file
...@@ -145,7 +145,7 @@ public class OaiPmhServletTest { ...@@ -145,7 +145,7 @@ public class OaiPmhServletTest {
assertEquals("en", language); assertEquals("en", language);
String relation = engine.evaluate("//OAI-PMH/GetRecord/record/metadata/dc/relation", document); 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); String coverage = engine.evaluate("//OAI-PMH/GetRecord/record/metadata/dc/coverage", document);
assertEquals("Thu Jan 01 00:00:00 CET 1987", coverage); 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; 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 org.junit.Test;
import com.nesstar.oaipmh.result.GetRecordResult; 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 { public class DdiRecordBuilderTest extends GetRecordXmlBuilderTest {
private static final String TIMESTAMP = "2011-06-24T11:49:23+0000";
@Test @Test
public void buildDDiXML() throws Exception { 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); 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(); 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; 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.Hashtable;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import org.junit.Before; import org.junit.Before;
import com.nesstar.api.NesstarObject;
import com.nesstar.oaipmh.mock.ServletConfigMock; import com.nesstar.oaipmh.mock.ServletConfigMock;
import com.nesstar.oaipmh.mock.StudyMock;
import com.nesstar.oaipmh.controller.GetRecordController; import com.nesstar.oaipmh.controller.GetRecordController;
import com.nesstar.oaipmh.result.GetRecordResult; import com.nesstar.oaipmh.result.GetRecordResult;
import com.nesstar.oaipmh.util.Properties; import com.nesstar.oaipmh.util.Properties;
...@@ -17,19 +25,19 @@ public abstract class GetRecordXmlBuilderTest { ...@@ -17,19 +25,19 @@ public abstract class GetRecordXmlBuilderTest {
Properties.init(new ServletConfigMock()); Properties.init(new ServletConfigMock());
} }
protected GetRecordResult initResult(String metadataPrefix) throws Exception { protected GetRecordResult initResult(String identifier) throws Exception {
GetRecordResult result = new GetRecordResult(metadataPrefix); GetRecordResult result = new GetRecordResult(identifier) {
protected NesstarObject retriveResourceFromServer(URL url) throws IOException, Exception {
return (NesstarObject) new StudyMock();
}
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"); result.setVerb("GetRecord");
return result; return result;
} }
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);
}
} }
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