qaf-ws-support API - Read request xml and json data from jar

74 views
Skip to first unread message

karthik Kumar

unread,
Jul 29, 2020, 4:27:04 PM7/29/20
to qaf users
We have a base jar App1 where we have all our API requests in xml and json data files under resources. We want to use these in calling apps App2, App3 etc without duplicating API calls as we are planning to keep everything in App1 jar

While trying to test with BDD step in App2, the resources in jar (app1) weren't reading. I think its only reading resources in App2 and was able to test bu dupicating request xml in App2. Is there a way to be able to read and invoke API calls from calling app to jar API. Please suggest any advice. thank you.

WsStep.userRequests("requests.api1", data);

The WsRequestBean.fillData(Object ojb) always invoke else where it calls fillFromJsonString instead of fillFromConfig()

cjayswal

unread,
Jul 31, 2020, 3:51:37 PM7/31/20
to qaf users
This is valid usecase and looks good to have feature. As of today qaf doesn't consider resources from class path. You need to load them, for example, adding a listener that loads required resources from class path.

karthik Kumar

unread,
Aug 3, 2020, 12:45:24 PM8/3/20
to qaf users
Hi cjayswal,

Thanks for the reply. I tried to read the jar directory like below and read all the xml file names within it. Then tried to load using PropertyUtil, this gave me exception. Is there any better approach or resolving below

URL url = TestingClass.class.getResource(JAR_DIRECTORY);
FileSystem fileSystem = FileSystems.newFileSystem(url.toURI(), Collections.<String, Object>emptyMap());

 Path myPath = fileSystem.getPath(JAR_DIRECTORY);

PropertyUtil propertyUtil = ConfigurationManager.getBundle();

       try (Stream<Path> walk = Files.walk(myPath, 1)) {


             List<String> xmlFileNames = walk.map(x -> x.toString())

                           .filter(f -> f.endsWith(".xml")).collect(Collectors.toList());


             xmlFileNames.forEach(System.out::println);

           
       
xmlFileNames.forEach(file ->  {
                URL
urlFile = TestingClass.class.getResource(file.subString(1));    
               
propertyUtil.load(urlFile);

     
}

               

       } catch (IOException e) {

              e.printStackTrace();

       }



IllegralArgumentException: Passed in key must select exactly one node: requests.request1   on HierarchialConfiguration.java:588

Amit Bhoraniya

unread,
Aug 17, 2020, 12:50:38 AM8/17/20
to qaf users
Below code works perfectly for me to load resources from classpath.

public void loadResources(Class clazz, String folderName) {
       
List<String> files;
       
try {
            files
= IOUtils.readLines(clazz.getClassLoader().getResourceAsStream(folderName + "/"),
                   
Charset.forName("UTF-8"));
           
if (files != null && !files.isEmpty()) {
               
for (String file : files) {
                   
InputStream inputStream = SFListener.class.getClassLoader()
                           
.getResourceAsStream(folderName + "/" + file);
                   
AbstractConfiguration.setDefaultListDelimiter(';');
                   
PropertiesConfiguration configuration = new PropertiesConfiguration();
                    configuration
.load(inputStream);
                   
ConfigurationManager.getBundle().copy(configuration);
               
}
           
} else {
               
Enumeration<URL> en = ElementUtils.class.getClassLoader().getResources(folderName);
               
if (en.hasMoreElements()) {
                    URL url
= en.nextElement();
                   
JarURLConnection urlcon = (JarURLConnection) (url.openConnection());
                   
try (JarFile jar = urlcon.getJarFile();) {
                       
Enumeration<JarEntry> entries = jar.entries();
                       
while (entries.hasMoreElements()) {
                           
JarEntry entry = entries.nextElement();
                           
if (entry.getName().startsWith(folderName) && !entry.isDirectory()) {
                               
InputStream inputStream = jar.getInputStream(entry);
                               
AbstractConfiguration.setDefaultListDelimiter(';');
                               
PropertiesConfiguration configuration = new PropertiesConfiguration();
                                configuration
.load(inputStream);
                               
ConfigurationManager.getBundle().copy(configuration);
                           
}
                       
}
                   
}
               
}
           
}
       
} catch (IOException | ConfigurationException e) {
            e
.printStackTrace();
       
}
}

Thanks,
Amit Bhoraniya

karthik Kumar

unread,
Aug 17, 2020, 10:01:27 PM8/17/20
to qaf users
Hi Amit,
Are these classes SFListener, ElementUtils  , from any dependency or used defined?

Amit Bhoraniya

unread,
Aug 17, 2020, 10:20:41 PM8/17/20
to qaf-...@googlegroups.com
I missed to change some code. Refer updated one.

On Tue, 18 Aug, 2020, 7:31 am karthik Kumar, <kumarka...@gmail.com> wrote:
Hi Amit,
Are these classes SFListener, ElementUtils  , from any dependency or used defined?

On Sunday, August 16, 2020 at 11:50:38 PM UTC-5, Amit Bhoraniya wrote:
Below code works perfectly for me to load resources from classpath.

public void loadResources(Class clazz, String folderName) {
       
List<String> files;
       
try {
            files
= IOUtils.readLines(clazz.getClassLoader().getResourceAsStream(folderName + "/"),
                   
Charset.forName("UTF-8"));
           
if (files != null && !files.isEmpty()) {
               
for (String file : files) {

                   
InputStream inputStream = clazz.getClassLoader()

                           
.getResourceAsStream(folderName + "/" + file);
                   
AbstractConfiguration.setDefaultListDelimiter(';');
                   
PropertiesConfiguration configuration = new PropertiesConfiguration();
                    configuration
.load(inputStream);
                   
ConfigurationManager.getBundle().copy(configuration);
               
}
           
} else {

               
Enumeration<URL> en = clazz.getClassLoader().getResources(folderName);

               
if (en.hasMoreElements()) {
                    URL url
= en.nextElement();
                   
JarURLConnection urlcon = (JarURLConnection) (url.openConnection());
                   
try (JarFile jar = urlcon.getJarFile();) {
                       
Enumeration<JarEntry> entries = jar.entries();
                       
while (entries.hasMoreElements()) {
                           
JarEntry entry = entries.nextElement();
                           
if (entry.getName().startsWith(folderName) && !entry.isDirectory()) {
                               
InputStream inputStream = jar.getInputStream(entry);
                               
AbstractConfiguration.setDefaultListDelimiter(';');
                               
PropertiesConfiguration configuration = new PropertiesConfiguration();
                                configuration
.load(inputStream);
                               
ConfigurationManager.getBundle().copy(configuration);
                           
}
                       
}
                   
}
               
}
           
}
       
} catch (IOException | ConfigurationException e) {
            e
.printStackTrace();
       
}
}

Thanks,
Amit Bhoraniya

--
You received this message because you are subscribed to the Google Groups "qaf users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to qaf-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/qaf-users/8ff71f8f-8eba-4eee-889a-593fbc62c0dao%40googlegroups.com.

karthik Kumar

unread,
Aug 17, 2020, 11:53:41 PM8/17/20
to qaf users
Thanks Ankit. I tried calling this  code in the listener onStart() and also tried in onTest(), I see it takes the files and reads from jar and loads configuration. 

My classpath resources has requestcallrepository_requests.xml, properties  in one folder and request jsons in another. These are in classpath jar. However, when I tried to invoke the API from calling app, it fails as it cannot read the requests from bundle configurations. In WsRequestBean.fillData(Object ojb) still invokes else part fillFromJsonString(jsonstr) instead of fillFromConfig((String) obj). Not sure if I am missing anything!

On Monday, August 17, 2020 at 9:20:41 PM UTC-5, Amit Bhoraniya wrote:
I missed to change some code. Refer updated one.

To unsubscribe from this group and stop receiving emails from it, send an email to qaf-...@googlegroups.com.

cjayswal

unread,
Sep 22, 2021, 4:48:17 PM9/22/21
to qaf users
Refer #350 available with 3.0.1b release. Here is the example of implementation which is registered as service.

સોમવાર, 17 ઑગસ્ટ, 2020ના રોજ 08:53:41 PM UTC-7 વાગ્યે kumarka...@gmail.com દ્વારા આમ લખવામાં આવ્યું હતું:
Reply all
Reply to author
Forward
0 new messages