-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #156 from europeana/MET-1053
MET-1053 , RepresentationIterator throws null pointer exception in a …
- Loading branch information
Showing
3 changed files
with
151 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...ce/mcs/driver/src/test/java/eu/europeana/cloud/mcs/driver/RepresentationIteratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package eu.europeana.cloud.mcs.driver; | ||
|
||
import eu.europeana.cloud.common.model.Representation; | ||
import eu.europeana.cloud.common.response.ResultSlice; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Matchers.eq; | ||
import static org.mockito.Matchers.notNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
|
||
/** | ||
* Created by Tarek on 5/24/2018. | ||
*/ | ||
public class RepresentationIteratorTest { | ||
|
||
public static final String NEXT_SLICE = "nextSlice"; | ||
private DataSetServiceClient dataSetServiceClient; | ||
private static final String PROVIDER = "PROVIDER"; | ||
private static final String DATASET = "DATASET"; | ||
|
||
|
||
@Before | ||
public void init() { | ||
dataSetServiceClient = mock(DataSetServiceClient.class); | ||
} | ||
|
||
@Test | ||
public void testRepresentationIteratorForTheFirstIteration() throws Exception { | ||
RepresentationIterator representationIterator = new RepresentationIterator(dataSetServiceClient, PROVIDER, DATASET); | ||
ResultSlice<Representation> representationResultSlice = getFinalRepresentationResultSlice(2); | ||
|
||
when(dataSetServiceClient.getDataSetRepresentationsChunk(PROVIDER, DATASET, null)).thenReturn(representationResultSlice); | ||
int count = 0; | ||
|
||
while (representationIterator.hasNext()) { | ||
assertTrue(representationIterator.hasNext()); | ||
representationIterator.next(); | ||
count++; | ||
} | ||
assertEquals(count, 2); | ||
verify(dataSetServiceClient, times(0)).getDataSetRepresentationsChunk(eq(PROVIDER), eq(DATASET), notNull(String.class)); | ||
assertFalse(representationIterator.hasNext()); | ||
|
||
|
||
} | ||
|
||
@Test | ||
public void testRepresentationIteratorWhenTheSecondIterationIsEmpty() throws Exception { | ||
RepresentationIterator representationIterator = new RepresentationIterator(dataSetServiceClient, PROVIDER, DATASET); | ||
|
||
ResultSlice<Representation> representationResultSlice = getRepresentationResultSlice(100); | ||
|
||
when(dataSetServiceClient.getDataSetRepresentationsChunk(PROVIDER, DATASET, null)).thenReturn(representationResultSlice); | ||
|
||
|
||
ResultSlice<Representation> emptyResultSet = new ResultSlice<>(); | ||
emptyResultSet.setResults(new ArrayList<Representation>()); | ||
when(dataSetServiceClient.getDataSetRepresentationsChunk(eq(PROVIDER), eq(DATASET), eq(NEXT_SLICE))).thenReturn(emptyResultSet); | ||
|
||
int count = 0; | ||
while (representationIterator.hasNext()) { | ||
assertTrue(representationIterator.hasNext()); | ||
representationIterator.next(); | ||
count++; | ||
} | ||
assertEquals(100, count); | ||
verify(dataSetServiceClient, times(1)).getDataSetRepresentationsChunk(eq(PROVIDER), eq(DATASET), eq(NEXT_SLICE)); | ||
assertFalse(representationIterator.hasNext()); | ||
|
||
|
||
} | ||
|
||
|
||
@Test | ||
public void testRepresentationIteratorWhenTheSecondIterationNotEmpty() throws Exception { | ||
RepresentationIterator representationIterator = new RepresentationIterator(dataSetServiceClient, PROVIDER, DATASET); | ||
|
||
ResultSlice<Representation> representationResultSlice = getRepresentationResultSlice(100); | ||
when(dataSetServiceClient.getDataSetRepresentationsChunk(PROVIDER, DATASET, null)).thenReturn(representationResultSlice); | ||
|
||
ResultSlice<Representation> nextResultSet = getFinalRepresentationResultSlice(5); | ||
when(dataSetServiceClient.getDataSetRepresentationsChunk(eq(PROVIDER), eq(DATASET), eq(NEXT_SLICE))).thenReturn(nextResultSet); | ||
|
||
int count = 0; | ||
|
||
while (representationIterator.hasNext()) { | ||
assertTrue(representationIterator.hasNext()); | ||
representationIterator.next(); | ||
count++; | ||
} | ||
assertEquals(105, count); | ||
verify(dataSetServiceClient, times(1)).getDataSetRepresentationsChunk(eq(PROVIDER), eq(DATASET), eq(NEXT_SLICE)); | ||
assertFalse(representationIterator.hasNext()); | ||
|
||
|
||
} | ||
|
||
|
||
private ResultSlice<Representation> getRepresentationResultSlice(int ItemNumber) { | ||
List<Representation> representationList = new ArrayList<>(ItemNumber); | ||
for (int i = 0; i < ItemNumber; i++) { | ||
representationList.add(new Representation()); | ||
} | ||
|
||
ResultSlice<Representation> representationResultSlice = new ResultSlice<>(); | ||
representationResultSlice.setResults(representationList); | ||
representationResultSlice.setNextSlice(NEXT_SLICE); | ||
return representationResultSlice; | ||
} | ||
|
||
private ResultSlice<Representation> getFinalRepresentationResultSlice(int ItemNumber) { | ||
ResultSlice<Representation> representationResultSlice = getRepresentationResultSlice(ItemNumber); | ||
representationResultSlice.setNextSlice(null); | ||
return representationResultSlice; | ||
} | ||
|
||
|
||
} |