gwtmockito with requestbuilder - java

This is a part of my class, I want to test:
public class PrefPanel extends Composite {
private static PrefPanelUiBinder uiBinder = GWT.create(PrefPanelUiBinder.class);
interface PrefPanelUiBinder extends UiBinder<Widget, PrefPanel> {}
public PrefPanel(GlobalParams globalParams) {
initWidget(uiBinder.createAndBindUi(this));
String url = URL.encode(globalParams.getBaseUrl() + "book.html");
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
try {
Request response = builder.sendRequest(jsonString, new RequestCallback() {
#Override
public void onError(Request request, Throwable exception) {
displayError("Error");
}
#Override
public void onResponseReceived(Request request, Response response) {
updateBookList(response.getText());
}
});
} catch (RequestException e) {
displayError("Error");
}
Here is a part of my test class:
#RunWith(GwtMockitoTestRunner.class)
public class PositionServiceTest {
#Mock RequestBuilder builder;
#Mock GlobalParams globalParams;
#Mock URL url;
private PrefPanel prefPanel;
#Before
public void setup() {
GwtMockito.useProviderForType(RequestBuilder.class, new FakeProvider() {
#Override
public Object getFake(Class aclass) {
return builder;
}
});
when(globalParams.getBaseUrl()).thenReturn("http://localhost/");
prefPanel = new PrefPanel(globalParams);
...
When I start to debug I get an error message:
- url cannot be empty
- java.lang.IllegalArgumentException
- at com.google.gwt.http.client.StringValidator.throwlfEmptyOrNull(StringValidator.java:52)
- ...
The error occurs on the line where I create the RequestBuilder (new RequestBuilder). I have no idea how to create a new instance of RequestBuilder. Could you give me a clue?
I have heard that gwtmockit can't handle constructors. Is there a way to avoid the new RequestBuilder? Do I have to use powermockito?

Related

Why PowerMockito.whenNew not woking in my code?

here is my code, AgentRest is not mocked in A class
class A {
public void t() throws IOException {
AgentRest agentRest = new AgentRest("127.0.0.1", 8888);
HttpResponse<TaskStatusResponse> a = agentRest.dataBackup(null); // not mock
}
}
#Slf4j
#PrepareForTest({A.class, SftpClientTest.class,AgentRest.class })
#RunWith(PowerMockRunner.class)
class SftpClientTest {
#Test
void getHome() throws Exception {
HttpResponse<TaskStatusResponse> httpResponse =
HttpResponse.<TaskStatusResponse>builder().code(0).body(TaskStatusResponse.builder().status("").build()).build();
AgentRest agentRest = PowerMockito.mock(AgentRest.class);
PowerMockito.whenNew(AgentRest.class).withAnyArguments().thenReturn(agentRest);
PowerMockito.when(agentRest.dataBackup(ArgumentMatchers.any())).thenReturn(httpResponse);
new A().t();
log.info("");
}
}
i have try a lot but still failed, PowerMockito.whenNew seams not working, and i have added all class to PrepareForTest
I have found the probelm is junit5 is not working with powermock, solution link: https://rieckpil.de/mock-java-constructors-and-their-object-creation-with-mockito/
here is my new code:
class A {
public void t() throws IOException {
AgentRest agentRest = new AgentRest("127.0.0.1", 8888);
HttpResponse<TaskStatusResponse> a = agentRest.dataBackup(null);
}
}
#Slf4j
class SftpClientTest {
#Test
void getHome() throws Exception {
try (MockedConstruction<AgentRest> mocked = mockConstruction(AgentRest.class)) {
HttpResponse<TaskStatusResponse> httpResponse =
HttpResponse.<TaskStatusResponse>builder().code(0).body(TaskStatusResponse.builder().status("").build()).build();
// every object creation is returning a mock from now on
AgentRest agentRest = new AgentRest("sa", 22);
when(agentRest.dataBackup(ArgumentMatchers.any())).thenReturn(httpResponse);
new A().t();
}
}
}

Spring Boot Raw WebSocket Set Principal

I want to develop notification sender in real time with Spring Boot but I can't use Stomp and sockjs, so I need implement raw WebSocket but I can't find out how to set Principal in WebSocket connection beacuse I want to Authenticate with JWT token. So, where or how can I set principal.
I'am using these;
WebSocketConfig.java :
#EnableWebSocket
#Configuration
public class WebSocketConfig implements WebSocketConfigurer {
#Autowired
WebSocketNotificationSenderService senderService;
#Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry
webSocketHandlerRegistry) {
webSocketHandlerRegistry.addHandler(createHandler(),
"/handler").addInterceptors(new HttpSessionHandshakeInterceptor()
{
#Override
public void afterHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
#Nullable Exception ex) {
super.afterHandshake(request, response, wsHandler, ex);
}
#Override
public boolean beforeHandshake(ServerHttpRequest request,
ServerHttpResponse response, WebSocketHandler wsHandler,
Map<String, Object> attributes) throws Exception {
return super.beforeHandshake(request, response,
wsHandler, attributes)
}
});
}
#Bean
public WebSocketHandler createHandler() {
return new MyHandler(senderService);
}
}
MyHandler.java :
#Component
public class MyHandler extends TextWebSocketHandler {
WebSocketNotificationSenderService senderService;
public MyHandler(WebSocketNotificationSenderService senderService){
this.senderService = senderService;
}
#Override
public void afterConnectionEstablished(WebSocketSession session) throws Exception {
senderService.addToSession(session);
}
}
WebSocketNotificationSenderService.java :
#Service
public class WebSocketNotificationSenderService implements
IWebSocketSenderService<WrapperWsNotification> {
private List<WebSocketSession> sessions = new CopyOnWriteArrayList<>();
private ObjectMapper mapper = new ObjectMapper();
public void addToSession(WebSocketSession session) {
sessions.add(session);
}
#Override
public void convertAndSend(WrapperWsNotification payload) throws JsonProcessingException {
String payloadString = mapper.writeValueAsString(payload);
sessions.stream().forEach(session -> {
try {
session.sendMessage(new TextMessage(payloadString));
} catch (IOException e) {
e.printStackTrace();
}
});
}
#Override
public void convertAndSendToUser(String user, WrapperWsNotification payload) throws
JsonProcessingException {
String payloadString = mapper.writeValueAsString(payload);
sessions.forEach(session -> {
if (session.getPrincipal().getName().equals(user)) {
try {
session.sendMessage(new TextMessage(payloadString));
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
My Notification Sender to websocket;
#Component
public class NotificationConsumer {
#Autowired
WebSocketNotificationSenderService webSocket;
private Logger logger = LoggerFactory.getLogger(NotificationConsumer.class);
public void onReceiveNotification(String object) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
WrapperWsNotification wrapperWsNotification= objectMapper.readValue(object, WrapperWsNotification.class);
logger.info("User where coming from redis " + wrapperWsNotification.getUser().getUsername());
webSocket.convertAndSendToUser(wrapperWsNotification.getUser().getUsername(), wrapperWsNotification);
}
}
I find out solution and added an example

Unit testing respository with in MVVM pattern android

I am trying to write unit tests for repository while using MVVM pattern in android.
What i have is a repository which fetched data from the network using retrofit
public class ValidateCbuRepository {
private static ValidateCbuRepository single_instance = null;
private MutableLiveData<CBUValidationImageResponse> data = new MutableLiveData<>();
public static ValidateCbuRepository getInstance() {
if (single_instance == null)
single_instance = new ValidateCbuRepository();
return single_instance;
}
public MutableLiveData<CBUValidationImageResponse> processImage(String encodedString) {
JsonObject postParam = new JsonObject();
postParam.addProperty("image", encodedString);
Api service = RetrofitClientInstance.getRetrofitInstance().create(Api.class);
data.setValue(null);
HttpUrl httpUrl = HttpUrl.parse("some url");
Call<CBUValidationImageResponse> responseCall = service.getProcessedImage_cbu_validation(httpUrl.toString(),postParam);
responseCall.enqueue(new Callback<CBUValidationImageResponse>() {
#Override
public void onResponse(Call<CBUValidationImageResponse> call, Response<CBUValidationImageResponse> response) {
if(response.isSuccessful()) {
CBUValidationImageResponse res = response.body();
CBUValidationImageResponse cbuValidationImageResponse = res;
Log.i("CBU response ",""+cbuValidationImageResponse.toString());
cbuValidationImageResponse.setSuccess(true);
cbuValidationImageResponse.setShowProgres(false);
cbuValidationImageResponse.setError(false);
data.setValue(cbuValidationImageResponse);
}
}
#Override
public void onFailure(Call<CBUValidationImageResponse> call, Throwable t) {
CBUValidationImageResponse cbuValidationImageResponse = new CBUValidationImageResponse();
cbuValidationImageResponse.setError(true);
cbuValidationImageResponse.setShowProgres(false);
data.setValue(cbuValidationImageResponse);
t.printStackTrace();
}
});
return data;
}
}
The unit test part
#Mock
private Observer<CBUValidationImageResponse> observer;
#Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
#Test
public void testApiResponse_success() {
Api mockedApiInterface = Mockito.mock(Api.class);
Call<CBUValidationImageResponse> mockedCall = Mockito.mock(Call.class);
Mockito.when(mockedApiInterface.getProcessedImage_cbu_validation(any(),any())).thenReturn(mockedCall);
try {
Mockito.doAnswer(new Answer() {
#Override
public Void answer(InvocationOnMock invocation) throws Throwable {
Callback<CBUValidationImageResponse> callback = invocation.getArgument(0);
CBUValidationImageResponse cbuValidationImageResponse = new CBUValidationImageResponse();
cbuValidationImageResponse.setCBU_code("some code");
cbuValidationImageResponse.setHeight(7);
cbuValidationImageResponse.setBreadth(7);
cbuValidationImageResponse.setLength(7);
callback.onResponse(mockedCall, Response.success(cbuValidationImageResponse));
// or callback.onResponse(mockedCall, Response.error(404. ...);
// or callback.onFailure(mockedCall, new IOException());
return null;
}
}).when(mockedCall).enqueue(any(Callback.class));
ValidateCbuRepository validateCbuRepository = new ValidateCbuRepository();
String encodedString= "";
validateCbuRepository.processImage(encodedString).observeForever(observer);
Getting a null pointer exception at validateCbuRepository.processImage(encodedString).observeForever(observer). Next step is to verify the observer.
I expect the test to pass. What am i doing wrong here?. I did something similar foe view model and the test passes with 100% code coverage.
The retrofit call is asynchronous. Is that the reason why it fails?
Edit : It seems livedata is null while testing causing NPE.

To where was HttpClient's LocalTestServer moved?

I've read about how HttpClient's LocalTestServer can be used for automated testing, however I can't seem to find where it's been moved. I tried defining dependency to httpclient with tests classifier:
'org.apache.httpcomponents:httpclient:4.5.2:tests'
but there doesn't seem to be a LocalTestServer class defined in there. Has this been discontinued?
Your test should now extend org.apache.http.localserver.LocalServerTestBase.
This is available in the httpclient module with classifier tests.
Your pom could look like:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
<scope>test</scope>
<classifier>tests</classifier>
</dependency>
Related issue:
https://issues.apache.org/jira/browse/HTTPCLIENT-1172
Related changeset:
https://github.com/apache/httpclient/commit/2ebd8202849c1f4a17d4320543e315a46cbfdc10
Can use simple implementation of the LocalServer
public class LocalHttpServer extends ExternalResource {
private static final Logger log = LoggerFactory.getLogger(LocalHttpServer.class);
private final int port;
private MockServer server;
public LocalHttpServer(int port) {
this.port = port;
}
#Override
protected void before() throws Throwable {
server = new MockServer();
server.setUp();
}
public void start() throws Exception {
server.start(port);
log.info("LocalHttpServer started on {}", port);
}
/**
* Need to be setup before starting server
*/
public LocalHttpServer registerSimpleHandler(String path, String resp) {
server.registerSimpleHandler(path, resp);
return this;
}
#Override
protected void after() {
try {
server.shutDown();
} catch (Exception e) {
e.printStackTrace();
}
log.info("LocalHttpServer shutdown on {}", port);
}
static class MockServer extends LocalServerTestBase {
#Override
public void setUp() throws Exception {
super.setUp();
HttpRequestFactory requestFactory = new DefaultHttpRequestFactory() {
#Override
public HttpRequest newHttpRequest(final RequestLine requestline) throws MethodNotSupportedException {
return super.newHttpRequest(requestline);
}
};
HttpMessageParserFactory<HttpRequest> requestParserFactory = new DefaultHttpRequestParserFactory(
BasicLineParser.INSTANCE, requestFactory);
DefaultBHttpServerConnectionFactory connectionFactory = new DefaultBHttpServerConnectionFactory(
ConnectionConfig.DEFAULT, requestParserFactory, DefaultHttpResponseWriterFactory.INSTANCE);
this.serverBootstrap.setConnectionFactory(connectionFactory);
}
public void registerSimpleHandler(String path, String resp) {
this.serverBootstrap.registerHandler(path, (request, response, context) ->
response.setEntity(new StringEntity(resp, ContentType.TEXT_PLAIN)));
}
public void start(int port) throws Exception {
this.serverBootstrap.setListenerPort(port);
start();
}
}
Example usage
public class HttpTest {
private static final LocalHttpServer LOCAL_HTTP = new LocalHttpServer(8080);
#ClassRule
public static final RuleChain RULE_CHAIN = RuleChain.outerRule(LOCAL_HTTP);
#Before
public void setUp() throws Exception {
LOCAL_HTTP.registerSimpleHandler("/path", "response")
.start();
}
#Test
public void someTest() {
//request here
}
}

Async servlet in embedded jetty seems to write to wrong response at high load

The code given below works fine when I send manual requests. If I generate requests with siege (siege -c 500 -r 100 'http://localhost:8080/?name=Chandru&age=560'), I start seeing these messages in jetty's log:
2013-01-02 00:06:55.761:WARN:oejh.HttpGenerator:Ignoring extra content {name: "
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content Chandru
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content ", age:
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content 560
2013-01-02 00:07:56.393:WARN:oejh.HttpGenerator:Ignoring extra content }
I'm using Jetty 8.1.8.v20121106 on Java 1.7.0_10.What am I doing wrong?
public class Main {
public static void main(String[] args) throws Exception {
final Server server = new Server(8080);
final ServletContextHandler context = new ServletContextHandler(server, "/");
final ServletHolder servlet = new ServletHolder(new RequestHandler(new Responder()));
servlet.setAsyncSupported(true);
context.addServlet(servlet, "/");
server.start();
}
}
class RequestHandler extends HttpServlet {
private final Responder responder;
public RosterHandler(Responder responder) {
this.responder = responder;
}
#Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
responder.process(request.startAsync());
}
}
class Responder {
private final ExecutorService pool;
public Responder() {
this.pool = Executors.newFixedThreadPool(100);
}
public void process(final AsyncContext ctx) {
pool.execute(new Runnable() {
#Override
public void run() {
final String name = ctx.getRequest().getParameter("name");
final int age = Integer.parseInt(ctx.getRequest().getParameter("age"));
ctx.getResponse().setContentType("application/json");
try (final PrintWriter w = ctx.getResponse().getWriter()) {
w.printf("{name: \"%s\", age: %d}", name, age);
} catch (IOException e) {
e.printStackTrace(System.err);
}
}
});
}
}
You should call context.complete() to signal that your work is done and the async connection can be considered finished.

Categories

Resources