### Question:
MandatoryCheckInterceptor implements ExecutionPreProcessor { @Override public InterceptorResult invoke(ExecutionContext ctx, OperationKey key, Object[] args) { OperationDefinition operationDefinition = bev.getOperationDefinition(key); Parameter[] parms = operationDefinition.getParameters(); for (int i = 0 ; i < args.length; i++) { if (args[i] == null) { if (parms[i].isMandatory()) { return new InterceptorResult(InterceptorState.FORCE_ON_EXCEPTION, new CougarValidationException(ServerFaultCode.MandatoryNotDefined, "Mandatory attributes not defined for parameter '"+parms[i].getName() + "'")); } } else { try { ValidationUtils.validateMandatory(args[i]); } catch (IllegalArgumentException e) { return new InterceptorResult(InterceptorState.FORCE_ON_EXCEPTION, new CougarValidationException(ServerFaultCode.MandatoryNotDefined, "Embedded fields not defined for parameter '"+parms[i].getName() + "'", e)); } } } return SUCCESS; } MandatoryCheckInterceptor(ExecutionVenue bev); @Override InterceptorResult invoke(ExecutionContext ctx, OperationKey key, Object[] args); String getName(); @Override ExecutionRequirement getExecutionRequirement();  }### Answer:
@Test public void testInterceptorWithMissingNullArgument() { InterceptorResult result = interceptor.invoke(null, mockOperationKey1, new Object[] {null} ); assertNotNull(result); assertEquals(InterceptorState.FORCE_ON_EXCEPTION, result.getState()); assertTrue(result.getResult() instanceof CougarValidationException); }
@Test public void testInterceptorWithCorrectArguments() { InterceptorResult result = interceptor.invoke(null, mockOperationKey1, new Object[] {"1", "2" } ); assertEquals(InterceptorState.CONTINUE, result.getState()); }
@Test public void testInterceptorWithMissingMandatory() { InterceptorResult result = interceptor.invoke(null, mockOperationKey1, new Object[] {null, "2" } ); assertNotNull(result); assertEquals(InterceptorState.FORCE_ON_EXCEPTION, result.getState()); assertTrue(result.getResult() instanceof CougarValidationException); }
@Test public void testInterceptorWithMissingMandatoryAttributesOfCollectionParam() { List<MyComplexObject> l = new ArrayList<MyComplexObject>(); l.add(new MyComplexObject(false)); Map<String,MyComplexObject> m = new HashMap<String, MyComplexObject>(); m.put("bob", new MyComplexObject(true)); InterceptorResult result = interceptor.invoke(null, mockOperationKey2, new Object[] { l, m } ); assertNotNull(result); assertEquals(InterceptorState.FORCE_ON_EXCEPTION, result.getState()); assertTrue(result.getResult() instanceof CougarValidationException); }
@Test public void testInterceptorWithMissingMandatoryAttributesOfCollectionParam2() { List<MyComplexObject> l = new ArrayList<MyComplexObject>(); l.add(new MyComplexObject(false)); InterceptorResult result = interceptor.invoke(null, mockOperationKey2, new Object[] { l, null } ); assertNotNull(result); assertEquals(InterceptorState.FORCE_ON_EXCEPTION, result.getState()); assertTrue(result.getResult() instanceof CougarValidationException); }
@Test public void testInterceptorWithCollections() { List<MyComplexObject> l = new ArrayList<MyComplexObject>(); l.add(new MyComplexObject(true)); Map<String,MyComplexObject> m = new HashMap<String, MyComplexObject>(); m.put("bob", new MyComplexObject(true)); InterceptorResult result = interceptor.invoke(null, mockOperationKey2, new Object[] { l, m } ); assertEquals(InterceptorState.CONTINUE, result.getState()); assertNull(result.getResult()); }