Skip to content

Commit

Permalink
added mock behaviour for fixity results
Browse files Browse the repository at this point in the history
  • Loading branch information
fasseg committed May 28, 2013
1 parent f31a4e0 commit a7cc4ed
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 76 deletions.
Expand Up @@ -24,7 +24,7 @@
@Entity
@Table(name = "datastream_results")
@XmlAccessorType(XmlAccessType.FIELD)
public class DatastreamFixity {
public class DatastreamFixity{

private static final Logger logger = LoggerFactory.getLogger(DatastreamFixity.class);

Expand Down Expand Up @@ -63,7 +63,7 @@ public DatastreamFixity() {
super();
}

public long getId() {
public long getId() {
return id;
}

Expand All @@ -75,11 +75,11 @@ public void setType(ResultType type) {
this.type = type;
}

public Date getTimestamp() {
public Date getTimestamp() {
return timestamp;
}

public void setTimestamp(Date timestamp){
public void setTimestamp(Date timestamp){
this.timestamp = timestamp;
}

Expand Down
@@ -1,3 +1,4 @@

package org.fcrepo.services;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -31,23 +32,24 @@
@ContextConfiguration("/context.xml")
public class TestFixityService {

@Inject
private FixityService service;
@Inject
private DatastreamChecksumCheck checksumCheck;
@Inject
private FixityService service;

@Inject
private DatastreamChecksumCheck checksumCheck;

private FixityClient client;
private FixityClient client;

@PostConstruct
public void initMock() {
/* inject the mock client to the datastream checksum check */
this.client = mock(FixityClient.class);
this.checksumCheck.setClient(this.client);
@PostConstruct
public void initMock() {
/* inject the mock client to the datastream checksum check */
this.client = mock(FixityClient.class);
this.checksumCheck.setClient(this.client);

}
}

@Test
public void testFixityService() throws Exception {
@Test
public void testFixityService() throws Exception {
/* Mock object profile */
String ePid = "test:2";
String eDsId = "testds:2";
Expand All @@ -63,19 +65,76 @@ public void testFixityService() throws Exception {
DatastreamFixity fixity = new DatastreamFixity();
fixity.setDatastreamId(eDsId);
fixity.setTimestamp(new Date());
fixity.setType(ResultType.SUCCESS);
when(client.getDatastreamFixity(ePid, eDsId)).thenReturn(fixity);

/* tell the service to check a specific object */
service.checkObject(ePid);

/* let the daemon respond to the request give it 15 secs max */
long started = System.currentTimeMillis();
do {
if (System.currentTimeMillis() - started > 15000) {
throw new Exception("Timeout while waiting for JMS queue");
}
Thread.sleep(500);
} while (service.getResults(ePid).size() == 0);

verify(client).getDatastreamFixity(ePid, eDsId);

List<ObjectFixity> results = service.getResults(ePid);
assertEquals(1, results.size());
ObjectFixity f = results.get(0);
assertEquals(f.getErrors().size(), 0);
assertEquals(f.getSuccesses().size(), 1);
assertEquals(ePid, f.getPid());
}

@Test
public void testChecksumFixityServiceError() throws Exception {

/* Mock object profile */
String ePid = "test:4";
String eDsId = "testds:4";

URI expectedChecksum = URI.create("urn:sha1:ABBA1");

byte[] someData = new byte[16387];
new Random().nextBytes(someData);

/* setup the client mock */
when(client.getDatastreamIds(ePid)).thenReturn(Arrays.asList(eDsId));

DatastreamFixity fixity = new DatastreamFixity();
fixity.setDatastreamId(eDsId);
fixity.setTimestamp(new Date());
fixity.setType(ResultType.ERROR);

FixityProblem prob1 = new FixityProblem();
prob1.cacheId = "id1";
prob1.details = "error occured";
prob1.type = ResultType.ERROR;

FixityProblem prob2 = new FixityProblem();
prob2.cacheId = "id2";
prob2.details = "error occured";
prob2.type = ResultType.REPAIRED;

fixity.setProblems(Arrays.asList(prob1, prob2));

when(client.getDatastreamFixity(ePid, eDsId)).thenReturn(fixity);

/* tell the service to check a specific object */
service.checkObject(ePid);

/* let the daemon respond to the request give it 15 secs max*/
/* let the daemon respond to the request give it 15 secs max */
long started = System.currentTimeMillis();
do{
if (System.currentTimeMillis() - started > 15000){
do {
if (System.currentTimeMillis() - started > 15000) {
throw new Exception("Timeout while waiting for JMS queue");
}
Thread.sleep(500);
}while(service.getResults(ePid).size() == 0);
} while (service.getResults(ePid).size() == 0);

verify(client).getDatastreamFixity(ePid, eDsId);

Expand All @@ -90,67 +149,15 @@ public void testFixityService() throws Exception {
assertEquals(2, numCauses);
int repaired = 0;
int errors = 0;
for (FixityProblem p:errorResult.getProblems()){
for (FixityProblem p : errorResult.getProblems()) {
if (p.type == DatastreamFixity.ResultType.REPAIRED) {
repaired++;
} else errors++;
} else
errors++;
}
assertEquals(ResultType.ERROR, errorResult.getType());
assertEquals(1, repaired);
assertEquals(1, errors);
}

@Test
public void testChecksumFixityServiceError() throws Exception {

/* Mock object profile */
String ePid = "test:2";
String eDsId = "testds:2";

URI expectedChecksum = URI.create("urn:sha1:ABBA1");

byte[] someData = new byte[16387];
new Random().nextBytes(someData);

/* setup the client mock */
when(client.getDatastreamIds(ePid)).thenReturn(Arrays.asList(eDsId));

DatastreamFixity fixity = new DatastreamFixity();
fixity.setDatastreamId(eDsId);
fixity.setTimestamp(new Date());
when(client.getDatastreamFixity(ePid, eDsId)).thenReturn(fixity);

/* tell the service to check a specific object */
service.checkObject(ePid);

/* let the daemon respond to the request give it 15 secs max*/
long started = System.currentTimeMillis();
do{
if (System.currentTimeMillis() - started > 15000){
throw new Exception("Timeout while waiting for JMS queue");
}
Thread.sleep(500);
}while(service.getResults(ePid).size() == 0);
verify(client).getDatastreamFixity(ePid, eDsId);
List<ObjectFixity> results = service.getResults(ePid);
assertEquals(1, results.size());
assertEquals(ePid, results.get(0).getPid());
assertEquals(1, results.get(0).getErrors().size());
DatastreamFixity errorResult = results.get(0).getErrors().get(0);
assertEquals(eDsId, errorResult.getDatastreamId());
assertNotNull(errorResult.getTimestamp());
int numCauses = errorResult.getProblems().size();
assertEquals(2, numCauses);
int repaired = 0;
int errors = 0;
for (FixityProblem p:errorResult.getProblems()){
if (p.type == DatastreamFixity.ResultType.REPAIRED) {
repaired++;
} else errors++;
}
assertEquals(ResultType.ERROR, errorResult.getType());
assertEquals(1, repaired);
assertEquals(1, errors);
}

}

0 comments on commit a7cc4ed

Please sign in to comment.