### Question:
RestUtils { public static String extractAccessKey(HttpServletRequest request) { if (request == null) { return ""; } String accessKey = ""; String auth = request.getHeader(HttpHeaders.AUTHORIZATION); boolean isAnonymousRequest = isAnonymousRequest(request); if (StringUtils.isBlank(auth)) { auth = request.getParameter("X-Amz-Credential"); if (!StringUtils.isBlank(auth)) { accessKey = StringUtils.substringBetween(auth, "=", "/"); } } else { if (isAnonymousRequest) { accessKey = StringUtils.substringAfter(auth, "Anonymous").trim(); } else { accessKey = StringUtils.substringBetween(auth, "=", "/"); } } if (isAnonymousRequest && StringUtils.isBlank(accessKey)) { accessKey = request.getParameter("accessKey"); } return accessKey; } private  RestUtils(); static String extractAccessKey(HttpServletRequest request); static boolean isAnonymousRequest(HttpServletRequest request); static String extractDate(HttpServletRequest request); static String extractResourcePath(HttpServletRequest request); static ParaObject readResourcePath(String appid, String path); static Response getEntity(InputStream is, Class<?> type); static byte[] readEntityBytes(InputStream in); static Response getVotingResponse(ParaObject object, Map<String, Object> entity); static Response getReadResponse(App app, ParaObject content); static Response getCreateResponse(App app, String type, InputStream is); static Response getOverwriteResponse(App app, String id, String type, InputStream is); static Response getUpdateResponse(App app, ParaObject object, InputStream is); static Response getDeleteResponse(App app, ParaObject content); static Response getBatchReadResponse(App app, List<String> ids); static Response getBatchCreateResponse(final App app, InputStream is); static Response getBatchUpdateResponse(App app, Map<String, ParaObject> oldObjects,
			List<Map<String, Object>> newProperties); static Response getBatchDeleteResponse(App app, List<String> ids); static Response readLinksHandler(ParaObject pobj, String id2, String type2,
			MultivaluedMap<String, String> params, Pager pager, boolean childrenOnly); static Response deleteLinksHandler(ParaObject pobj, String id2, String type2, boolean childrenOnly); static Response createLinksHandler(ParaObject pobj, String id2); static Pager getPagerFromParams(MultivaluedMap<String, String> params); static Response getStatusResponse(Response.Status status, String... messages); static void returnStatusResponse(HttpServletResponse response, int status, String message); static void returnObjectResponse(HttpServletResponse response, Object obj); static String pathParam(String param, ContainerRequestContext ctx); static List<String> pathParams(String param, ContainerRequestContext ctx); static String queryParam(String param, ContainerRequestContext ctx); static List<String> queryParams(String param, ContainerRequestContext ctx); static boolean hasQueryParam(String param, ContainerRequestContext ctx);  }### Answer:
@Test public void testExtractAccessKey() { HttpServletRequest req = Mockito.mock(HttpServletRequest.class); Mockito.when(req.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(null); String appid = "app:test-123"; assertEquals("", extractAccessKey(null)); assertEquals(null, extractAccessKey(req)); Mockito.when(req.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("Anonymous " + appid); assertEquals(appid, extractAccessKey(req)); Mockito.when(req.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn(null); Mockito.when(req.getParameter("X-Amz-Credential")).thenReturn("AWS4-HMAC-SHA256 Credential=" + appid + "/123/"); assertEquals(appid, extractAccessKey(req)); Mockito.when(req.getParameter("X-Amz-Credential")).thenReturn(null); Mockito.when(req.getParameter("accessKey")).thenReturn(appid); assertEquals(appid, extractAccessKey(req)); Mockito.when(req.getParameter("accessKey")).thenReturn(null); Mockito.when(req.getHeader(HttpHeaders.AUTHORIZATION)).thenReturn("AWS4-HMAC-SHA256 Credential=" + appid + "/0/"); assertEquals(appid, extractAccessKey(req)); }