@@ -32,6 +32,7 @@ class EligibilityCheckResourceTest {
3232
3333 private static final String USER_ID = "owner-1" ;
3434 private static final String CHECK_ID = "check-1" ;
35+ private static final String PUBLISHED_PREFIX = "P-" + USER_ID + "-my-module-my-check" ;
3536
3637 private final EligibilityCheckResource resource = new EligibilityCheckResource ();
3738 private final EligibilityCheckRepository repository = mock (EligibilityCheckRepository .class );
@@ -66,6 +67,8 @@ void setUp() throws Exception {
6667 EligibilityCheck check = invocation .getArgument (0 );
6768 return "W-" + check .getOwnerId () + "-" + check .getModule () + "-" + check .getName ();
6869 });
70+ when (repository .getPublishedId (any (), anyString ()))
71+ .thenAnswer (invocation -> PUBLISHED_PREFIX + "-" + invocation .<String >getArgument (1 ));
6972 }
7073
7174 @ Test
@@ -218,6 +221,81 @@ void staleWorkingVersionDoesNotReusePublishedVersion() throws Exception {
218221 assertEquals ("3.0.0" , capturedPublishedVersion ());
219222 }
220223
224+ @ Test
225+ void malformedPublishedVersionsAreIgnored () throws Exception {
226+ workingCheck .setVersion ("2.0.0" );
227+ when (repository .getPublishedCheckVersions (workingCheck ))
228+ .thenReturn (List .of (
229+ publishedVersion ("" ),
230+ publishedVersion ("not-a-version" ),
231+ publishedVersion ("1.invalid.0" ),
232+ publishedVersion ("2.0.0" )
233+ ));
234+
235+ Response response = resource .publishCustomCheck (identity , CHECK_ID );
236+
237+ assertEquals (Response .Status .OK .getStatusCode (), response .getStatus ());
238+ assertEquals ("3.0.0" , capturedPublishedVersion ());
239+ }
240+
241+ // Dot-only strings split into no parts at all, so they must not be read as version 0.0.0
242+ @ Test
243+ void dotOnlyPublishedVersionsAreIgnored () throws Exception {
244+ workingCheck .setVersion ("2.0.0" );
245+ when (repository .getPublishedCheckVersions (workingCheck ))
246+ .thenReturn (List .of (publishedVersion ("." ), publishedVersion ("..." )));
247+
248+ Response response = resource .publishCustomCheck (identity , CHECK_ID );
249+
250+ assertEquals (Response .Status .OK .getStatusCode (), response .getStatus ());
251+ assertEquals ("2.0.0" , capturedPublishedVersion ());
252+ }
253+
254+ /* Ignoring a corrupt version must not hand back a version whose published id is already taken:
255+ that document could never be written, so publishing would fail for good. */
256+ @ Test
257+ void publishSkipsVersionsWhosePublishedIdAlreadyExists () throws Exception {
258+ workingCheck .setVersion ("2.0.0" );
259+ when (repository .getPublishedCheckVersions (workingCheck ))
260+ .thenReturn (List .of (
261+ publishedVersion ("1.0.0" ),
262+ publishedVersion ("v2.0.0" , PUBLISHED_PREFIX + "-2.0.0" )
263+ ));
264+
265+ Response response = resource .publishCustomCheck (identity , CHECK_ID );
266+
267+ assertEquals (Response .Status .OK .getStatusCode (), response .getStatus ());
268+ assertEquals ("3.0.0" , capturedPublishedVersion ());
269+ }
270+
271+ @ Test
272+ void publishKeepsSkippingUntilAnUnusedVersionIsFound () throws Exception {
273+ workingCheck .setVersion ("1.0.0" );
274+ when (repository .getPublishedCheckVersions (workingCheck ))
275+ .thenReturn (List .of (
276+ publishedVersion ("1.0.0" ),
277+ publishedVersion ("v2.0.0" , PUBLISHED_PREFIX + "-2.0.0" ),
278+ publishedVersion ("v3.0.0" , PUBLISHED_PREFIX + "-3.0.0" )
279+ ));
280+
281+ Response response = resource .publishCustomCheck (identity , CHECK_ID );
282+
283+ assertEquals (Response .Status .OK .getStatusCode (), response .getStatus ());
284+ assertEquals ("4.0.0" , capturedPublishedVersion ());
285+ }
286+
287+ // A corrupt working version cannot be published as-is, or the next publish inherits the corruption
288+ @ Test
289+ void firstPublishOfACorruptWorkingVersionStartsAtTheInitialVersion () throws Exception {
290+ workingCheck .setVersion ("not-a-version" );
291+ when (repository .getPublishedCheckVersions (workingCheck )).thenReturn (List .of ());
292+
293+ Response response = resource .publishCustomCheck (identity , CHECK_ID );
294+
295+ assertEquals (Response .Status .OK .getStatusCode (), response .getStatus ());
296+ assertEquals ("1.0.0" , capturedPublishedVersion ());
297+ }
298+
221299 // An unreadable version list must not be mistaken for "never published"
222300 @ Test
223301 void publishFailsWhenPublishedVersionsCannotBeRead () throws Exception {
@@ -232,8 +310,14 @@ void publishFailsWhenPublishedVersionsCannotBeRead() throws Exception {
232310 }
233311
234312 private EligibilityCheck publishedVersion (String version ) {
313+ return publishedVersion (version , PUBLISHED_PREFIX + "-" + version );
314+ }
315+
316+ /* The id is fixed when a version is published, so a version field corrupted later no longer matches it */
317+ private EligibilityCheck publishedVersion (String version , String id ) {
235318 EligibilityCheck published = new EligibilityCheck ("my-check" , "my-module" , "a check" , List .of (), USER_ID );
236319 published .setVersion (version );
320+ published .setId (id );
237321 return published ;
238322 }
239323
0 commit comments