Best Karate code snippet using com.intuit.karate.resource.ResourceUtils
Source:ResourceUtilsTest.java
...39/**40 *41 * @author pthomas342 */43class ResourceUtilsTest {44 static final Logger logger = LoggerFactory.getLogger(ResourceUtilsTest.class);45 46 static File wd = FileUtils.WORKING_DIR;47 @Test48 void testFindFilesByExtension() {49 Collection<Resource> list = ResourceUtils.findResourcesByExtension(wd, "txt", "src/test/java/com/intuit/karate/resource");50 assertEquals(1, list.size());51 Resource resource = list.iterator().next();52 assertTrue(resource.isFile());53 assertFalse(resource.isClassPath());54 assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getRelativePath());55 assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getPrefixedPath());56 assertEquals("foo", FileUtils.toString(resource.getStream()));57 }58 @Test59 void testGetFileByPath() {60 Resource resource = ResourceUtils.getResource(wd, "src/test/java/com/intuit/karate/resource/test1.txt");61 assertTrue(resource.isFile());62 assertFalse(resource.isClassPath());63 assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getRelativePath());64 assertEquals("src/test/java/com/intuit/karate/resource/test1.txt", resource.getPrefixedPath());65 assertEquals("foo", FileUtils.toString(resource.getStream()));66 }67 @Test68 void testResolveFile() {69 Resource temp = ResourceUtils.getResource(wd, "src/test/java/com/intuit/karate/resource/test1.txt");70 Resource resource = temp.resolve("test2.log");71 assertTrue(resource.isFile());72 assertFalse(resource.isClassPath());73 assertEquals("src/test/java/com/intuit/karate/resource/test2.log", resource.getRelativePath());74 assertEquals("src/test/java/com/intuit/karate/resource/test2.log", resource.getPrefixedPath());75 assertEquals("bar", FileUtils.toString(resource.getStream()));76 }77 @Test78 void testResolveRelativeFile() {79 Resource temp = ResourceUtils.getResource(wd, "src/test/java/com/intuit/karate/resource/dir1/dir1.log");80 Resource resource = temp.resolve("../dir2/dir2.log");81 assertTrue(resource.isFile());82 assertFalse(resource.isClassPath());83 assertEquals("src/test/java/com/intuit/karate/resource/dir1/../dir2/dir2.log", resource.getRelativePath());84 assertEquals("src/test/java/com/intuit/karate/resource/dir1/../dir2/dir2.log", resource.getPrefixedPath());85 assertEquals("src.test.java.com.intuit.karate.resource.dir1.dir2.dir2.log", resource.getPackageQualifiedName());86 assertEquals("bar", FileUtils.toString(resource.getStream()));87 }88 @Test89 void testFindJarFilesByExtension() {90 Collection<Resource> list = ResourceUtils.findResourcesByExtension(wd, "properties", "classpath:cucumber");91 Resource resource = null;92 for (Resource temp : list) {93 if ("cucumber/version.properties".equals(temp.getRelativePath())) {94 resource = temp;95 break;96 }97 }98 assertFalse(resource.isFile());99 assertTrue(resource.isClassPath());100 assertEquals("cucumber/version.properties", resource.getRelativePath());101 assertEquals("classpath:cucumber/version.properties", resource.getPrefixedPath());102 assertEquals("cucumber-jvm.version=1.2.5", FileUtils.toString(resource.getStream()));103 }104 @Test105 void testGetJarFileByPath() {106 Resource resource = ResourceUtils.getResource(wd, "classpath:cucumber/version.properties");107 assertFalse(resource.isFile());108 assertTrue(resource.isClassPath());109 assertEquals("cucumber/version.properties", resource.getRelativePath());110 assertEquals("classpath:cucumber/version.properties", resource.getPrefixedPath());111 assertEquals("cucumber-jvm.version=1.2.5", FileUtils.toString(resource.getStream()));112 }113 @Test114 void testResolveJarFile() {115 Resource temp = ResourceUtils.getResource(wd, "classpath:cucumber/version.properties");116 Resource resource = temp.resolve("api/cli/USAGE.txt");117 assertFalse(resource.isFile());118 assertTrue(resource.isClassPath());119 assertEquals("cucumber/api/cli/USAGE.txt", resource.getRelativePath());120 assertEquals("classpath:cucumber/api/cli/USAGE.txt", resource.getPrefixedPath());121 }122 @Test123 void testFindClassPathFilesByExtension() {124 Collection<Resource> list = ResourceUtils.findResourcesByExtension(wd, "txt", "classpath:com/intuit/karate/resource");125 assertEquals(1, list.size());126 Resource resource = list.iterator().next();127 assertTrue(resource.isFile());128 assertTrue(resource.isClassPath());129 assertEquals("com/intuit/karate/resource/test1.txt", resource.getRelativePath());130 assertEquals("classpath:com/intuit/karate/resource/test1.txt", resource.getPrefixedPath());131 assertEquals("foo", FileUtils.toString(resource.getStream()));132 }133 @Test134 void testGetClassPathFileByPath() {135 Resource resource = ResourceUtils.getResource(wd, "classpath:com/intuit/karate/resource/test1.txt");136 assertTrue(resource.isFile());137 assertTrue(resource.isClassPath());138 assertEquals("com/intuit/karate/resource/test1.txt", resource.getRelativePath());139 assertEquals("classpath:com/intuit/karate/resource/test1.txt", resource.getPrefixedPath());140 assertEquals("foo", FileUtils.toString(resource.getStream()));141 }142 @Test143 void testResolveClassPathFile() {144 Resource temp = ResourceUtils.getResource(wd, "classpath:com/intuit/karate/resource/test1.txt");145 Resource resource = temp.resolve("test2.log");146 assertTrue(resource.isFile());147 assertTrue(resource.isClassPath());148 assertEquals("com/intuit/karate/resource/test2.log", resource.getRelativePath());149 assertEquals("classpath:com/intuit/karate/resource/test2.log", resource.getPrefixedPath());150 assertEquals("bar", FileUtils.toString(resource.getStream()));151 }152 @Test153 void testResolveRelativeClassPathFile() {154 Resource temp = ResourceUtils.getResource(new File(""), "classpath:com/intuit/karate/resource/dir1/dir1.log");155 Resource resource = temp.resolve("../dir2/dir2.log");156 assertTrue(resource.isFile());157 assertTrue(resource.isClassPath());158 assertEquals("com/intuit/karate/resource/dir1/../dir2/dir2.log", resource.getRelativePath());159 assertEquals("classpath:com/intuit/karate/resource/dir1/../dir2/dir2.log", resource.getPrefixedPath());160 assertEquals("bar", FileUtils.toString(resource.getStream()));161 }162 @Test163 void testGetFeatureWithLineNumber() {164 String path = "classpath:com/intuit/karate/resource/test.feature:6";165 List<Feature> features = ResourceUtils.findFeatureFiles(new File(""), Collections.singletonList(path));166 assertEquals(1, features.size());167 assertEquals(6, features.get(0).getCallLine());168 }169 @Test170 void testClassPathToFileThatExists() {171 File file = ResourceUtils.classPathToFile("com/intuit/karate/resource/test1.txt");172 assertTrue(file.exists());173 }174 @Test175 void testClassPathToFileThatDoesNotExist() {176 File file = ResourceUtils.classPathToFile("com/intuit/karate/resource/nope.txt");177 assertNull(file);178 }179 180 @Test181 void testFindJsFilesFromFileSystem() {182 Set<String> files = ResourceUtils.findJsFilesInDirectory(new File("src/test/java/demo").getAbsoluteFile());183 assertEquals(4, files.size());184 Match.that(new ArrayList(files)).contains("['api/demo.js', 'api/cats.js', 'api/payments.js', 'api/render.js']");185 }186 187 @Test188 void testFindJsFilesFromClassPath() {189 Set<String> files = ResourceUtils.findJsFilesInClassPath("demo");190 assertEquals(4, files.size());191 Match.that(new ArrayList(files)).contains("['/api/demo.js', '/api/cats.js', '/api/payments.js', '/api/render.js']");192 } 193}...
ResourceUtils
Using AI Code Generation
1def json = read('classpath:sample.json')2def json = read('file:/home/user/sample.json')3def xml = read('classpath:sample.xml', 'xml')4def xml = read('file:/home/user/sample.xml', 'xml')5def yaml = read('classpath:sample.yml', 'yaml')6def yaml = read('file:/home/user/sample.yml', 'yaml')7def json = read('classpath:sample.json', 'json')8def json = read('file
ResourceUtils
Using AI Code Generation
1def jsonFile = ResourceUtils.getResourceAsFile('classpath:sample.json')2def json = new JsonSlurper().parseText(jsonFile.text)3def xmlFile = ResourceUtils.getResourceAsFile('classpath:sample.xml')4def xml = new XmlSlurper().parseText(xmlFile.text)5def yamlFile = ResourceUtils.getResourceAsFile('classpath:sample.yaml')6def yaml = new Yaml().load(yamlFile.text)7def csvFile = ResourceUtils.getResourceAsFile('classpath:sample.csv')8def csv = new CsvParser().parse(csvFile)9def excelFile = ResourceUtils.getResourceAsFile('classpath:sample.xlsx')10def excel = new ExcelReader().read(excelFile)11def propsFile = ResourceUtils.getResourceAsFile('classpath:sample.properties')12def props = new Properties()13props.load(propsFile.newInputStream())14def textFile = ResourceUtils.getResourceAsFile('classpath:sample.txt')15def binaryFile = ResourceUtils.getResourceAsFile('classpath:sample.bin')16def jsonFileAsString = ResourceUtils.getResourceAsString('classpath:sample.json')17def xmlFileAsString = ResourceUtils.getResourceAsString('classpath:sample.xml')18def yamlFileAsString = ResourceUtils.getResourceAsString('classpath:sample.yaml')
ResourceUtils
Using AI Code Generation
1def content = ResourceUtils.readAsString('classpath:file.txt')2def content = ResourceUtils.readAsBytes('classpath:file.txt')3def content = ResourceUtils.readAsStream('classpath:file.txt')4def content = ResourceUtils.readAsReader('classpath:file.txt')5def content = ResourceUtils.readAsFile('classpath:file.txt')6def content = ResourceUtils.readAsUrl('classpath:file.txt')7def content = ResourceUtils.readAsUri('classpath:file.txt')8def content = ResourceUtils.readAsPath('classpath:file.txt')9def content = ResourceUtils.readAsResource('classpath:file.txt')10def content = ResourceUtils.readAsResource('classpath:file.txt')11def content = ResourceUtils.readAsResource('classpath:file.txt')12def content = ResourceUtils.readAsResource('classpath:file.txt')
ResourceUtils
Using AI Code Generation
1ResourceUtils utils = new ResourceUtils(FeatureContext.feature)2def data = utils.read('data.csv')3ResourceUtils utils = new ResourceUtils(FeatureContext.feature)4def data = utils.read('data.csv')5def list = data.split('\r6def headers = list[0].split(',')7rows.each { row ->8 def values = row.split(',')9 headers.eachWithIndex { header, index ->10 }11}12ResourceUtils utils = new ResourceUtils(FeatureContext.feature)13def data = utils.read('data.csv')14def list = data.split('\r15def headers = list[0].split(',')16rows.each { row ->17 def values = row.split(',')18 headers.eachWithIndex { header, index ->19 }20}21def json = JsonUtils.toJson(map)22ResourceUtils utils = new ResourceUtils(FeatureContext.feature)23def data = utils.read('data.csv')24def list = data.split('\r25def headers = list[0].split(',')26rows.each { row ->27 def values = row.split(',')28 headers.eachWithIndex { header, index ->29 }30}31def json = JsonUtils.toJson(map)32def jsonPath = JsonPath.compile('$.1.name')33def value = jsonPath.read(json)34ResourceUtils utils = new ResourceUtils(FeatureContext.feature)35def data = utils.read('data.csv')36def list = data.split('\r
ResourceUtils
Using AI Code Generation
1def resource = ResourceUtils.read('classpath:sample.json')2def resource = ResourceUtils.read('file:/Users/username/sample.json')3def resource = ResourceUtils.read('file:../sample.json')4def resource = ResourceUtils.read('classpath:sample.json', Thread.currentThread().getContextClassLoader())5def resource = ResourceUtils.read('classpath:sample.json', Thread.currentThread().getContextClassLoader(), 'UTF-8')6def resource = ResourceUtils.read('file:/Users/username/sample.json', 'UTF-8')7def resource = ResourceUtils.read('classpath:sample.json', 'UTF-8')8def resource = ResourceUtils.read('file:/Users/username/sample.json', 'UTF-8')9def resource = ResourceUtils.read('file:../sample.json', 'UTF-8')10def resource = ResourceUtils.read('classpath:sample.json', Thread.currentThread().getContextClassLoader(), 'UTF-8')11def resource = ResourceUtils.read('file:/Users/username/sample.json', Thread.currentThread().getContextClassLoader(), 'UTF-8')
ResourceUtils
Using AI Code Generation
1String content = ResourceUtils.readAsString("classpath:myjsonfile.json")2String content = ResourceUtils.readAsString("file:myfile.json")3String content = ResourceUtils.readAsString("myjsonfile.json")4String content = ResourceUtils.readAsString("myjsonfile")5String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")6String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")7String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")8String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")9String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")10String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")11String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")12String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")13String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")14String content = ResourceUtils.readAsString("classpath:myjsonfile.json", "UTF-8")
ResourceUtils
Using AI Code Generation
1def ResourceUtils utils = new ResourceUtils()2def json = utils.read('classpath:com/intuit/karate/demo/demo.json')3def ResourceUtils utils = new ResourceUtils()4def json = utils.read('file:src/test/java/com/intuit/karate/demo/demo.json')5def ResourceUtils utils = new ResourceUtils()6def ResourceUtils utils = new ResourceUtils()7def ResourceUtils utils = new ResourceUtils()8def json = utils.read('file:src/test/java/com/intuit/karate/demo/demo.json')9def ResourceUtils utils = new ResourceUtils()10def ResourceUtils utils = new ResourceUtils()11def ResourceUtils utils = new ResourceUtils()12def json = utils.read('file:src/test/java/com/intuit/karate/demo/demo.json')
ResourceUtils
Using AI Code Generation
1def content = ResourceUtils.readAsString('classpath:myFile.txt')2assert content.contains('Hello World')3def content = ResourceUtils.readAsString('classpath:myFile.txt')4assert content.contains('Hello World')5def content = ResourceUtils.readAsString('classpath:myFile.txt')6assert content.contains('Hello World')7def content = ResourceUtils.readAsString('classpath:myFile.txt')8assert content.contains('Hello World')9def content = ResourceUtils.readAsString('classpath:myFile.txt')10assert content.contains('Hello World')11def content = ResourceUtils.readAsString('classpath:myFile.txt')12assert content.contains('Hello World')13def content = ResourceUtils.readAsString('classpath:myFile.txt')14assert content.contains('Hello World')15def content = ResourceUtils.readAsString('classpath:myFile.txt')16assert content.contains('Hello World')17def content = ResourceUtils.readAsString('classpath
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!