Coverage Report - org.jbehave.core.io.rest.confluence.IndexFromConfluence
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexFromConfluence
75%
25/33
44%
8/18
2.5
 
 1  
 package org.jbehave.core.io.rest.confluence;
 2  
 
 3  
 import java.util.HashMap;
 4  
 import java.util.Map;
 5  
 import java.util.regex.Pattern;
 6  
 
 7  
 import org.jbehave.core.io.rest.RESTClient;
 8  
 import org.jbehave.core.io.rest.RESTClient.Type;
 9  
 import org.jbehave.core.io.rest.Resource;
 10  
 import org.jbehave.core.io.rest.ResourceIndexer;
 11  
 import org.jbehave.core.io.rest.confluence.Confluence.Page;
 12  
 
 13  
 public class IndexFromConfluence implements ResourceIndexer {
 14  
 
 15  
     private static final String DISPLAY = "/display/";
 16  
 
 17  
     private final Confluence confluence;
 18  
 
 19  
     public IndexFromConfluence() {
 20  0
         this(null, null);
 21  0
     }
 22  
 
 23  
     public IndexFromConfluence(String username, String password) {
 24  0
         this(new RESTClient(Type.XML, username, password));
 25  0
     }
 26  
 
 27  1
     public IndexFromConfluence(RESTClient client) {
 28  1
         this.confluence = new Confluence(client);
 29  1
     }
 30  
 
 31  
     public Map<String, Resource> indexResources(String rootURI) {
 32  1
         return indexResources(rootURI, null);
 33  
     }
 34  
 
 35  
     public Map<String, Resource> indexResources(String rootURI, String rootPath, String syntax, String includes) {
 36  0
         return indexResources(rootURI, includes);
 37  
     }
 38  
 
 39  
     protected Map<String, Resource> indexResources(String rootURI, String includePattern) {
 40  1
         if (rootURI == null || !rootURI.contains(DISPLAY)) {
 41  0
             throw new RuntimeException("Root URI is not in correct format: " + rootURI);
 42  
         }
 43  1
         String[] split = rootURI.split(DISPLAY);
 44  1
         String baseUrl = split[0];
 45  1
         if (split.length == 1) {
 46  0
             throw new RuntimeException("URI does not contain space and page: " + rootURI);
 47  
         }
 48  1
         String[] searchTerms = split[1].split("/");
 49  1
         if (split.length != 2) {
 50  0
             throw new RuntimeException("URI does not contain space and page: " + rootURI);
 51  
         }
 52  1
         return createResourceMap(baseUrl, searchTerms[0], searchTerms[1], includePattern);
 53  
     }
 54  
 
 55  
     private Map<String, Resource> createResourceMap(String baseUrl, String spaceKey, String pageName, String pattern) {
 56  1
         Map<String, Resource> result = new HashMap<String, Resource>();
 57  1
         Page rootPage = confluence.loadRootPage(baseUrl, spaceKey, pageName);
 58  1
         addPage(result, rootPage.getSelfReference(), pattern);
 59  1
         return result;
 60  
     }
 61  
 
 62  
     private void addPage(Map<String, Resource> result, String href, String pattern) {
 63  2
         Page page = confluence.loadPage(href, true);
 64  2
         Resource resource = new Resource(page.getSelfReference(), page.getTitle());
 65  2
         resource.setContent(page.getBody());
 66  2
         if (pattern == null ||
 67  
                 (pattern != null && Pattern.matches(pattern, page.getTitle()))) {
 68  2
             result.put(page.getTitle(), resource);
 69  
         }
 70  2
         if (page.hasChildren()) {
 71  2
             for (Page child : page.getChildren()) {
 72  1
                 addPage(result, child.getSelfReference(), pattern);
 73  1
             }
 74  
         }
 75  2
     }
 76  
 
 77  
 }