### Question:
ResourceExtensionResolver { public Long resolvePageId(APISession apiSession) throws BonitaException { final HttpServletRequest httpServletRequest = getHttpServletRequest(); final PageReference pageReference; pageReference = pageMappingService.getPage(httpServletRequest, apiSession, generateMappingKey(), httpServletRequest.getLocale(), false); return pageReference.getPageId(); } ResourceExtensionResolver(Request request, PageMappingService pageMappingService); Long resolvePageId(APISession apiSession); String generateMappingKey(); File resolveRestApiControllerFile(PageResourceProviderImpl pageResourceProvider); static final String MAPPING_KEY_SEPARATOR; static final String MAPPING_KEY_PREFIX; static final String API_EXTENSION_TEMPLATE_PREFIX; }### Answer:
@Test public void should_resolve_pageId() throws Exception { final Request request = new Request(Method.POST, "/bonita/API/extension/myPostResource"); doReturn(Locale.FRENCH).when(httpServletRequest).getLocale(); doReturn(pageReference).when(pageMappingService).getPage(httpServletRequest, apiSession, API_EXTENSION_POST_MAPPING_KEY, Locale.FRENCH, false); doReturn(PAGE_ID).when(pageReference).getPageId(); final ResourceExtensionResolver resourceExtensionResolver = createSpy(request, "/bonita/API/extension/myPostResource"); final Long pageId = resourceExtensionResolver.resolvePageId(apiSession); verify(pageMappingService).getPage(any(HttpServletRequest.class), eq(apiSession), eq(API_EXTENSION_POST_MAPPING_KEY), any(Locale.class), eq(false)); assertThat(pageId).isEqualTo(PAGE_ID); }
@Test public void should_resolve_pageId_with_parameters() throws Exception { final Request request = new Request(Method.GET, "/bonita/API/extension/helloWorld?param1=a&param2=b"); doReturn(pageReference).when(pageMappingService).getPage(httpServletRequest, apiSession, API_EXTENSION_GET_MAPPING_KEY, Locale.FRENCH, false); doReturn(PAGE_ID).when(pageReference).getPageId(); final ResourceExtensionResolver resourceExtensionResolver = createSpy(request, "/bonita/API/extension/helloWorld"); final Long pageId = resourceExtensionResolver.resolvePageId(apiSession); verify(pageMappingService).getPage(any(HttpServletRequest.class), eq(apiSession), eq(API_EXTENSION_GET_MAPPING_KEY), any(Locale.class), eq(false)); assertThat(pageId).isEqualTo(PAGE_ID); }
@Test(expected = NotFoundException.class) public void should_unresolved_pageId_throw_exception() throws Exception { final Request request = new Request(Method.POST, "/bonita/API/extension/myPostResource"); final NotFoundException notFoundException = new NotFoundException("page not found"); doThrow(notFoundException).when(pageMappingService).getPage(httpServletRequest, apiSession, API_EXTENSION_POST_MAPPING_KEY, Locale.FRENCH, false); doReturn(PAGE_ID).when(pageReference).getPageId(); final ResourceExtensionResolver resourceExtensionResolver = createSpy(request, "/bonita/API/extension/myPostResource"); final Long pageId = resourceExtensionResolver.resolvePageId(apiSession); verify(pageMappingService).getPage(any(HttpServletRequest.class), eq(apiSession), eq(API_EXTENSION_POST_MAPPING_KEY), any(Locale.class), eq(false)); assertThat(pageId).isEqualTo(PAGE_ID); }