### Question:
BatchResource extends RealmAwareResource { @Override public Promise<ActionResponse, ResourceException> actionCollection(Context serverContext, ActionRequest actionRequest) { if (!actionRequest.getAction().equals(BATCH)) { final String msg = "Action '" + actionRequest.getAction() + "' not implemented for this resource"; final NotSupportedException exception = new NotSupportedException(msg); debug.error(msg, exception); return exception.asPromise(); } String scriptId = null; try { JsonValue scriptIdValue = actionRequest.getContent().get(SCRIPT_ID); if (scriptIdValue == null) { if (debug.errorEnabled()) { debug.error("BatchResource :: actionCollection - ScriptId null. Default scripts not implemented."); } return new BadRequestException().asPromise(); } else { scriptId = scriptIdValue.asString(); } final JsonValue requests = actionRequest.getContent().get(REQUESTS); final String realm = getRealm(serverContext); final ScriptConfiguration scriptConfig = scriptingServiceFactory.create(realm).get(scriptId); final ScriptObject script = new ScriptObject(scriptConfig.getName(), scriptConfig.getScript(), scriptConfig.getLanguage()); final ScriptResponse response = new ScriptResponse(); final Bindings bindings = new SimpleBindings(); bindings.put(PAYLOAD, requests); bindings.put(CONTEXT, serverContext); bindings.put(LOGGER, debug); bindings.put(REQUESTER, requester); bindings.put(RESPONSE, response); return newResultPromise(newActionResponse((JsonValue) scriptEvaluator.evaluateScript(script, bindings))); } catch (ScriptException e) { debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId); return exceptionMappingHandler.handleError(serverContext, actionRequest, e).asPromise(); } catch (javax.script.ScriptException e) { debug.error("BatchResource :: actionCollection - Error running script : {}", scriptId); return new InternalServerErrorException().asPromise(); } } @Inject  BatchResource(ScriptEvaluator scriptEvaluator,
                         ScriptingServiceFactory scriptingServiceFactory,
                         @Named("frRest") Debug debug,
                         ExceptionMappingHandler<ScriptException, ResourceException> exceptionMappingHandler,
                         Requester requester); @Override Promise<ActionResponse, ResourceException> actionCollection(Context serverContext,
            ActionRequest actionRequest); @Override Promise<ActionResponse, ResourceException> actionInstance(Context serverContext, String s,
            ActionRequest actionRequest); @Override Promise<ResourceResponse, ResourceException> createInstance(Context serverContext,
            CreateRequest createRequest); @Override Promise<ResourceResponse, ResourceException> deleteInstance(Context serverContext, String s,
            DeleteRequest deleteRequest); @Override Promise<ResourceResponse, ResourceException> patchInstance(Context serverContext, String s,
            PatchRequest patchRequest); @Override Promise<QueryResponse, ResourceException> queryCollection(Context serverContext,
            QueryRequest queryRequest, QueryResourceHandler queryResultHandler); @Override Promise<ResourceResponse, ResourceException> readInstance(Context serverContext, String s,
            ReadRequest readRequest); @Override Promise<ResourceResponse, ResourceException> updateInstance(Context serverContext, String s,
            UpdateRequest updateRequest);  }### Answer:
@Test public void shouldRejectNonBatchActions() { Context mockContext = Mockito.mock(Context.class); ActionRequest mockRequest = mock(ActionRequest.class); given(mockRequest.getAction()).willReturn("false"); @SuppressWarnings("unchecked") ExceptionHandler<ResourceException> handler = mock(ExceptionHandler.class); Promise<ActionResponse, ResourceException> result = batchResource.actionCollection(mockContext, mockRequest); result.thenOnException(handler); verify(handler, times(1)).handleException(any(ResourceException.class)); verifyNoMoreInteractions(handler); }
@Test public void shouldRejectNullScriptId() { Context mockContext = mock(Context.class); ActionRequest action = newActionRequest("batch", "batch"); action.setContent(JsonValueBuilder.toJsonValue("{ \"notScriptId\" : \"blah\" }")); @SuppressWarnings("unchecked") ExceptionHandler<ResourceException> handler = mock(ExceptionHandler.class); Promise<ActionResponse, ResourceException> promise = batchResource.actionCollection(mockContext, action); promise.thenOnException(handler); verify(handler, times(1)).handleException(any(ResourceException.class)); verifyNoMoreInteractions(handler); }