XHtmlToConfluenceServiceImpl implements XHtmlToConfluenceService { @Override public void postXHtmlToConfluence(final SwaggerConfluenceConfig swaggerConfluenceConfig, final String xhtml) { LOG.info("Posting XHTML to Confluence..."); SWAGGER_CONFLUENCE_CONFIG.set(swaggerConfluenceConfig); SWAGGER_DOCUMENT.set(parseXhtml(xhtml)); final Map<String, ConfluenceLink> titleLinkMap = buildTableOfContentsLinkMap(); final List<ConfluencePage> confluencePages = handlePagination(); Integer categoryAncestorId = null; Integer individualAncestorId = null; for (final ConfluencePage confluencePage : confluencePages) { final PageType pageType = confluencePage.getPageType(); LOG.debug("PROCESSING PAGE: {} --> {}", confluencePage.getPageType(), confluencePage.getXhtml()); switch (pageType) { case ROOT: confluencePage.setAncestorId(swaggerConfluenceConfig.getAncestorId()); break; case CATEGORY: confluencePage.setAncestorId(categoryAncestorId); break; case INDIVIDUAL: confluencePage.setAncestorId(individualAncestorId); break; default: throw new SwaggerConfluenceConfigurationException("Unknown Page Type Encountered!"); } LOG.debug("ANCESTOR ID SET: <{}> -> {}", confluencePage.getConfluenceTitle(), confluencePage.getAncestorId()); addExistingPageData(confluencePage); if (confluencePage.exists()) { updatePage(confluencePage, titleLinkMap); if(pageType == PageType.CATEGORY){ cleanPages(confluencePage); } } else { createPage(confluencePage, titleLinkMap); } if (pageType == PageType.ROOT) { categoryAncestorId = confluencePage.getAncestorId(); } else if (pageType == PageType.CATEGORY) { individualAncestorId = confluencePage.getAncestorId(); } } } XHtmlToConfluenceServiceImpl(final RestTemplate restTemplate); @Override void postXHtmlToConfluence(final SwaggerConfluenceConfig swaggerConfluenceConfig, final String xhtml);  }
@Test public void testCreatePageWithPaginationModeSingleWithOrphanFailSafe(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setAncestorId(null); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity, responseEntity); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_NOT_FOUND, GET_RESPONSE_FOUND, POST_RESPONSE); final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate, times(2)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.POST), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getValue(); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-create-json-body-example.json") ); assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }
@Test public void testUpdatePageWithPaginationModeSingleAndNoTableOfContents(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setIncludeTableOfContentsOnSinglePage(false); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); final ResponseEntity<String> postResponseEntity = new ResponseEntity<>(POST_RESPONSE, HttpStatus.OK); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_FOUND); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.PUT), any(RequestEntity.class), eq(String.class))).thenReturn(postResponseEntity); final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate).exchange(any(URI.class), eq(HttpMethod.PUT), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getValue(); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-update-json-body-example.json") ); assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }
@Test public void testCreatePageWithPaginationModeCategory(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setPaginationMode(PaginationMode.CATEGORY_PAGES); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); for(int i = 0; i < 5; i++) { when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_NOT_FOUND); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(POST_RESPONSE); } final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate, times(5)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate, times(5)).exchange(any(URI.class), eq(HttpMethod.POST), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getAllValues().get(3); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-create-json-body-definitions-example.json") ); assertNotNull("Failed to Capture RequeestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }
@Test public void testUpdatePageWithPaginationModeCategory(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setPaginationMode(PaginationMode.CATEGORY_PAGES); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); final ResponseEntity<String> postResponseEntity = new ResponseEntity<>(POST_RESPONSE, HttpStatus.OK); for(int i = 0; i < 5; i++) { when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_FOUND); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.PUT), any(RequestEntity.class), eq(String.class))).thenReturn(postResponseEntity); } final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate, times(5)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate, times(5)).exchange(any(URI.class), eq(HttpMethod.PUT), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getAllValues().get(3); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-update-json-body-definitions-example.json") ); assertNotNull("Failed to Capture RequeestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }
@Test public void testCreatePageWithPaginationModeIndividual(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setPaginationMode(PaginationMode.INDIVIDUAL_PAGES); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); for(int i = 0; i < 31; i++) { when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_NOT_FOUND); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.POST), any(HttpEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getBody()).thenReturn(POST_RESPONSE); } final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate, times(34)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate, times(34)).exchange(any(URI.class), eq(HttpMethod.POST), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getAllValues().get(30); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-create-json-body-user-example.json") ); assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }
@Test public void testUpdatePageWithPaginationModeIndividual(){ final SwaggerConfluenceConfig swaggerConfluenceConfig = getTestSwaggerConfluenceConfig(); swaggerConfluenceConfig.setPaginationMode(PaginationMode.INDIVIDUAL_PAGES); final String xhtml = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-petstore-xhtml-example.html") ); final ResponseEntity<String> postResponseEntity = new ResponseEntity<>(POST_RESPONSE, HttpStatus.OK); final List<String> returnJson = new ArrayList<>(); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); for(int i = 0; i < 34; i++) { if(i > 0) { returnJson.add(GET_RESPONSE_FOUND); } if(CATEGORY_INDEXES.contains(i)){ returnJson.add(GET_CHILDREN); } } final String[] returnJsonArray = new String[returnJson.size()]; returnJson.toArray(returnJsonArray); when(responseEntity.getBody()).thenReturn(GET_RESPONSE_FOUND, returnJsonArray); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.DELETE), any(RequestEntity.class), eq(String.class))).thenReturn(responseEntity); when(responseEntity.getStatusCode()).thenReturn(HttpStatus.NO_CONTENT); when(restTemplate.exchange(any(URI.class), eq(HttpMethod.PUT), any(RequestEntity.class), eq(String.class))).thenReturn(postResponseEntity); final ArgumentCaptor<HttpEntity> httpEntityCaptor = ArgumentCaptor.forClass(HttpEntity.class); xHtmlToConfluenceService.postXHtmlToConfluence(swaggerConfluenceConfig, xhtml); verify(restTemplate, times(38)).exchange(any(URI.class), eq(HttpMethod.GET), any(RequestEntity.class), eq(String.class)); verify(restTemplate, times(34)).exchange(any(URI.class), eq(HttpMethod.PUT), httpEntityCaptor.capture(), eq(String.class)); final HttpEntity<String> capturedHttpEntity = httpEntityCaptor.getAllValues().get(30); final String expectedPostBody = IOUtils.readFull( AsciiDocToXHtmlServiceImplTest.class.getResourceAsStream( "/swagger-confluence-update-json-body-user-example.json") ); assertNotNull("Failed to Capture RequestEntity for POST", capturedHttpEntity); assertEquals("Unexpected JSON Post Body", expectedPostBody, capturedHttpEntity.getBody()); }