Thought Note

I feel it went well at work today.

I am still behind my plan, slow at writing codes.

But I have good understanding of what I did today, I learned something new, I had good discussions with my team, I learned a junior colleague and at the same time learned myself how to work and help others.

When something goes well, I want to continue. I feel fun and like what happened.

When I get stuck, I still want to continue and want to beat the problems.

I sat with a project manager today, I don’t think I want to have her job.

Is it because what I’m doing is considered to be a strategic product and innovation at my department? I feel important and am doing something important.

Is it activities? I lead development, provide architecture, practice agile activities.

Is it atmosphere at work? Now, I have a team, work with others, have freedom to choose technology and working sets.

Last thought

Can everyone be a teacher?

It would be cool if I can teach junior developers to built a system.

Advertisements

Arghhh!

I get stuck on writing a unit test for a Camel route for 2 days now.

Before I shut down my computer today, I got a green light on my unit test but it doesn’t feel I’m thorough with the subject.

Unit testing a Camel route should not be difficult but I hardly find a good solution/example to my problem.

There are too many ways to unit test the Camel route which requires more time to read and understand the subject.

What I learned on August 6th,

Add Camel and ActiveMQ to a Spring Boot application

Dependencies

From version 2.22.0 Camel supports Spring Boot 2.0.x. With a camel-spring-boot-starter dependency, when you write a route, it allows you to annotate your class with a @Component annotation. Spring will auto scan and instantiate the route and add it to a CamelContext.

<dependency>
	<groupId>org.apache.camel</groupId>
	<artifactId>camel-spring-boot-starter</artifactId>
	<version>${camel-version}</version>
</dependency>

Add Camel’s features for ActiveMQ to your Camel project. activemq-broker.jar is excluded since we’re going to connect to a standalone ActiveMQ broker.

<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-camel</artifactId>
	<!-- version>5.14.0</version -->
	<exclusions>
		<exclusion>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
		</exclusion>
	</exclusions>
</dependency>

Add Spring’s supports and infrastructure to connect to ActiveMQ broker.

 <dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-activemq</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.apache.activemq</groupId>
			<artifactId>activemq-broker</artifactId>
		</exclusion>
	</exclusions>
</dependency>

activemq-broker.jar is provided at runtime when connecting to a standalone ActiveMQ broker.

<dependency>
	<groupId>org.apache.activemq</groupId>
	<artifactId>activemq-broker</artifactId>
	<scope>provided</scope>
</dependency>

Create a Camel route

@Component
public class MyCamelRoute extends RouteBuilder {
	@Override
	public void configure() throws Exception {
		from("activemq:foo")
			.to("log:sample");
		
		from("timer:bar")
			.setBody(constant("Hello from Camel"))
			.to("activemq:foo");		
	}
}

Now you’re good to run the application.

Secure Restful Web Services

What do we want to solve?

We are going to impleument a bunch of RESTful web services. Some services are consumed internally and some are consumed by Internet users.

For internal clients, we want to use HTTP basic authentication to keep implementation simple. For Internet users, we want to use Swedish BankID, Google or Facebook account as a means to authenticate the users.

Tasks

Study Spring Security, how to implement the requirements above.

 

Write a Unit Test for Spring Boot REST Web Services

Note: A purpose of this post is to repeat knowledge on how to unit test Restful web services in a Spring Boot applications.

What do you need to know when making a REST web request

A GET request

  • username and password or a token for authentication
  • a service URL
  • request parameters
  • some required HTTP headers

What do you need when running a test

From a list of what you need to know to be able to make a request, it defines how you gonna write your unit test

In our unit test, we want to test a @RestController component. The controller uses a @Service component which we want to ignore. Therefore, we mock the @Service component.

Below is what we need to do to create a unit test

Load a test context

Since it is a Spring Boot application, we are going to use test support functionality provided by Spring Boot and Spring Test framework

@RunWith (SpringRunner.class)

Launch the controller under test

@WebMvcTest (value = StudentController.class, secure = false)

Mock components that are not under test

The StudentController uses the service StudentService but we’re not going to test the StudentService. Therefore, we mock it.

@MockBean StudentService studentService;

@MockBean will inject a StudentService instance into an ApplicationContext.

Mockito.when(studentService.retrieveCourse(Mockito.anyString(), Mockito.anyString())).thenReturn(mockCourse);

Build a request

Use MockMvcRequestBuilders

RequestBuilder rb = MockMvcRequestBuilders.get("/students/Student1/courses/Course1").accept(MediaType.APPLICATION_JSON);

Build an expected response

String expected = "{id:Course1,name:Spring,description:\"10 Steps\"}";

Make a call

@Autowired private MockMvc mockMvc;
...
MvcResult result = mockMvc.perform(rb).andReturn();

Verify the result

 

JSONAssert.assertEquals(expected, result.getResponse().getContentAsString(), false);

In-memory Database: H2

You’re never too old to learn

I use H2 as a database when I refresh my knowledge about JPA (Java Persistence API). Therefore, I think to write a little bit about H2 database and how to use H2 as a database in your Spring Boot application.

Note: A purpose of this post is to help me review what I have learned. As I am old and start to have bad memories.

What is H2?

H2 is a relational database management system written in Java.

Enable H2 database in Spring Boot application

  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <scope>runtime</scope>

Enable H2 console

Configure the following properties in application.properties at src/main/resources

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2

Persistent, embedded database

Configure the following properties in application.properties

# Datasource
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver

 

Clean up MySql database

There are things you can think about when deleting data from a large table. In my case the table contains 40 million records.

Symptoms 1: it took long time to execute a delete statement.
Solution: delete smaller chunks of data

Use the following command to find out what are being executed in database

mysql> show processlist;

To see which tables are locked (in used)

mysql> show open tables where in_use > 0;

To kill a process ex. a query that takes long time to execute

mysql> show processlist; 
mysql> kill <pid>;