|
| 1 | +/* |
| 2 | + * Copyright 2020 https://github.com/openapi-processor/openapi-processor-test |
| 3 | + * PDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.openapiprocessor.test |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.DeserializationFeature |
| 9 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 10 | +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory |
| 11 | +import com.github.difflib.DiffUtils |
| 12 | +import com.github.difflib.UnifiedDiffUtils |
| 13 | + |
| 14 | +import java.nio.file.Files |
| 15 | +import java.nio.file.Path |
| 16 | +import java.util.stream.Stream |
| 17 | + |
| 18 | +class FileSupport { |
| 19 | + |
| 20 | + private Class resourceBase |
| 21 | + |
| 22 | + FileSupport(Class resourceBase) { |
| 23 | + this.resourceBase = resourceBase |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * copy paths: file system => file system |
| 28 | + * |
| 29 | + * @param source source folder |
| 30 | + * @param target target folder |
| 31 | + */ |
| 32 | + static void copy (Path source, Path target) { |
| 33 | + Stream<Path> paths = Files.walk (source) |
| 34 | + .filter ({f -> !Files.isDirectory (f)}) |
| 35 | + |
| 36 | + paths.forEach { p -> |
| 37 | + Path relativePath = source.relativize (p) |
| 38 | + Path targetPath = target.resolve (relativePath.toString ()) |
| 39 | + Files.createDirectories (targetPath.getParent ()) |
| 40 | + |
| 41 | + InputStream src = Files.newInputStream (p) |
| 42 | + OutputStream dst = Files.newOutputStream (targetPath) |
| 43 | + src.transferTo (dst) |
| 44 | + } |
| 45 | + |
| 46 | + paths.close () |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * copy paths: resources <=> file system |
| 51 | + * |
| 52 | + * @param source source prefix |
| 53 | + * @param sources source files |
| 54 | + * @param target target folder |
| 55 | + */ |
| 56 | + void copy (String source, List<String> sources, Path target) { |
| 57 | + for (String p : sources) { |
| 58 | + String relativePath = p.substring (source.size () + 1) |
| 59 | + |
| 60 | + Path targetPath = target.resolve (relativePath.toString ()) |
| 61 | + Files.createDirectories (targetPath.getParent ()) |
| 62 | + |
| 63 | + InputStream src = getResource (p) |
| 64 | + OutputStream dst = Files.newOutputStream (targetPath) |
| 65 | + src.transferTo (dst) |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * collect paths in file system. |
| 71 | + * |
| 72 | + * will convert all paths to use "/" as path separator |
| 73 | + */ |
| 74 | + static List<String> collectPaths(Path source) { |
| 75 | + List<String> files = [] |
| 76 | + |
| 77 | + def found = Files.walk (source) |
| 78 | + .filter ({ f -> |
| 79 | + !Files.isDirectory (f) |
| 80 | + }) |
| 81 | + |
| 82 | + found.forEach ({f -> |
| 83 | + files << source.relativize (f).toString () |
| 84 | + }) |
| 85 | + found.close () |
| 86 | + |
| 87 | + files.collect { |
| 88 | + it.replace ('\\', '/') |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * collect input paths |
| 94 | + */ |
| 95 | + List<String> collectAbsoluteInputPaths (String path) { |
| 96 | + collectAbsoluteResourcePaths (path, "inputs.yaml") |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * collect output paths |
| 101 | + */ |
| 102 | + List<String> collectAbsoluteOutputPaths (String path) { |
| 103 | + collectAbsoluteResourcePaths (path, "generated.yaml") |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * collect output paths, relative to packageName |
| 108 | + */ |
| 109 | + List<String> collectRelativeOutputPaths (String path, String packageName) { |
| 110 | + collectRelativeResourcePaths (path, "generated.yaml").collect { |
| 111 | + it.substring (packageName.size () + 1) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * collect absolute paths from output.yaml in resources |
| 117 | + */ |
| 118 | + List<String> collectAbsoluteResourcePaths (String path, String itemsYaml) { |
| 119 | + collectRelativeResourcePaths (path, itemsYaml).collect { |
| 120 | + "${path}/${it}".toString () |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * collect paths from output.yaml in resources |
| 126 | + */ |
| 127 | + List<String> collectRelativeResourcePaths (String path, String itemsYaml) { |
| 128 | + def source = getResource ("${path}/${itemsYaml}") |
| 129 | + if (!source) { |
| 130 | + println "ERROR: missing '${path}/${itemsYaml}' configuration file!" |
| 131 | + } |
| 132 | + |
| 133 | + def mapper = createYamlParser () |
| 134 | + def sourceItems = mapper.readValue (source.text, TestItems) |
| 135 | + sourceItems.items |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * unified diff resources <=> file system |
| 140 | + * |
| 141 | + * @return true if there is a difference |
| 142 | + */ |
| 143 | + boolean printUnifiedDiff (String expected, Path generated) { |
| 144 | + def expectedLines = getResource (expected).readLines () |
| 145 | + |
| 146 | + def patch = DiffUtils.diff ( |
| 147 | + expectedLines, |
| 148 | + generated.readLines ()) |
| 149 | + |
| 150 | + def diff = UnifiedDiffUtils.generateUnifiedDiff ( |
| 151 | + getResource (expected).text, |
| 152 | + generated.toString (), |
| 153 | + expectedLines, |
| 154 | + patch, |
| 155 | + 4 |
| 156 | + ) |
| 157 | + |
| 158 | + diff.each { |
| 159 | + println it |
| 160 | + } |
| 161 | + |
| 162 | + return !patch.deltas.isEmpty () |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * unified diff file system <=> file system |
| 167 | + */ |
| 168 | + static void printUnifiedDiff (Path expected, Path generated) { |
| 169 | + def patch = DiffUtils.diff ( |
| 170 | + expected.readLines (), |
| 171 | + generated.readLines ()) |
| 172 | + |
| 173 | + def diff = UnifiedDiffUtils.generateUnifiedDiff ( |
| 174 | + expected.toString (), |
| 175 | + generated.toString (), |
| 176 | + expected.readLines (), |
| 177 | + patch, |
| 178 | + 2 |
| 179 | + ) |
| 180 | + |
| 181 | + diff.each { |
| 182 | + println it |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + InputStream getResource (String path) { |
| 187 | + resourceBase.getResourceAsStream (path) |
| 188 | + } |
| 189 | + |
| 190 | + private static ObjectMapper createYamlParser () { |
| 191 | + new ObjectMapper (new YAMLFactory ()) |
| 192 | + .configure (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) |
| 193 | + } |
| 194 | + |
| 195 | +} |
0 commit comments