Eddú Meléndez

Cloudbees

Time ago, I was looking for subversion server. I could have used Google code, but I had a problem with that, it is a public repository. So, I found Cloudbees and meets my expectation.

What is Paas?

Nowadays, we should know about Platform as a Service. But, it doesn’t matter if you don’t know it. You can learn something new everyday. Briefly, PaaS is a environment on cloud based on products from the providers. So, Cloudbees is a PaaS. In this case, Cloudbees was built using Sonar, Jenkins, Subversion, Git, MySQL, Tomcat and many other tools.

What can you find on Cloudbees?

You can find several products such as I have mentioned before, besides JFrog, SendGrid, AppDynamics, New Relic, Mongo HQ.

I suggest to use it at university projects. Sometimes we need to application server, and using Cloudbees we can have source control management, database, application server, maven repository. It is a good place to start teaching about Continuous Integration, Continuous Delivery.

Note: We have to consider that Cloudbees provide a limit space, if you want more space you have to buy. Cloudbees, is a good service. And I hope you can use it.

Database Change Management

First, I have some questions to you:

  1. Do you have scripts into the Source Control Management?
  2. Do you know which scripts have been executed into the database?
  3. Do you write scripts for rollback?
  4. Are DBAs involve in your development process?
  5. Tell me, how do you manage your database changes?

If I were a new team member, I will ask you how to build my database and maybe this would be your answer “Let me get a dump from my local database and I will send you”. This is valid if your dump has the same structure that production environment but I would not be 100% sure. Another option is get a backup from production, is not a bad idea if you want to work with real data and solve a specific issue but is not ever the case. So, you must be able to build the database whenever you want.

What it is my point?

Continuous Improvement, that is my point. We need to improve our delivery process every day and Database Change Management is something that you can not forget.

Which is involve?

  1. Every database modification should be written as a delta script.
  2. Add rollback section to your scripts.
  3. Use a naming conventions for your scripts.
  4. If a delta script has already been applied to a database is subsequently modified that subsequent modification will not be applied to the database.
  5. Version Control your scripts.
  6. Maintain a database changelog.

Advantages

  1. Reduce risks during deployments.
  2. Ensure you are executing the right scripts.
  3. You can build a new database.

After this, you can research about some tools such as liquibase or flyway in order to automated scripts execution and then promote communication with Database Team in your job.

Embedded Database Compatibility

Some weeks ago I was watching a video about Spring Testing, one thing that called my attention was one feature about embedded databases. It is about compatibility with other databases.

From my side, this feature is really useful because when I started using embedded database in my tests I had to change some types or remove lines in my script due to there are not supported (At that time, I didn’t know about it)

Lines below you can see how to setup MYSQL compatibility into HSQL and H2 databases, both for testing purpose. Also, you can see the dependencies if you are using maven projects.

HSQL

You can review this link for more information.

script.sql
1
SET DATABASE SQL SYNTAX MYS TRUE;
pom.xml
1
2
3
4
5
6
<dependency>
  <groupId>org.hsqldb</groupId>
  <artifactId>hsqldb</artifactId>
  <version>2.2.8</version>
  <scope>test</scope>
</dependency>

H2

You can review this link for more information.

script.sql
1
SET MODE MySQL;
pom.xml
1
2
3
4
5
6
<dependency>
  <groupId>com.h2database</groupId>
  <artifactId>h2</artifactId>
  <version>1.3.175</version>
  <scope>test</scope>
</dependency>

JUnit and Mockito

As you maybe know, JUnit is a java testing framework.

Before to talk about our mock framework, we need to be in the same page about what mock is?

Marting Fowler in his article Mocks are not Stubs said:

Mocks are objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Mockito is the mock framework which we will use.

Lets start with the example, first of all we need to setup our Maven project.

1. Adding dependencies

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<dependencies>
  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <scope>test</scope>
  </dependency>
  <dependency>
      <groupId>org.mockito</groupId>
      <artifactId>mockito-all</artifactId>
      <version>1.8.5</version>
      <scope>test</scope>
  </dependency>
</dependencies>

2. In this example, we will use annotations so we have three ways to enable them.

UserServiceImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
@RunWith(MockitoJUnitRunner.class)               //First method to enable mockito's annotations
public class UserServiceImplTest {

  @Rule
  public MockitoRule rule = MockitoJUnit.rule();    //Second method to enable mockito's annotations

  @Before
  public void setup() {
      MockitoAnnotations.initMocks(this);      //Third method to enable mockito's annotations
  }

}

Note: You can use one of them. I suggest the use of MockitoRule in favor of use another runners like org.junit.runners.Parameterized

3. UserServiceImpl HAS-A UserRepository. For that reason, create a UserRepository mock and it will be injected in UserServiceImpl. Then, we can mock UserRepository methods as you can see in step 3.

UserServiceImplTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class UserServiceImplTest {

  @Mock                               //1. Create UserRepository mock object
  UserRepository userRepository;

  @InjectMocks                        //2. Insert UserRepository mock object into UserService
  UserService userService = new UserServiceImpl();

  @Before
  public void setup() {
      //MockitoAnnotations.initMocks(this);
  }

  @Test
  public void testSomething() {
      String username = "emelendez";
      Mockito.when(userRepository.getPassword(username)).thenReturn("PasswordMock"); //3. Mocking objects
      userService.validateCredential(username);
      Mockito.verify(userRepository).getPassword(username);
  }

}

You can download the source code here

Why Am I Writing This Blog?

I am starting writing this blog because I want to share my knowledge and expertise with everyone. Also, I would like to get feedback from others.

My favourite topics are related to:

  • Agile Practices
  • Java
  • Build Tools (Gradle, Maven)
  • Mule ESB
  • Spring Framework
  • Testing

Also, I will share some books that I have taken into consideration during my continuous improvement process.