Skip to content

Commit 7cc8c76

Browse files
authored
Merge pull request #1377 from jjones287/feature/fix-tests
Some small fixes to the new integration tests
2 parents c3c8c1c + 46e9e7f commit 7cc8c76

File tree

4 files changed

+71
-88
lines changed

4 files changed

+71
-88
lines changed

src/test/java/org/ecocean/api/EncounterExportImagesTest.java

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.lang.reflect.InvocationTargetException;
1111
import java.nio.file.Files;
1212
import java.nio.file.Path;
13+
import java.nio.file.StandardCopyOption;
1314
import java.util.EnumSet;
1415
import java.util.HashSet;
1516
import java.util.Set;
@@ -462,11 +463,6 @@ private void assertExcelFileEquals(InputStream fis, List<String[]> expectedRows)
462463
String[] expectedRow = expectedRows.get(rowIndex);
463464

464465
assertNotNull(actualRow, "Row " + rowIndex + " should not be null");
465-
466-
// Compare cell count
467-
int actualCellCount = actualRow.getLastCellNum();
468-
assertEquals(expectedRow.length, actualCellCount,
469-
"Row " + rowIndex + " should have " + expectedRow.length + " cells");
470466
// Compare each cell
471467
for (int cellIndex = 0; cellIndex < expectedRow.length; cellIndex++) {
472468
Cell actualCell = actualRow.getCell(cellIndex);
@@ -503,6 +499,10 @@ private void assertExcelFileEquals(InputStream fis, List<String[]> expectedRows)
503499
}
504500
}
505501
}
502+
// check there aren't extra cells in the Excel that we didn't compare
503+
int actualCellCount = actualRow.getLastCellNum();
504+
assertTrue(expectedRow.length >= actualCellCount,
505+
"Row " + rowIndex + " should have " + expectedRow.length + " cells");
506506
}
507507
System.out.println("Excel metadata validation passed - all cells match expected CSV");
508508
}
@@ -530,6 +530,7 @@ private void assertExcelFileEquals(InputStream fis, List<String[]> expectedRows)
530530
.cookie("JSESSIONID", authenticationCookie)
531531
.contentType(ContentType.JSON)
532532
.body(requestBody)
533+
.queryParam("includeMetadata", "true")
533534
.when()
534535
.post("/api/v3/encounters/export")
535536
.then()
@@ -608,7 +609,8 @@ private void assertExcelFileEquals(InputStream fis, List<String[]> expectedRows)
608609
"ZIP should contain Individual_2/ subdirectory");
609610

610611
// Should NOT contain Unidentified_annotations (since unidentifiedEncounters: false)
611-
assertFalse(zipEntries.stream().anyMatch(e -> e.contains(EncounterImageExportFile.UNIDENTIFIED_INDIVIDUAL)),
612+
assertFalse(zipEntries.stream().anyMatch(e -> e.contains(
613+
EncounterImageExportFile.UNIDENTIFIED_INDIVIDUAL)),
612614
"ZIP should NOT contain unidentified annotations (unidentifiedEncounters: false)");
613615

614616
// Should contain actual image files with proper naming convention
@@ -725,14 +727,14 @@ private static void initializeTestData() {
725727

726728
Path assetsRoot = FileSystems.getDefault().getPath("src", "test",
727729
"bulk-images").toAbsolutePath();
728-
AssetStore localStore = new LocalAssetStore("local", assetsRoot,
729-
"file://" + assetsRoot.toString(), false);
730-
MediaAsset asset1 = ((LocalAssetStore)localStore).create(assetsRoot.resolve(
731-
"image-ok-0.jpg").toFile());
732-
MediaAsset asset2 = ((LocalAssetStore)localStore).create(assetsRoot.resolve(
733-
"image-ok-0.jpg").toFile());
734-
MediaAsset asset3 = ((LocalAssetStore)localStore).create(assetsRoot.resolve(
735-
"image-ok-0.jpg").toFile());
730+
Path tmpRoot = FileSystems.getDefault().getPath("/tmp");
731+
File testImage = Files.copy(assetsRoot.resolve("image-ok-0.jpg"),
732+
tmpRoot.resolve("test_image.jpg"), StandardCopyOption.REPLACE_EXISTING).toFile();
733+
AssetStore localStore = new LocalAssetStore("local", tmpRoot, "file://" + tmpRoot,
734+
false);
735+
MediaAsset asset1 = ((LocalAssetStore)localStore).create(testImage);
736+
MediaAsset asset2 = ((LocalAssetStore)localStore).create(testImage);
737+
MediaAsset asset3 = ((LocalAssetStore)localStore).create(testImage);
736738

737739
// Create test encounters
738740
org.ecocean.Encounter enc1 = new org.ecocean.Encounter();

src/test/java/org/ecocean/shepherd/core/ShepherdTest.java

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
import org.mockito.MockedStatic;
1414
import org.mockito.Mockito;
1515

16+
import java.util.Properties;
17+
1618
import static org.junit.jupiter.api.Assertions.*;
1719
import static org.mockito.ArgumentMatchers.anyString;
1820
import static org.mockito.Mockito.*;
1921

2022
public class ShepherdTest {
21-
2223
private MockedStatic<ShepherdPMF> mockedShepherdPMF;
2324
private PersistenceManagerFactory mockPMF;
2425
private PersistenceManager mockPM;
2526
private Transaction mockTransaction;
2627

27-
@BeforeEach
28-
public void setUp() {
28+
@BeforeEach public void setUp() {
2929
// Create mock PersistenceManager and stub critical nested methods
3030
mockTransaction = mock(Transaction.class, RETURNS_DEEP_STUBS);
3131
mockPM = mock(PersistenceManager.class, RETURNS_DEEP_STUBS);
@@ -38,25 +38,23 @@ public void setUp() {
3838
// Open the static mock for ShepherdPMF
3939
mockedShepherdPMF = Mockito.mockStatic(ShepherdPMF.class);
4040
// Configure the behavior for static getPMF()
41-
mockedShepherdPMF.when(() -> ShepherdPMF.getPMF(anyString()))
42-
.thenReturn(mockPMF);
41+
mockedShepherdPMF.when(() -> ShepherdPMF.getPMF(anyString(), any()))
42+
.thenReturn(mockPMF);
4343
}
4444

45-
@AfterEach
46-
public void tearDown() {
45+
@AfterEach public void tearDown() {
4746
// Ensure that the static mock is closed after each test
4847
mockedShepherdPMF.close();
4948
}
5049

51-
@Test
52-
public void testBasicShepherdInitialization() {
50+
@Test public void testBasicShepherdInitialization() {
5351
Shepherd testShepherd = new Shepherd("testContext");
52+
5453
assertEquals("testContext", testShepherd.getContext());
5554
assertEquals(mockPM, testShepherd.getPM());
5655
}
5756

58-
@Test
59-
public void testBeginTransactionWhenInactive() {
57+
@Test public void testBeginTransactionWhenInactive() {
6058
when(mockTransaction.isActive()).thenReturn(false);
6159
Shepherd testShepherd = new Shepherd("testContext");
6260
testShepherd.beginDBTransaction();
@@ -65,8 +63,7 @@ public void testBeginTransactionWhenInactive() {
6563
verify(mockPM, times(1)).addInstanceLifecycleListener(any(), isNull());
6664
}
6765

68-
@Test
69-
public void testBeginTransactionWhenActive() {
66+
@Test public void testBeginTransactionWhenActive() {
7067
when(mockTransaction.isActive()).thenReturn(true);
7168
Shepherd testShepherd = new Shepherd("testContext");
7269
testShepherd.beginDBTransaction();
@@ -76,56 +73,50 @@ public void testBeginTransactionWhenActive() {
7673
verify(mockPM, times(1)).addInstanceLifecycleListener(any(), isNull());
7774
}
7875

79-
@Test
80-
public void testCommitTransactionWhenActive() {
76+
@Test public void testCommitTransactionWhenActive() {
8177
when(mockTransaction.isActive()).thenReturn(true);
8278
Shepherd testShepherd = new Shepherd("testContext");
8379
testShepherd.commitDBTransaction();
8480
verify(mockTransaction, times(1)).commit();
8581
}
8682

87-
@Test
88-
public void testCommitTransactionWhenInactive() {
83+
@Test public void testCommitTransactionWhenInactive() {
8984
when(mockTransaction.isActive()).thenReturn(false);
9085
Shepherd testShepherd = new Shepherd("testContext");
9186
testShepherd.commitDBTransaction();
9287
verify(mockTransaction, times(0)).commit();
9388
}
9489

95-
@Test
96-
public void testRollbackTransactionWhenActive() {
90+
@Test public void testRollbackTransactionWhenActive() {
9791
when(mockTransaction.isActive()).thenReturn(true);
9892
Shepherd testShepherd = new Shepherd("testContext");
9993
testShepherd.rollbackDBTransaction();
10094
verify(mockTransaction, times(1)).rollback();
10195
}
10296

103-
@Test
104-
public void testCheckActiveTransaction() {
97+
@Test public void testCheckActiveTransaction() {
10598
when(mockTransaction.isActive()).thenReturn(true);
10699
Shepherd testShepherd = new Shepherd("testContext");
107100
assertTrue(testShepherd.isDBTransactionActive());
108101
}
109-
@Test
110102

111-
public void testCheckInactiveTransaction() {
103+
@Test public void testCheckInactiveTransaction() {
112104
when(mockTransaction.isActive()).thenReturn(false);
113105
Shepherd testShepherd = new Shepherd("testContext");
114106
assertFalse(testShepherd.isDBTransactionActive());
115107
}
116108

117-
@Test
118-
public void testClosePMWhenOpen() {
109+
@Test public void testClosePMWhenOpen() {
119110
when(mockPM.isClosed()).thenReturn(false);
120111
Shepherd testShepherd = new Shepherd("testContext");
121-
testShepherd.closeDBTransaction(); // note: closeDBTransaction actually closes the persistence manager, not the transaction
112+
testShepherd.closeDBTransaction(); // note: closeDBTransaction actually closes the persistence manager, not the transaction
122113
verify(mockPM, times(1)).close();
123114
}
124115

125-
@Test
126-
public void testSetAction() {
116+
@Test public void testSetAction() {
127117
Shepherd testShepherd = new Shepherd("testContext");
128118
String action = "testAction";
119+
129120
testShepherd.setAction(action);
130121
assertEquals(action, testShepherd.getAction());
131122
}

src/test/java/org/ecocean/shepherd/entity/IndividualTest.java

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public class IndividualTest {
2626
private Transaction mockTransaction;
2727
private Query mockQuery;
2828

29-
@BeforeEach
30-
public void setUp() {
29+
@BeforeEach public void setUp() {
3130
// Create mock PersistenceManager and stub critical nested methods
3231
mockTransaction = mock(Transaction.class, RETURNS_DEEP_STUBS);
3332
mockQuery = mock(Query.class, RETURNS_DEEP_STUBS);
@@ -42,18 +41,17 @@ public void setUp() {
4241
// Open the static mock for ShepherdPMF
4342
mockedShepherdPMF = mockStatic(ShepherdPMF.class);
4443
// Configure the behavior for static getPMF()
45-
mockedShepherdPMF.when(() -> ShepherdPMF.getPMF(anyString()))
46-
.thenReturn(mockPMF);
44+
mockedShepherdPMF.when(() -> ShepherdPMF.getPMF(anyString(), any()))
45+
.thenReturn(mockPMF);
4746
}
4847

49-
@AfterEach
50-
public void tearDown() {
48+
@AfterEach public void tearDown() {
5149
// Ensure that the static mock is closed after each test
5250
mockedShepherdPMF.close();
5351
}
52+
5453
// creation
55-
@Test
56-
public void testStoreNewMarkedIndividual() {
54+
@Test public void testStoreNewMarkedIndividual() {
5755
when(mockTransaction.isActive()).thenReturn(false);
5856
Shepherd testShepherd = spy(new Shepherd("testContext"));
5957
// should a null MarkedIndividual "work" here?
@@ -67,8 +65,7 @@ public void testStoreNewMarkedIndividual() {
6765
}
6866

6967
// behavior should be the same as inactive trans
70-
@Test
71-
public void testStoreNewMarkedIndividualWithActiveTransaction() {
68+
@Test public void testStoreNewMarkedIndividualWithActiveTransaction() {
7269
when(mockTransaction.isActive()).thenReturn(true);
7370
Shepherd testShepherd = spy(new Shepherd("testContext"));
7471
// should a null MarkedIndividual "work" here?
@@ -81,13 +78,11 @@ public void testStoreNewMarkedIndividualWithActiveTransaction() {
8178
assertTrue(returnValue);
8279
}
8380

84-
@Test
85-
public void testStoreNewMarkedIndividualWithException() {
81+
@Test public void testStoreNewMarkedIndividualWithException() {
8682
when(mockTransaction.isActive()).thenReturn(true);
8783
when(mockPM.makePersistent(any())).thenThrow(IllegalStateException.class);
8884
Shepherd testShepherd = spy(new Shepherd("testContext"));
8985
MarkedIndividual markedIndividual = null;
90-
9186
boolean returnValue = testShepherd.storeNewMarkedIndividual(markedIndividual);
9287

9388
assertThrows(IllegalStateException.class, () -> mockPM.makePersistent(any()));
@@ -96,13 +91,13 @@ public void testStoreNewMarkedIndividualWithException() {
9691
assertFalse(returnValue);
9792
}
9893

99-
@Test
100-
public void testStoreNewScheduledIndividualMerge() {
94+
@Test public void testStoreNewScheduledIndividualMerge() {
10195
when(mockTransaction.isActive()).thenReturn(false);
10296
Shepherd testShepherd = spy(new Shepherd("testContext"));
10397
// should a null ScheduledIndividualMerge "work" here?
10498
ScheduledIndividualMerge scheduledIndividualMerge = null;
105-
boolean returnValue = testShepherd.storeNewScheduledIndividualMerge(scheduledIndividualMerge);
99+
boolean returnValue = testShepherd.storeNewScheduledIndividualMerge(
100+
scheduledIndividualMerge);
106101

107102
verify(testShepherd, times(1)).beginDBTransaction();
108103
verify(mockPM, times(1)).makePersistent(scheduledIndividualMerge);
@@ -111,55 +106,51 @@ public void testStoreNewScheduledIndividualMerge() {
111106
}
112107

113108
// deletion
114-
@Test
115-
public void testThrowAwayMarkedIndividual() {
109+
@Test public void testThrowAwayMarkedIndividual() {
116110
MarkedIndividual markedIndividual = spy(new MarkedIndividual());
117111
Shepherd testShepherd = new Shepherd("testContext");
112+
118113
testShepherd.throwAwayMarkedIndividual(markedIndividual);
119114
verify(mockPM, times(1)).deletePersistent(markedIndividual);
120115
verify(markedIndividual, times(1)).opensearchUnindexQuiet();
121116
}
122117

123118
// utilities
124-
@Test
125-
public void testIsMarkedIndividualWithString() {
119+
@Test public void testIsMarkedIndividualWithString() {
126120
when(mockPM.getObjectById(any(), anyBoolean())).thenReturn(new MarkedIndividual());
127121
Shepherd testShepherd = spy(new Shepherd("testContext"));
128122
assertTrue(testShepherd.isMarkedIndividual("testIndividual"));
129123
}
130124

131125
// perhaps the edge case of an empty string should be handled?
132-
@Test
133-
public void testIsMarkedIndividualWithEmptyString() {
126+
@Test public void testIsMarkedIndividualWithEmptyString() {
134127
when(mockPM.getObjectById(any(), anyBoolean())).thenReturn(new MarkedIndividual());
135128
Shepherd testShepherd = spy(new Shepherd("testContext"));
136129
assertTrue(testShepherd.isMarkedIndividual(""));
137130
}
138131

139-
@Test
140-
public void testIsMarkedIndividualWithStringWithException() {
132+
@Test public void testIsMarkedIndividualWithStringWithException() {
141133
when(mockPM.getObjectById(any(), anyBoolean())).thenThrow(JDONullIdentityException.class);
142134
Shepherd testShepherd = spy(new Shepherd("testContext"));
143135
assertFalse(testShepherd.isMarkedIndividual(""));
144136
}
145137

146-
@Test
147-
public void testIsMarkedIndividualWithMIisFalseWithNull() {
138+
@Test public void testIsMarkedIndividualWithMIisFalseWithNull() {
148139
Shepherd testShepherd = spy(new Shepherd("testContext"));
149140
MarkedIndividual markedIndividual = null;
141+
150142
assertFalse(testShepherd.isMarkedIndividual(markedIndividual));
151143
}
152144

153145
// getters
154-
@Test
155-
public void testGetMarkedIndividualQuiet() {
146+
@Test public void testGetMarkedIndividualQuiet() {
156147
when(mockPM.getObjectById(any(), anyBoolean())).thenReturn(new MarkedIndividual());
157148
Shepherd testShepherd = spy(new Shepherd("testContext"));
158-
assertInstanceOf(MarkedIndividual.class, testShepherd.getMarkedIndividualQuiet("some individual"));
149+
assertInstanceOf(MarkedIndividual.class,
150+
testShepherd.getMarkedIndividualQuiet("some individual"));
159151
}
160152

161-
@Test
162-
public void testGetMarkedIndividualByProject() {
153+
@Test public void testGetMarkedIndividualByProject() {
163154
// null project returns empty list?
164155
Project testProject = null;
165156
List<MarkedIndividual> expected = new ArrayList<>();
@@ -170,29 +161,28 @@ public void testGetMarkedIndividualByProject() {
170161
assertEquals(expected, testShepherd.getMarkedIndividualsFromProject(testProject));
171162
}
172163

173-
@Test
174-
public void testGetMarkedIndividualByNullEncounter() {
164+
@Test public void testGetMarkedIndividualByNullEncounter() {
175165
Shepherd testShepherd = new Shepherd("testContext");
176166
Encounter testEncounter = null;
167+
177168
assertNull(testShepherd.getMarkedIndividual(testEncounter));
178169
}
179170

180-
@Test
181-
public void testGetMarkedIndividualByEmptyEncounter() {
171+
@Test public void testGetMarkedIndividualByEmptyEncounter() {
182172
Encounter mockEncounter;
183-
184173
Shepherd testShepherd = new Shepherd("testContext");
174+
185175
mockEncounter = mock(Encounter.class);
186176
when(mockEncounter.getIndividualID()).thenReturn("");
187177
assertNull(testShepherd.getMarkedIndividual(mockEncounter));
188178
}
189179

190180
/*
191-
// todo: verify these are removed or refactored after Shepherd method cleanup.
192-
@Test
193-
public void testGetMarkedIndividualThumbnails() {} // one usage ...
181+
// todo: verify these are removed or refactored after Shepherd method cleanup.
182+
@Test
183+
public void testGetMarkedIndividualThumbnails() {} // one usage ...
194184
195-
@Test
196-
public void testGetMarkedIndividualHard() {} // deprecated
197-
*/
185+
@Test
186+
public void testGetMarkedIndividualHard() {} // deprecated
187+
*/
198188
}

0 commit comments

Comments
 (0)