REST API script taking longer time to execute - rest

I am new to API automation. When trying to execute the basic script in selenium I am getting the below error. Can some one please help me with it.
package GetRequest;
import static io.restassured.RestAssured.given;
import io.restassured.RestAssured;
public class trying {
public static void main(String[] args) {
// base url
RestAssured.baseURI="https://maps.googleapis.com";
given().
param("location","-33.8670522,151.1957362").
param("radius","1500").
param("Key","AIzaSyBBuJ-3wBy1VKGUMtNqO8PpAHWGESIItAo").
when().
get("/maps/api/place/nearbysearch/json").
then().
assertThat().statusCode(200);
}
}

What is the port you are connecting at ? Have you specified that somewhere ? Are you using proxy ?
Why not try something like?
#BeforeClass
public void setProxy()
{
System.setProperty("http.proxyHost", YOUR_PROXY_HOST_HERE);
System.setProperty("http.proxyPort", YOUR_PROXY_PORT_HERE);
}

Related

Async method in Spring Boot

I have a problem with sending email with method annotated as #Async.
Firstly, I am not sure if it is possible to work as I want so I need help with explanation.
Here is what am doing now:
In main method i have annotation
#EnableAsync(proxyTargetClass = true)
Next I have AsyncConfig class
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurerSupport;
import java.util.concurrent.Executor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
#Configuration
public class AsyncConfig extends AsyncConfigurerSupport {
#Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(2);
executor.setMaxPoolSize(2);
executor.setQueueCapacity(500);
executor.setThreadNamePrefix("email-");
executor.initialize();
return executor;
}
}
Of course, its rest application so i have controller, service etc, looks normally, nothing special
My async method looks like this:
#Async
public void sendEmail() throws InterruptedException {
log.info("Sleep");
Thread.sleep(10000L);
//method code
log.info("Done");
}
I executing this method in another service method:
#Override
public boolean sendSystemEmail() {
try {
this.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("pending sendEmail method");
return true;
}
Now what I want archive is to ignore executing sendEmail() function and execute return true; meanwhile function sendEmail() will be executing in another Thread. Of course it doesn't work now as I want. Unfortunately.
Note that I am new into async programming, so I have lack of knowledge in some parts of this programming method.
Thanks for any help.
First – let’s go over the rules – #Async has two limitations:
it must be applied to public methods only
self-invocation – calling the async method from within the same class – won’t work
The reasons are simple – the method needs to be public so that it can be proxied. And self-invocation doesn’t work because it bypasses the proxy and calls the underlying method directly.
http://www.baeldung.com/spring-async

Portico RTI + Java WEB Application = NoClassDefFoundError

I'm trying to start a Federate (HLA RTI) from a Java Web application, but I'm receiving the error java.lang.NoClassDefFoundError: hla/rti1516/FederateAmbassador.
The same Federate is starting well from an ordinary java application.
My goal is to start the RTI and a Federation by starting a Federate when the web application is started. So I create a WebListener class to start my Federate:
#WebListener
public class Startup implements ServletContextListener {
#Override
public void contextDestroyed(ServletContextEvent arg0) {
//
}
#Override
public void contextInitialized(ServletContextEvent arg0) {
Federate fed = new Federate();
fed.start();
}
}
This is the Federate start() code. I'll not put all code because this is not even reached:
public class Federate {
public void start() {
System.out.println("start");
try {
RTIambassador rtiAmb = RtiFactoryFactory.getRtiFactory().getRtiAmbassador();
MyFederateAmbassador fedAmb = new MyFederateAmbassador();
...
}
When my webserver is starting, I never see the System.out.println("start") output, just only this error :
Grave: Exception sending context initialized event to listener instance of class cmabreu.scorpio.startup.Startup
java.lang.NoClassDefFoundError: hla/rti1516/FederateAmbassador
at cmabreu.scorpio.startup.Startup.contextInitialized(Startup.java:29)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
My portico.jar is configured in Build Path and the Federate imports are pretty fine ( no errors ):
import hla.rti1516.AttributeHandle;
import hla.rti1516.AttributeHandleSet;
import hla.rti1516.AttributeHandleValueMap;
import hla.rti1516.LogicalTime;
import hla.rti1516.ObjectClassHandle;
import hla.rti1516.ObjectInstanceHandle;
import hla.rti1516.RTIambassador;
import hla.rti1516.ResignAction;
import hla.rti1516.jlc.RtiFactoryFactory;
What I'm doing wrong?
After contact Portico creator, Tim Pokorny, I solved the problem changing some code in Portico source. The Portico's Log4j is in conflict with catalina's Log4J and a few other things more.

GWT : Getting a 404 error when unit testing RequestBuilder

I use the GWT RequestBuilder, and for testing purposes, I'd like to load a json file in the server.
It works perfectly with the DevMode, but throw a 404 error with GWTTestCase.
With RPC, there is a fix adding <servlet path=".." class="..."/>, but what can I do with static content ?
I could easily use #TextResource, but it's not the goal of my UnitTest (which is in fact a functionnal test)
Static resources can be bundled with a module by putting them in the module's public path.
I used (once again) Thomas's answer to resolve the problem. My module is io.robusta.fora.comments.Comment.gwt.xml and I've put my user.json file in the io.robsuta.fora.comments.resources package.
I had so to add in Comment.gwt.xml file : <public path="resources"/>
Then the GWTTestCase is straightforward :
public class GwtRestClientTest extends GWTTestCase{
#Override
public String getModuleName() {
return "io.robusta.fora.comments.Comments";
}
public void testGET(){
String base = GWT.getModuleBaseURL();
System.out.println(base); //-> http://192.168.0.10:53551/io.robusta.fora.comments.Comments.JUnit/
GwtRestClient client = new GwtRestClient(base); //base url
AsyncCallback<String> cb = new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
System.out.println(result);//->{id:1,email:"jo#robusta.io"}
finishTest();
}
#Override
public void onFailure(Throwable caught) {
caught.printStackTrace();
}
};
client.GET("user.json", null, cb);//fetch my json file with no params
delayTestFinish(3000);
}
}

Reusable Liferay (6.0.6) service

I am trying to implement resuable Custom Services without using ext and servicebuilder.
I referred this article: http://www.devatwork.nl/2010/04/implementing-a-reusable-liferay-service-without-ext-or-service-builder/ , but I am confused in how should I implement this using eclipse? Following are the steps that I followed to do this:
- Created liferay-plugin project within eclipse.
- Created package containing CustomServices (interface) and CustomServicesUtil.
- Created jar file of package in step 2.
- Placed that jar file in tomcat\lib\ext\
- Then created package (with in same liferay-plugin project), that includes CutomServicesImpl and CustomServicesBaseImpl
- Defined portlet-spring.xml, service.properties, and modified web.xml (as per the article), and finally deployed the project.
On deployment, project is deployed successfully, but when I am trying to use customMethods defined in CustomServicesImpl through CustomServicesUtil.getCustomMethod(), I am getting the following error:
"java.lang.ClassNotFoundException: com.demo.custom.services.CustomServicesUtil"
I configure build path to include customservices.jar file but its not working out, still showing the same error. I don’t know whether this is the correct way to implement resuable services or not. I tried this so that i can make use of custom method in one of my project.
Here is the code for custom services:
CustomServices.java
package com.demo.custom.services;
import com.liferay.portal.model.User;
public interface CustomServices {
String getCustomName(User user);
}
CustomServicesUtil.java
package com.demo.custom.services;
import com.liferay.portal.model.User;
public class CustomServicesUtil {
private static CustomServices services;
public static CustomServices getServices() {
if (services == null) {
throw new RuntimeException("Custom Services not set");
}
return services;
}
public void setServices(CustomServices pServices) {
services = pServices;
}
public static String getCustomName(User user){
return getServices().getCustomName(user);
}
}
CustomServicesBaseImpl.java
package com.demo.custom.services.impl;
import com.demo.custom.services.CustomServices;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.service.base.PrincipalBean;
import com.liferay.portal.util.PortalUtil;
public abstract class CustomServicesBaseImpl extends PrincipalBean implements CustomServices {
protected CustomServices services;
public CustomServices getServices() {
return services;
}
public void setServices(CustomServices pServices) {
this.services = pServices;
}
protected void runSQL(String sql) throws SystemException {
try {
PortalUtil.runSQL(sql);
} catch (Exception e) {
throw new SystemException(e);
}
}
}
CustomServicesImpl.java
package com.demo.custom.services.impl;
import com.liferay.portal.model.User;
public class CustomServicesImpl extends CustomServicesBaseImpl {
#Override
public String getCustomName(User user) {
// TODO Auto-generated method stub
if(user == null){
return null;
}else{
return new StringBuffer().append(user.getFirstName()).append(" ").append(user.getLastName()).toString();
}
}
}
Here is the code of controller class of my another portlet, where i am making use of this service.
HelloCustomName.java
package com.test;
import java.io.IOException;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.demo.custom.services.CustomServicesUtil;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.User;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.util.bridges.mvc.MVCPortlet;
public class HelloCustomName extends MVCPortlet {
#Override
public void doView(RenderRequest renderRequest,
RenderResponse renderResponse) throws IOException, PortletException {
System.out.println("--doview----");
ThemeDisplay themeDisplay = (ThemeDisplay)renderRequest.getAttribute(WebKeys.THEME_DISPLAY);
User user = themeDisplay.getUser();
String customName = CustomServicesUtil.getCustomName(user); //getting error here
System.out.println("customName:" + customName);
}
}
Please point me on how to implement resuable services? Any guidance will be really useful.
Thanks.
My mind, you don't need the complexity of services. Simply make utility classes and put this in to tomcat/lib/ext. Be sure that tomcat/lib/ext is correct configured in tomcat/conf/catalina.properties, something like this:
common.loader=${catalina.home}/lib/ext/*.jar

GWT RequestBuilder fails with SSL connections (in tests). Why? Are there any workarounds?

I try to connect to a (local) web service using the GWT RequestBuilder with a secure connection (SSL), but the connection isn't established... When I connect using a plain HTTP connection everything works fine.
Some details
everything works fine when I'm using my browser to view the pages,
I use an auto signed SSL certificate on my local machine,
the tests fail because the actual response code (responseCode) is not set,
the tests work fine if I'm using a plain HTTP connection (no SSL).
Code
package com.example.services;
import com.google.gwt.http.client.*;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.Timer;
public class RequestBuilderTest extends GWTTestCase {
private static String SERVER_URL = "https://127.0.0.1/api";
private static final int ASSERT_DELAY_IN_MS = 15000;
private static final int TEST_DURATION_IN_MS = 20000;
private int statusCode;
public void testGet() throws Exception {
new RequestBuilder(RequestBuilder.GET, SERVER_URL).sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable e) {
fail(e.getMessage());
}
public void onResponseReceived(Request request, Response response) {
statusCode = response.getStatusCode();
}
});
delayTestFinish(TEST_DURATION_IN_MS);
new Timer() {
#Override
public void run() {
assertEquals(Response.SC_OK, statusCode);
finishTest();
}
}.schedule(ASSERT_DELAY_IN_MS);
}
#Override
public String getModuleName() {
return "com.example.services.RequestBuilder";
}
}
Results
test passes with SERVER_URL = "http://127.0.0.1/api";
test fails with SERVER_URL = "https://127.0.0.1/api";
This is the stack trace for junit:
junit.framework.AssertionFailedError: Remote test failed at 127.0.0.1
expected=200 actual=0
Any ideas on what could be wrong and how can I make the tests work with SSL?
EDIT
How can I force the tests to run in secure mode? I use eclipse... I tried setting some "Program arguments" in the "Run configurations" (for the junit tests), but they don't work... Here are the arguments:
-noserver -startupUrl https://192.168.8.147/com.example.services.RequestBuilderTest.JUnit/ -bindAddress 0.0.0.0
Is it better if I just deactivate the SSL on the server? These tests are meant to be launched on the continuous integration server and I wanted to test them using SSL.
It sounds like you're running into a same-origin policy problem. Embedding a URL into the app like that is inherently unreliable. Instead, use GWT.getModuleBaseUrl().