How to get originalName from a File in quarkus - file-upload

I have an api in quarkus java when I upload the file I can't get the original name of it. When I use File.getName the return I get is this pfx16311440985252189274sfx
`
package br.com.upload.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import br.com.upload.entity.MultipartBody;
#Path("upload")
#Produces(MediaType.APPLICATION_JSON)
#Consumes(MediaType.APPLICATION_JSON)
public class UploadController {
#POST
#Consumes(MediaType.MULTIPART_FORM_DATA)
public String sendUploada(#MultipartForm MultipartBody data) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(data.file));
// String mimeType = URLConnection.guessContentTypeFromStream(is);
// Long size = data.file.length();
System.out.println("data file " + data.file.getName());
Files.copy(is,
new File("/home/allanfenx/Público/Java/upload/src/main/resources/upload",
"mai.png")
.toPath(),
StandardCopyOption.REPLACE_EXISTING);
return data.fileName;
}
}`
`package br.com.upload.entity;
import java.io.File;
import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
public class MultipartBody {
#FormParam("file")
#PartType(MediaType.APPLICATION_OCTET_STREAM)
public File file;
#FormParam("fileName")
#PartType(MediaType.APPLICATION_JSON)
public String fileName;
}`
This is my code quarkus uses InputStream but I'm using File. The code works perfectly but I can't get the original file name. The return I have is pfx16311440985252189274sfx it looks like they are characters to be decoded

I've been through this a few days ago.
Unfortunately using Resteasy annotation, there is no way to get the filename directly. You have to parse the filename from request header 'Content-disposition', something like you can find here : #MultipartForm How to get the original file name?
However something's not mentionned: you can use 'quarkus-resteasy-reactive' implementation as explained here : https://quarkus.io/guides/resteasy-reactive
You'll find a nice FileUpload class that can give you what you want.
WARNING though: changing JAX-RS implementation to reactive might impact your entire app so I advise you to read quarkus documentation if you do that :)

Related

karate.log file is not getting generated

I have placed the logback-test.xml in classpath
karate.log is not getting generated, I have seen there is a file called main.log that gets generated sometime
Here is the project structure
Here is the runner file
package com.org.KarateLearning;
import com.intuit.karate.KarateOptions;
import com.intuit.karate.Results;
import com.intuit.karate.Runner;
import com.intuit.karate.junit4.Karate;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Map;
import java.util.Properties;
import static org.junit.Assert.assertTrue;
public class ParallelTestRunner {
#Test
public void parallel() {
Results res = Runner.path("classpath:features").tags("~#ignore").parallel(5);
assertTrue(res.getErrorMessages(),res.getFailCount()==0);
}}
I have tried placing logback-test.xml in resources and com.org.KarateLearning but karate.log is not getting genearted
Deleted the existing file ( logback-test.xml ) and created a new one from below path under src/test/java
https://github.com/intuit/karate/blob/master/karate-demo/src/test/java/logback-test.xml
Now file is getting generated in target folder
In between it was getting generated outside of target folder. Now its getting generated at correct location inside target folder.
Also note even after deleting the logback-test.xml log was getting generated. (not sure about the reason for it)
Now all works well

Click button doesn't work - Katalon Studio

I am new to Katalon studio and am trying to run my test case but it's always filed as it says:
Error:
Test Cases/Logging in FAILED because (of) (Stack trace: com.kms.katalon.core.exception.StepFailedException: Unable to >click on object 'Object Repository/Page_BaseLine - test/input_Przypomnienie o fakturze' (Root cause: com.kms.katalon.core.webui.exception.WebElementNotFoundException: Web element with id: 'Object Repository/Page_BaseLine - test/input_Przypomnienie o fakturze' located by 'By.xpath: >//input[#id='SubmitButton']' not found)
I tried to do it manually but everytime the same problem appears, what shall I do then?
Here is my source code:
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
WebUI.openBrowser('')
def plik = new File('Config.txt')
def lines = plik.readLines()
WebUI.navigateToUrl(lines[0])
WebUI.setText(findTestObject('Object Repository/Page_Demo version BaseLine/input_Login form_LoginTextBox'), lines[1])
WebUI.setText(findTestObject('Object Repository/Page_Demo version BaseLine/input_Login form_PasswordTextB'), lines[2])
WebUI.click(findTestObject('Object Repository/Page_Demo version BaseLine/input_remember me_LoginButton'))
WebUI.click(findTestObject('Page_BaseLine - test/a_Kontrahenci'))
WebUI.click(findTestObject('Page_BaseLine - test/a_Dodaj Kontrahenta'))
WebUI.setText(findTestObject('own/Page_BaseLine - test/input_Nazwa skrcona_ShortNameT'), 'greg12')
WebUI.setText(findTestObject('own/Page_BaseLine - test/textarea_'), 'Grzegorz Spytek12')
WebUI.click(findTestObject('Page_BaseLine - test/input_Przypomnienie o fakturze'))
WebUI.click(findTestObject('Page_BaseLine - test/input_Opis_CloseButton'))
Thanks for help.
The xpath seems not correct. Could you try removing the '>'?
>//input[#id='SubmitButton']'

Cucumber.class error: cannot convert from class cucumber to calls runner

I'm getting an error in Cucumber.class saying
cannot convert from class cucumber to calls runner
Here's my code:
package testRunners;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
//import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features="resources/features", glue="StepDefinition")
public class Login {
}
It happens when you have dependency mismatch in your project. More details can be seen after digging in pom.xml. Please share it.
For now you should be knowing that #Runwith takes something which extends ParentRunner Class, so make sure the Cucumber.class that you are using here is extending ParentRunner.

How to run Jmeter webdriver sampler file uploading script in blazemeter?

I have a Jmeter webdriver sampler script it is running fine in Jmeter. Now I want it to run on blazemeter. Since I was using local directory in Jmeter it was fine. But I am not able to upload file using any path. Can some body help on this. I need to upload file in blazemeter using jmeter webdriver sampler. What path Can I give and where should I keep my file?
Also I tried my file keeping in shared folders of Blazemeter. but I failed in that. Please some body give some syntax for it.
import org.apache.jmeter.services.FileServer;
import org.openqa.selenium;
import java.awt.Robot;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import org.openqa.selenium.firefox.FirefoxProfile;
import java.time.temporal.ChronoUnit;
import java.time.format.DateTimeFormatter;
import java.time.LocalDateTime;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import org.testng.asserts.SoftAssert;
import java.util.List;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
WebDriverWait wait= new WebDriverWait(WDS.browser,20);
WDS.sampleResult.sampleStart();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[#class='menu-main']/child::li"))).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//p[#class='btn btn-outline']"))).click();
WDS.log.info("Dialog box");
Robot robot= new Robot();
robot.setAutoDelay(2000);
filepath="home/jmeter/Deepak/ColumnFunctionFile.csv";
StringSelection stringselection = new StringSelection(filepath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringselection, null);
robot.setAutoDelay(1000);
WDS.log.info("Copy");
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
WDS.log.info("release");
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
WDS.log.info("enter press release");
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
robot.setAutoDelay(2000);
//upload file status
WebDriverWait wait2= new WebDriverWait(WDS.browser,5);
oldTimeForProgress=LocalDateTime.now();
try{
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='progress progress-mini']")));
WDS.log.info("--------------------------------Progress bar gets invisible now-------------------");
}
catch(Exception e)
{
WDS.log.info("---------------Looking like file got stuck in processing messages----------------");
}
As far as I can see from the Selenium Webdriver Based Testing article you should just upload your .jmx script and BlazeMeter will pick it up. Just make sure not to use Internet Explorer as their systems seem to be running on Linux so only Firefox and Chrome are supported.
Also my expectation is that you should reach out to BlazeMeter Support in case of any issues, they should be more aware of their infrastructure than anyone here.

Eclipse Juno EE NoClassDefFoundError when using external Jar

I added an external jar in my eclipse dynamic webproject via Folder -> properties -> build path -> Libraries -> add external jar.
The code is working fine on compile time.
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tika.Tika;
#WebServlet(name="UploadServlet", urlPatterns={"/uploadFile"}) // specify urlPattern for servlet
#MultipartConfig //Specify that this servlet will receive a multipart data
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
Tika tika = new Tika();
res.setContentType("text/html");
PrintWriter writer = res.getWriter();
writer.write(mediaType);
}
}
I added Apache Tika but when I run my application. these exception occured.
root cause
java.lang.NoClassDefFoundError: org/apache/tika/Tika
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.ClassNotFoundException: org.apache.tika.Tika
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
servlet.UploadServlet.doPost(UploadServlet.java:19)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
I am using Apache Tika to determine what file has been upload, I want to use it for validating if a file is an image, or audio file
You forgot to add that jar to the Deployment Assembly page. It's not deployed to the server when you run your application, hence the NoClassDefFoundErrors.