UniqueByArgConstraint implements JobConstraint { @Override public Collection<String> test(AsyncJobStatusCurator jobCurator, AsyncJobStatus inbound) { if (jobCurator == null) { throw new IllegalArgumentException("jobCurator is null"); } if (inbound == null) { throw new IllegalArgumentException("inbound is null"); } String jobKey = inbound.getJobKey(); JobArguments jobArgs = inbound.getJobArguments(); Map<String, String> argMap = new HashMap<>(); for (String param : this.params) { argMap.put(param, jobArgs.getSerializedValue(param)); } return jobCurator.fetchJobIdsByArguments(jobKey, argMap); } UniqueByArgConstraint(String... params);  UniqueByArgConstraint(List<String> params); @Override Collection<String> test(AsyncJobStatusCurator jobCurator, AsyncJobStatus inbound);  }
@Test public void testStandardMatching() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("param1", "val1")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testStandardMatchingFromCollection() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("param1", "val1")); JobConstraint constraint = new UniqueByArgConstraint(Collections.singletonList("param1")); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMatchingOnJobsWithMultipleParams() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "paramA", "valA", "param2", "val2", "paramC", "valC")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param2"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMatchingOnJobsWithMultipleParamsFromCollection() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "paramA", "valA", "param2", "val2", "paramC", "valC")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint(Collections.singletonList("param2")); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMultiMatchingOnJobsWithMultipleParams() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMultiMatchingOnJobsWithMultipleParamsFromCollection() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint(Arrays.asList("param1", "param2", "param3")); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMultiMatchingOnJobsWithMultipleParamsOutOfOrder() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param2", "param1", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMultiMatchingOnJobsWithMultipleParamsOutOfOrderFromCollection() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint(Arrays.asList("param2", "param1", "param3")); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testMultiMatchingOnJobsWithMultipleParamsAndExtraParams() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "paramA", "valA", "param1", "val1", "paramB", "valB", "param2", "val2", "paramC", "valC", "param3", "val3", "paramD", "valD")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "paramR", "valR", "param1", "val1", "paramS", "valS", "param2", "val2", "paramT", "valT", "param3", "val3", "paramU", "valU")); JobConstraint constraint = new UniqueByArgConstraint("param2", "param1", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing.getId())); }
@Test public void testNoMatchOnKeyMismatch() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing = this.persistJobStatus("existing", "alt_key", this.map("param1", "val1")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testNoMatchOnKeyMismatchWithMultiParam() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "alt_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testNoMatchOnParamMismatch() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("paramX", "val1")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testMultiParamsNoMatchOnParamMismatch() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "paramX", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testNoMatchOnArgMismatch() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("param1", "valX")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testMultiParamsNoMatchOnArgMismatch() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "valX", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testNoMatchWhenInboundJobLacksParam() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param2", "val2")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("param1", null)); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testMultiParamNoMatchWhenInboundJobLacksParam() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testNoMatchWhenExistingJobsLackParam() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", null)); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map("param2", "val2")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testMultiParamNoMatchWhenExistingJobsLackParam() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map( "param1", "val1", "param2", "val2", "param3", "val3")); AsyncJobStatus existing = this.persistJobStatus("existing", "test_key", this.map( "param1", "val1", "param3", "val3")); JobConstraint constraint = new UniqueByArgConstraint("param1", "param2", "param3"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertTrue(result == null || result.isEmpty()); }
@Test public void testMultipleExistingJobsMatchesCorrectJob() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing1 = this.persistJobStatus("existing1", "test_key", this.map("param1", "val3")); AsyncJobStatus existing2 = this.persistJobStatus("existing2", "test_key", this.map("param1", "val1")); AsyncJobStatus existing3 = this.persistJobStatus("existing3", "test_key", this.map("param1", "val2")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(1, result.size()); assertThat(result, hasItem(existing2.getId())); }
@Test public void testMultipleExistingJobsMatchesMultipleCorrectJobs() { AsyncJobStatus inbound = this.buildJobStatus("inbound", "test_key", this.map("param1", "val1")); AsyncJobStatus existing1 = this.persistJobStatus("existing1", "test_key", this.map("param1", "val1")); AsyncJobStatus existing2 = this.persistJobStatus("existing2", "test_key", this.map("param1", "val2")); AsyncJobStatus existing3 = this.persistJobStatus("existing3", "test_key", this.map("param1", "val1")); JobConstraint constraint = new UniqueByArgConstraint("param1"); Collection<String> result = constraint.test(this.asyncJobCurator, inbound); assertNotNull(result); assertEquals(2, result.size()); assertThat(result, hasItem(existing1.getId())); assertThat(result, hasItem(existing3.getId())); }