### Question:
DefaultQueryLaunchingConstraintsChecker implements QueryLaunchingConstraintsChecker { @Override public boolean canLaunch(final QueryContext candidateQuery, final EstimatedImmutableQueryCollection launchedQueries) { Set<QueryLaunchingConstraint> allConstraints = prepareAllConstraints(candidateQuery); for (QueryLaunchingConstraint queryConstraint : allConstraints) { String launchRejectionMessage = queryConstraint.allowsLaunchOf(candidateQuery, launchedQueries); if (launchRejectionMessage != null) { log.info("query {} not allowed to launch. Constraint failed: {} with message: {}", candidateQuery, queryConstraint, launchRejectionMessage); candidateQuery.getStatus().setProgressMessage(launchRejectionMessage); return false; } } return true; } DefaultQueryLaunchingConstraintsChecker(
    @NonNull final ImmutableSet<QueryLaunchingConstraint> lensQueryConstraints); @Override boolean canLaunch(final QueryContext candidateQuery, final EstimatedImmutableQueryCollection launchedQueries);  }### Answer:
@Test public void testCanLaunchShouldReturnFalseWhenAtleastOneConstraintFails() { QueryContext mockCandidateQuery = mock(QueryContext.class); EstimatedImmutableQueryCollection mockRunningQueries = mock(EstimatedImmutableQueryCollection.class); final QueryLaunchingConstraint constraint1 = mock(QueryLaunchingConstraint.class); final QueryLaunchingConstraint constraint2 = mock(QueryLaunchingConstraint.class); QueryLaunchingConstraintsChecker constraintsChecker = new DefaultQueryLaunchingConstraintsChecker(ImmutableSet.of(constraint1, constraint2)); QueryStatus status = QueryStatus.getQueuedStatus(); final QueryLaunchingConstraint driverConstraint = mock(QueryLaunchingConstraint.class); when(mockCandidateQuery.getSelectedDriverQueryConstraints()).thenReturn(ImmutableSet.of(driverConstraint)); when(mockCandidateQuery.getStatus()).thenReturn(status); when(constraint1.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)).thenReturn(null); when(constraint2.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)).thenReturn("constraint 2 failed"); when(driverConstraint.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)) .thenReturn("driver constraint failed"); boolean canLaunchQuery = constraintsChecker.canLaunch(mockCandidateQuery, mockRunningQueries); Assert.assertFalse(canLaunchQuery); Assert.assertEquals(mockCandidateQuery.getStatus().getProgressMessage(), "constraint 2 failed"); }
@Test public void testCanLaunchShouldReturnTrueWhenAllConstraintPass() { QueryContext mockCandidateQuery = mock(QueryContext.class); EstimatedImmutableQueryCollection mockRunningQueries = mock(EstimatedImmutableQueryCollection.class); final QueryLaunchingConstraint constraint1 = mock(QueryLaunchingConstraint.class); final QueryLaunchingConstraint constraint2 = mock(QueryLaunchingConstraint.class); QueryLaunchingConstraintsChecker constraintsChecker = new DefaultQueryLaunchingConstraintsChecker(ImmutableSet.of(constraint1, constraint2)); final QueryLaunchingConstraint driverConstraint = mock(QueryLaunchingConstraint.class); when(mockCandidateQuery.getSelectedDriverQueryConstraints()).thenReturn(ImmutableSet.of(driverConstraint)); when(constraint1.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)).thenReturn(null); when(constraint2.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)).thenReturn(null); when(driverConstraint.allowsLaunchOf(mockCandidateQuery, mockRunningQueries)).thenReturn(null); boolean canLaunchQuery = constraintsChecker.canLaunch(mockCandidateQuery, mockRunningQueries); Assert.assertTrue(canLaunchQuery); }
@Test public void testCanLaunchShouldReturnTrueWhenConstraintsSetIsEmpty() { QueryContext mockCandidateQuery = mock(QueryContext.class); EstimatedImmutableQueryCollection mockRunningQueries = mock(EstimatedImmutableQueryCollection.class); ImmutableSet<QueryLaunchingConstraint> emptySetOfConstraints = ImmutableSet.copyOf(Sets.<QueryLaunchingConstraint>newHashSet()); when(mockCandidateQuery.getSelectedDriverQueryConstraints()).thenReturn(emptySetOfConstraints); QueryLaunchingConstraintsChecker constraintsChecker = new DefaultQueryLaunchingConstraintsChecker(ImmutableSet.copyOf(emptySetOfConstraints)); boolean canLaunchQuery = constraintsChecker.canLaunch(mockCandidateQuery, mockRunningQueries); Assert.assertTrue(canLaunchQuery); }