Coverage Report - org.jbehave.core.io.rest.IndexWithBreadcrumbs
 
Classes in this File Line Coverage Branch Coverage Complexity
IndexWithBreadcrumbs
87%
47/54
88%
16/18
1.933
IndexWithBreadcrumbs$ToLowerCase
100%
2/2
N/A
1.933
 
 1  
 package org.jbehave.core.io.rest;
 2  
 
 3  
 import static java.text.MessageFormat.format;
 4  
 import static org.apache.commons.lang.StringUtils.isNotBlank;
 5  
 import static org.jbehave.core.io.CodeLocations.codeLocationFromPath;
 6  
 import static org.jbehave.core.io.rest.filesystem.FilesystemUtils.fileNameWithoutExt;
 7  
 import static org.jbehave.core.io.rest.filesystem.FilesystemUtils.normalisedPathOf;
 8  
 
 9  
 import java.io.File;
 10  
 import java.io.IOException;
 11  
 import java.util.ArrayList;
 12  
 import java.util.HashMap;
 13  
 import java.util.List;
 14  
 import java.util.Map;
 15  
 
 16  
 import org.apache.commons.io.FileUtils;
 17  
 import org.jbehave.core.io.StoryFinder;
 18  
 
 19  
 public abstract class IndexWithBreadcrumbs implements ResourceIndexer {
 20  
 
 21  
         private static final String EMPTY = "";
 22  
         private static final String FULL_PATH = "{0}/{1}";
 23  
         
 24  
         private RESTClient client;
 25  
         private ResourceNameResolver nameResolver;
 26  
 
 27  
         public IndexWithBreadcrumbs(RESTClient client,
 28  4
                         ResourceNameResolver nameResolver) {
 29  4
                 this.client = client;
 30  4
                 this.nameResolver = nameResolver;
 31  4
         }
 32  
 
 33  
         public Map<String, Resource> indexResources(String rootURI) {
 34  0
                 String entity = get(uri(rootURI));
 35  0
                 Map<String, Resource> index = createIndexFromEntity(rootURI, entity);
 36  0
                 addBreadcrumbs(index);
 37  0
                 return index;
 38  
         }
 39  
 
 40  
         public Map<String, Resource> indexResources(String rootURI,
 41  
                         String rootPath, String syntax, String includes) {
 42  2
                 Map<String, Resource> index = createIndexFromPaths(rootURI, rootPath,
 43  
                                 syntax, includes);
 44  2
                 addBreadcrumbs(index);
 45  2
                 return index;
 46  
         }
 47  
 
 48  
         protected Map<String, Resource> createIndexFromPaths(String rootURI,
 49  
                         String rootPath, String syntax, String includes) {
 50  2
                 Map<String, Resource> index = new HashMap<String, Resource>();
 51  2
                 List<String> paths = new StoryFinder().findPaths(
 52  
                                 codeLocationFromPath(rootPath), includes, EMPTY);
 53  2
                 for (String path : paths) {
 54  6
                         addPath(rootURI, rootPath, fullPath(rootPath, path), syntax, index);
 55  6
                 }
 56  2
                 return index;
 57  
         }
 58  
 
 59  
         private void addPath(String rootURI, String rootPath, String path,
 60  
                         String syntax, Map<String, Resource> index) {
 61  14
                 File file = new File(path);
 62  14
                 String name = resolveName(fileNameWithoutExt(file));
 63  14
                 String parentName = parentName(file, rootPath);
 64  14
                 String uri = fullPath(rootURI, name);
 65  14
                 Resource resource = new Resource(uri, name, parentName);
 66  14
                 resource.setContent(contentOf(file));
 67  14
                 if ( isNotBlank(syntax) ){
 68  7
                         resource.setSyntax(syntax);
 69  
                 }
 70  14
                 index.put(name, resource);
 71  14
                 if (parentName != null) {
 72  8
                         addPath(rootURI, rootPath, file.getParent(), syntax, index);
 73  
                 }
 74  14
         }
 75  
 
 76  
         private String parentName(File file, String rootPath) {
 77  14
                 File parent = file.getParentFile();
 78  14
                 if (parent != null && !normalisedPathOf(parent).equals(rootPath)) {
 79  8
                         return resolveName(parent.getName());
 80  
                 }
 81  6
                 return null;
 82  
         }
 83  
 
 84  
         private String fullPath(String root, String path) {
 85  20
                 return format(FULL_PATH, root,  path);
 86  
         }
 87  
 
 88  
         private String contentOf(File file) {
 89  14
                 if (file.isDirectory()) {
 90  8
                         return EMPTY;
 91  
                 }
 92  
                 try {
 93  6
                         return FileUtils.readFileToString(file);
 94  0
                 } catch (IOException e) {
 95  0
                         throw new RuntimeException(
 96  
                                         "Failed to read content of file " + file, e);
 97  
                 }
 98  
         }
 99  
 
 100  
         protected void addBreadcrumbs(Map<String, Resource> index) {
 101  2
                 for (Resource resource : index.values()) {
 102  10
                         List<String> breadcrumbs = new ArrayList<String>();
 103  10
                         collectBreadcrumbs(breadcrumbs, resource, index);
 104  10
                         resource.setBreadcrumbs(breadcrumbs);
 105  10
                 }
 106  2
         }
 107  
 
 108  
         private void collectBreadcrumbs(List<String> breadcrumbs,
 109  
                         Resource resource, Map<String, Resource> index) {
 110  20
                 if (resource.hasParent()) {
 111  10
                         String parentName = resource.getParentName();
 112  10
                         breadcrumbs.add(0, parentName);
 113  10
                         Resource parent = index.get(parentName);
 114  10
                         if (parent != null) {
 115  10
                                 collectBreadcrumbs(breadcrumbs, parent, index);
 116  
                         }
 117  
                 }
 118  20
         }
 119  
 
 120  
         private String get(String uri) {
 121  0
                 return client.get(uri);
 122  
         }
 123  
         
 124  
         protected abstract Map<String, Resource> createIndexFromEntity(
 125  
                         String rootURI, String entity);
 126  
 
 127  
         protected abstract String uri(String rootPath);
 128  
 
 129  
         protected String resolveName(String input){
 130  36
                 return nameResolver.resolve(input);
 131  
         }
 132  
         
 133  4
         public static class ToLowerCase implements ResourceNameResolver {
 134  
 
 135  
                 public String resolve(String input) {
 136  36
                         return input.toLowerCase();
 137  
                 }
 138  
 
 139  
         }
 140  
 }