Automated tests for Spring Boot WebSocket server

20 05 2017

Developing WebSocket server for your Spring Boot app is fairly simple and well described and documented. However when it comes to making sure that it ‘actually works’ is done manually in most cases.

Below I will show how I do the automated integration tests for Websocket server using Spring’s StompClient. I assume that you are familiar with the idea of WebSockets in Spring. If not, here is a very good article: https://docs.spring.io/spring/docs/current/spring-framework-reference/html/websocket.html

Source Code

Code of this tutorial is for you to see here: https://github.com/yacekmm/looksok/tree/WebSocketDemo/Spring/WebSocket

System under test: configuration

The demo will be presented on the simpliest WS configuration which consists of one entry point endpoint (`/ws`) and in-memory message broker (under `/queue`):

@Configuration
@EnableWebSocketMessageBroker
public class WsConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) { 
        registry.addEndpoint("/ws");
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/queue");
    }
}

The idea behind the integration test

In the test I’m going to:
– use SpringRunner to start up the whole application with the full context
– Autowire Component that in production will be responsible for sending messages to WebSocket clients
– Build and configure Spring’s StompClient and connect a StompSession to my WebSocket server
send a message over WebSocket and verify if my test client received it

Starting the application for tests

With SpringRunner.class used within jUnit test I start the app context and autowire the WSProxy component (the one that sends messages to WS clients):

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
public class WsConfigIntegrationTest {

    @Value("${local.server.port}")
    private int port;
    @Autowired
    private WsProxy wsProxy;

WsProxy in this demo is a simple component sending message with a SimpMessagingTemplate:

@Component
public class WsProxy {

    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    public WsProxy(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendMessage(@RequestParam String clientId,
                            @RequestParam String payload){
        messagingTemplate.convertAndSend("/queue/" + clientId, payload);
    }
}

In this configuration, the url of WS endpoint is:

String wsUrl = "ws://127.0.0.1:" + port + "/ws";

Configuring StompClient and connecting StompSession

Using the StompClient with a minimum configuration:

WebSocketStompClient stompClient = new WebSocketStompClient(new StandardWebSocketClient());
stompClient.setMessageConverter(new StringMessageConverter());

I create StompSession to my WS url:

StompSession stompSession = stompClient.connect(wsUrl, new MyStompSessionHandler()).get();

The connect() method returns a future, but here, in tests, I wait synchronously until this session is ready by calling get() on it to get the session instantly.

Oh, and don’t worry about the MyStompSessionHandler – in this configuration it does nothing, except debug logging on the ‘Connect to WS’ event (just overrides the StompSessionHandlerAdapter)

Now it’s time to subscribe the /queue/my-id Channel within the session:

stompSession.subscribe(
    "/queue/my-id", 
    new MyStompFrameHandler((payload) -> resultKeeper.complete(payload.toString())));

The MyStompFrameHandler class is responsible for handling the incoming message in within the session and completing the CompletableFuture promise that it received as an argument. CompletableFuture is a helper variable needed to test asynchronous code:

CompletableFuture<String> resultKeeper = new CompletableFuture<>();

And the handler uses it as follows:

public class MyStompFrameHandler implements StompFrameHandler {

    private final Consumer<String> frameHandler;

    public MyStompFrameHandler(Consumer<String> frameHandler) {
        this.frameHandler = frameHandler;
    }

    ...

    @Override
    public void handleFrame(StompHeaders headers, Object payload) {
        log.info("received message: {} with headers: {}", payload, headers);
        frameHandler.accept(payload.toString());
    }
}

Sending the message

Message is sent by a WsProxy with SimpMessagingTemplate:

@Component
public class WsProxy {

    private SimpMessagingTemplate messagingTemplate;

    @Autowired
    public WsProxy(SimpMessagingTemplate messagingTemplate) {
        this.messagingTemplate = messagingTemplate;
    }

    public void sendMessage(String clientId, String payload){
        messagingTemplate.convertAndSend("/queue/" + clientId, payload);
    }
}

On some machines it’s also good to wait until the connection is fully established so don’t hesitate to add good old:

Thread.currentThread().sleep(1000);

Testing the result asynchronously

The code in test is async so I pass the Future and wait until it completes with the expected result, or to fail test after timeout on waiting for the response, verifying its body:

assertThat(resultKeeper.get(2, SECONDS)).isEqualTo("test-payload");

That’s it

Now you can run the test, it will start your app, send a message, receive it and verify the contents. Which is everything you need to implement the WebSockets.

Source Code

I’m sure that seeing the source code will make you understand the article better. Grab it from my GitHub: https://github.com/yacekmm/looksok/tree/WebSocketDemo/Spring/WebSocket





Proxy configuration for Gradle

6 05 2017

Are you using gradle behind corporate proxy? Here is how to configure proxy data for gradle (urls and credentials)

gradle.properties file

Gradle (and gradlew as well) is automatically reading gradle.properties file that is either in project directory or in USER_HOME/.gradle directory.

Inside of gradle.properties file set properties:

systemProp.http.proxyHost=http_proxy_ip_or_url
systemProp.http.proxyPort=port
systemProp.http.proxyUser=username
systemProp.http.proxyPassword=pwd
systemProp.https.proxyHost=https_proxy_ip_or_url
systemProp.https.proxyPort=port
systemProp.https.proxyUser=username
systemProp.https.proxyPassword=password

The configuration above concerns both http and https traffic.

Inline properties

As an alternative you can specify properties in each gradle command by:

gradle build -Dhttp.proxyHost=http_proxy_ip_or_url ...




Free weekend on CodeSchool!

19 11 2016

Get off from beaten track with new technology you always wanted to learn – CodeSchool.com gives free access to all its courses over this weekend! 

Try it: https://www.codeschool.com/free-weekend

There are several courses that takes 2 to 4 hours to attend so no excuses :)





New Technology Radar – what’s new for me

12 11 2016

Here is the new analysis of trends in Technology, Tools, Platforms and Languages & Frameworks. ThoughtWorks is sharing their experiences from projects they support or build in a periodic Tech Radar publications. The pdf version for Nov 2016 is here and the newest edition is always here. In this post I’m going to share remarks that affects currently my daily job.

 

TECHNIQUES

ADOPT Customer Driven Contracts

A must in a microservice environment, when you need to test your service in isolation from others in a line with theirs API (contract). There are several tools utilizing that, depending on your technology. Not all of them are mature enough, however it is a good investment to use them.

ADOPT Pipeline as a code

Manual CI job configurations finally can be replaced. for sure your CI has some support of keeping the configuration as a code that is tested and versioned together with your source code.

HOLD A single CI instances for all teams

I can imagine 20 projects on one CI, one is building and 19 are in queue. Then comes the 21st project and it changes the CI config that breaks others builds. One CI for whole organization is not a solution.

HOLD Anemic REST

REST API is not just an API to browse database. Feel free to model your business processes in API as well. If you are adding new user to your system, you don’t need to POST on ‘/users’, since you have a table ‘users’ in your DB. Go on and make an API for business process that you are modelling and POST for ‘/registration’

 

TOOLS

ADOPT Consul

A Service Discovery tool supporting DNS and HTTP discovery with customizable health checks. Right now I’m into Eureka so time to compare those two once again

ADOPT Graphana

As a live monitoring tool for visualizing your metrics. Definetely useful and easy to integrate with your framework

 

LANGUAGES & FRAMEWORKS

ADOPT Spring Boot

It’s in adopt since April, however is widely used in industry in production systems for a bit longer. I have an experience with it in production systems and definetely agree with this judgement.

HOLD AngularJS

Do not start a new project with AngularJS v1. If you start a new project ThoughtWorks prefers ‘the ramp-up speed and more maintainable codebases we are seeing with Ember and React’





Online ‘learning-by-doing’ courses I attended – was it worth the money?

22 10 2016

There are more and more online courses available. The ‘Learning by doing’ are in my particular interest. Here I want to:

– list the courses or sites I attended and write a short summary

– know the same from your side – feel free to add your thoughts on online courses.

 

Where I have been

Znalezione obrazy dla zapytania linux academy

About: Linux Academy is about the Ops part of software development. It teaches basics and advanced concepts of clouds, infrastructure automation, containers and so on.

Good? yes. Currently I am during the Ansible course and I recommend it.

Why? There are short lectures and so called ‘server lab’ where you can create up to six cloud servers and do all the exercises on them.

Why not? n/a

 

Code School

About: For learning web development (HTML, JS, CSS). Starting from real basics to advanced Topics.

Good? yes. I did HTML, JS and CSS (partially also git). JS starts from real basics but ends with quite important advanced topics.

Why? You have code editor online, so you do all the exercises online, have instant feedback

Why not? Maybe first you’d like to try CodeAcademy (below) which is free.

 

About: For web development basics

Good? yes, as a starting point. If you need more advanced topics, go to CodeSchool

Why? Its free and good. Exercises and course is held in a browser

Why not? if you need advanced topics, go to CodeSchool.

 

Znalezione obrazy dla zapytania industrial logic

About: Courses teaching Design Patterns, Refactorings, Code Smells detection for Object Oriented Languages. Advanced topics for practitioners.

Good? yes. The exercises you do on your IDE with their plugin installed. Plugin traces all your steps on doing the exercises and provides you with the feedback assessing not only your result but also the style you’ve reached it – I liked the feedback

Why? Practical application of refactoring techniques, Design Patterns with automated IDE refactorings / shortcuts

Why not? very expensive!

 

Znalezione obrazy dla zapytania coursera

About: various set of courses, not only for developers. I did only the Scala course there.

Good? yes, but probably depends on course. Scala course was well prepared, with practical exercises in your IDE

Why? free site, with a lot of courses. You can always start doing one. Coursera has schedule so encourages you to regular attendance

Why not? n/a

 

Where I haven’t been

Treehouse, lynda, pluralsight, edX. But are they even interactive courses or just a set of video tutorials? Let me hear from you!





Spring: Securing REST API with BasicAuth

23 07 2016

The simpliest, yet effective way to secure Spring REST API is to use Basic Auth. I’m going to show how to do it with Spring Security.

Note

Basic Auth is stateless (no need to manage sessions) and simple to implement. However it should be used with https only if outside of a trusted network. Also be aware that there are issues with Basic Auth (http://security.stackexchange.com/questions/988/is-basic-auth-secure-if-done-over-https)

Source Code

As usual, get it from my GitHub: https://github.com/yacekmm/looksok/tree/HttpBasicAuthDemo/Spring/SecurityBasicAuth

1. Create a project

Use your favorite tool. I choose Spring Boot Initializr in IntelliJ. Add web and security starter poms:

Przechwytywanie

Note that right after you add the security starter pom to your classpath, the auto configuration kicks in and your app is secured by default (the default password is logged on the application startup). I will override that default configuration and customize it later on.

2. Create Controller with urls to be secured

The most straight forward one as possible like mine below:

@RestController
@RequestMapping("/api")
public class SecuredController {

    @RequestMapping(value = "/secured", method = GET, produces = TEXT_PLAIN_VALUE)
    public String securedGet(){
        return "You have access to secured API!";
    }

    @RequestMapping(value = "/admin", method = GET, produces = TEXT_PLAIN_VALUE)
    public String adminGet(){
        return "Welcome, Admin";
    }

    @RequestMapping(value = "/open", method = GET, produces = TEXT_PLAIN_VALUE)
    public String openedGet(){ return "this API does not require authentication"; }
}

It contains 3 endpoints. Each of them I will secure to be accessible on various levels.

3. Create Basic Auth security configuration for selected endpoints

As in one of my previous tutorials (Spring Security Tutorial: Authorization and user roles), you need to extend the WebSecurityConfigurerAdapter:

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    ...
}

and configure the HttpSecurity in there:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .httpBasic()
        .and()
            .authorizeRequests()
            .antMatchers("/api/secured").authenticated()
            .antMatchers("/api/admin").hasRole("ADMIN")
            .antMatchers("/api/open").permitAll()
            .anyRequest().authenticated();
}

What just happened here?

  • first of all with httpBasic() you say that BasicAuth will be in use
  • then you say that want to authorizeRequests() so that:
    • url matching “/api/secured” is accessible for all authenticated users
    • url matching “/api/admin” is accessible for ADMIN users only
    • url matching “/api/open” is accessible with no authentication required
    • any other request is accessible for all authenticated() users

4. Create security configuration: usernames and passwords

Here I create two user accounts with USER and ADMIN role. That method is also in my WebsecurityConfig class. Using those accounts you can log in to the application:

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
            .withUser("admin").password("superhard").roles("ADMIN")
        .and()
            .withUser("user").password("easyone").roles("USER");
}

Normaly you would use the AuthenticationProvider, probably connected with your database or LDAP. This is separate big subject not to be cevered in this article.

5. Go ahead and try it!

Run the app and:

  1. head over to url 127.0.0.1:8080/api/open – you should access it without logging in
  2. head over http://127.0.0.1:8080/api/secured – the browser will ask you for username and password: type ‘user’ and ‘easyone’. Then you should access the site
  3. head over to url http://127.0.0.1:8080/api/admin – you will get 403 Forbidden, since browser cached your auth data and reused it. Only admin is permitted to access that url.
  4. To log in as another user you need to restart your browser and open the url once again. You can also use private browsing.




Spring JMX: Manage beans in runtime

19 06 2016

JMX (Java Management Extensions) allows to change the bean field values or invoke bean methods on the fly at runtime. It is in opposite to DI where you configure application when it starts, using profiles or classpath. With JMX you can tune, monitor or configure your application anytime when it is running.

At the heart of JMX are MBeans. MBean is a JavaBean that exposes certain methods that define the management interface. To make of those exposed methods you can use tool of your choice. My choice is Java Mission Control. Others are VisualVM or JConsole.

With Spring you can expose those methods with annotations. I will guide you how to do it.

Source Code

Source Code for this tutorial you can find on my GitHub at: https://github.com/yacekmm/looksok/tree/JmxDemo/Spring/SpringJMX

Demo app introduction

The simplest app to demonstrate JMX basic capabilities is app with one REST controller exposing one GET resource method:

@RequestMapping(value = "/api/my-jmx-value", method = GET)
public int getManagedAttributeValue(){
    return myJmxValue;
}

The return value of that method will be managed by JMX. You can run the app from my GitHub sources and access that resource at http://127.0.0.1:8080/api/my-jmx-value. The default hardcoded value of 25 will be returned and displayed in your browser.

Exposing Bean as a MBean
First of all you need to annotate your Bean with @ManagedResource so it can expose its methods. My annotated Controller is:

@RestController
@ManagedResource(objectName = "jmxDemo:name=MyController")
public class MyController { ... }

The name value indicates the name under which the MBean will be exported. jmxDemo is a domain and MyController is the name of the MBean.

Exposing Bean’s Attribute via JMX

You can expose a field via accessors methods annotated with @ManagedAttribute. Thanks to that you will be able to change the field value via JMX. Annotated attributes in my controller are:

private int myJmxValue = 25;

@ManagedAttribute
public void setMyJmxValue(int myJmxValue) {
    this.myJmxValue = myJmxValue;
}
    
@ManagedAttribute
public int getMyJmxValue(){
    return myJmxValue;
}

Exposing Bean’s Method via JMX

You can also expose your Bean method to be able to invoke it by JMX. This time the @ManagedOperation annotation should be used:

@ManagedOperation
public void logMyJmxValue(String requester){
    log.info("{} requested to log JmxManagedValue which is: {}",
            requester, myJmxValue);
}

Managing JMX Attributes in Java Mission Control

Java Mission Control (JMC) is a tool you can find in your jdk/bin folder under the jmc.exe name. With JMC you can monitor java process health and manage MBeans. See image below to get familiar with the tool and see how to find your MBean there:

JMC-MBeans

1. run the JMC and under the list of local java processes find your app and connect to its MBean Server
2. go to MBean Browser tab and find jmxDemo domain
3. under MyController you can set the MyJmxValue attribute to any nymber you want
4. go to http://127.0.0.1:8080/api/my-jmx-value and test that the returned value is exactly how you set it

Managing JMX Operations in Java Mission Control

Similarly you can invoke exposed operation via JMC. Go to Operations tab, provide the operation param and execute it. In your app logs you should find message indicating that it actually ran:

[4)-192.168.56.1] p.l.spring.controller.MyController       : Jacek requested to log JmxManagedValue which is: 99







%d bloggers like this: