Configure CircleCI

CircleCI is Continuous Integration, a development practice which is being used by software teams allowing them to to build, test and deploy applications easier and quicker on multiple platforms.

I followed the below steps to connect my BitBucket project to CircleCI

  1. Signed into Circle CI using Bitbucket credentials.
  2. Clicked Add Projects from Left hand navigation.
  3. Clicked Set Up Project on rest-r-ant-engine project line item.
  4. On the Setup Project page, picked Linux Operatting System and Gradle (Java) language.
  5. Followed the Next Steps actions as provided on Set Up Project page.
    1. On my local machine created folder .circleci under the root folder.
    2. Populated config.yml with the contents of config.yml as provided on the page.
    3. I updated the content of config.yml with the following
      1. Run with OpenJDK 11.
      2. Set working directory to rest-r-ant-engine folder.
      3. Use provided gradle using gradlew.
      4. Use ./gradlew check to do the build. My main aim is make sure all the tests pass on every git push.
      5. Set store_test_results variable so that it shows JUnit summary on Test Summary page.
      6. Set store_artifacts variable so that it shows the check reports on Artifacts tab of Circle CI.
    4. Here is the content of my config.yml.

Deploy Springboot Application in Heroku

Heroku is a cloud platform as a service supporting several programming languages. Heroku, one of the first cloud platforms, has been in development since June 2007, when it supported only the Ruby programming language, but now supports Java, Node.js, Scala, Clojure, Python, PHP, and Go.

Here is my running notes for deploying Rest R Ant application (a Springboot Application) in heroku.

  • I installed Heroku CLI from this link.
  • I logged in to heroku CLI.
  • In order to deploy to Heroku, we need to provision a new Heroku app. I created a new app named restrant2 (restrant name was already taken). This also created a remote git repository called restrant2 in my local git repository.
  • Since I plan to run my application using JDK 12, I created a system.properties file under the root of the project with one entry java.runtime.version=12
  • In order to deploy my application in heroku, I just had to push to heroku remote respository.
  • I verified the application is deployed in heroku.
  • Here is the source code for this blog.

Ping Service For Springboot Project

For the plain Vanila Spring boot project that we created, I will create a Ping Service. This service can be later used to check if the host server of this application is alive.

PingControllerITest.java

I followed Test Driven Development so first I created unit test class called PingControllerITest.java. I called this a ITest for integration test as we will be loading Spring Mock MVC engine to test our Rest API.

As general design, it is better to separate unit tests from integration tests. Unit tests should be able to run pretty quickly. I will show how to separate running unit tests from integration tests in a gradle project at a later blog.

I followed this blog to code this JUnit test as shown below.

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class PingControllerITest {
  @Autowired
  private MockMvc mockMvc;

  @Test
  public void testPingDefault() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/ping/").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().string(equalTo("Ping: Hi from SpringBoot")));
  }

  @Test
  public void testPingName() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/ping/Amar").accept(MediaType.APPLICATION_JSON))
        .andExpect(status().isOk())
        .andExpect(content().string(equalTo("Ping: Hi Amar!, from SpringBoot, How are you?")));
  }
}

As shown in the code above, I used Hamcrest Matchers library to do the assertions. The following link shows the benefit of using Hamcrest instead of JUnit assertions.

There are two API I am testing above.

  1. /ping should return Ping: Hi from SpringBoot.
  2. /ping/amar should return Ping: Hi Amar!, from SpringBoot, How are you?

PingController.java

After failing PingControllerITest.java, I created class PingController.java to pass the tests. As shown in the code below, this Rest Controller is a basic Spring impmlementaion of REST.

@RestController
public class PingController {

  private static final String PING_MESSAGE = "Ping: Hi %s!, from SpringBoot, How are you?";
  private static final String PING_MESSAGE_DEFAULT = "Ping: Hi from SpringBoot";

  @RequestMapping({"/ping/{name}", "/ping"})
  public String ping(@PathVariable(required = false) Optional<String> name) {
    String pingMessage = PING_MESSAGE_DEFAULT;
    if (name.isPresent()) {
      pingMessage =  String.format(PING_MESSAGE, name.get());
    }
    return pingMessage;
  }
}

Now I can run the Spring boot application and test the Ping service using a browser.

The code for this blog is available at this link.

Configure Checksytle for gradle project

Checkstyle is a development tool to help programmers write Java code that adheres to a coding standard. It automates the process of checking Java code to spare humans of this boring (but important) task. This makes it ideal for projects that want to enforce a coding standard.

I integrated Checkstyle using Gradle plugin. The following are the steps to configure Checkstyle.

build.gradle: The following are changes that are done in build.gradle to incorporate Checkstyle:

......

ext {
	checkstyleToolVersion = "8.21"
}
apply plugin: "checkstyle"

.......

checkstyle {
	toolVersion = "${checkstyleToolVersion}"
	ignoreFailures = true
	configFile = file("config/checkstyle/google_checks.xml")
}

tasks.withType(Checkstyle) {
	reports {
		xml.enabled false
		html.enabled true
		html.stylesheet resources.text.fromFile("config/checkstyle/checkstyle-author.xsl")
	}
}

......
  1. I added 8.21 version of Checkstyle.
  2. I am using google checks version of rules for checking code style. But I had to comment out the following modules as I was getting an error.
    1. InvalidJavadocPosition.
    2. MissingJavadocMethod.
  3. I stored google_checks.xml under config/checkstyle folder under the root folder.
  4. I used checkstyle-author.xsl for generating report of Checkstyle. I stored this file under config/checkstyle under the root folder.
  5. Now when I run ./gradlew check, Checkstyle plugin runs rules and shows results under build/reports/checkstyle folder.

Configure git to handle line endings

Since Rest R Ant application is an open source project, we need to make sure git takes care of normalizing line endings. Here is a link that explains why we need to do this.

I added a .gitattributes file under root folder of project to instruct git to handle line endings during checkout and commit operations. Here is the content of my .gitattributes file:

Git Attributes

My entries in .gitattributes is telling git to do the following:

  1. On all text files, update end of line with lf (line feed) like unix way.
  2. Let git handle javascript files automatically.
  3. On all .bat and .cmd files replace eol with crlf (carriage return line feed) the windows way.
  4. All jar files, png, pdf files are considered binary, which means they wont
    be modified during checkout and commit operations.

If .gitattributes file is added during middle of a project, run the following command to correct all end of line on all files.

  • git add –renormalize .

–renormalize option applies “clean” process freshly to all tracked files to forcibly add them again to the index.

Create Plain Vanilla Springboot Project

I have decided to build Rest R Ant Engine using Spring Boot Framework. I will be following Clean Architecture to build it using Spring Boot Framework.

Steps to create Springboot project

  1. Went to Spring initializer website at https://start.spring.io/.
  2. Picked the following options.
    1. Project: Gradle Project.
    2. Language: Java
    3. Spring Boot: 2.1.6 ( latest as of this writing)
    4. Project Metadata
      1. Group: com.aja
      2. Artifact: restRant
      3. Name: restRant
      4. Package Name: com.aja.restrant
      5. Packaging: jar
      6. Java: 12
    5. Dependencies
      1. Web: Spring Web Starter
  3. Clicked Generate the project button.
  4. This downloaded my Springboot plain vanila project as a zip file.
  5. I cloned Git repository that we created in BitBucket using the following command.
    1. git clone https://amarAjaTrends@bitbucket.org/Aja_Trends_LLC/rest-r-ant-engine.git
    2. This created a folder rest-r-ant-engine.
  6. I extracted the contents of restRant folder within zip file into rest-r-ant-engine folder.
  7. Ran the following command to run rest-r-ant-engine. gradlew bootRun

Create Open Source Bitbucket Project

I created a new open source project called Rest R Ant. I created that project in Bitbucket. The following are the steps that I followed.

Create Rest R Ant Project

Bitbucket Project allows you to group repositories and to manage permissions for them in an aggregated way.

Here are the steps to create a Bitbucket project.

  • Login to Bitbucket.
  • Click on Create Project button on the right hand corner.
Create Project Button
  • Fill project owner, name, description.
  • Uncheck Privacy check box if you want to create an Open Source Project.
  • Pick an Avatar for the project. You can upload an image and create Avatar from it. I downloaded an image from pixabay.com and used it as an Avatar.
  • Click Create Project button.
Create a project

Create Rest R Ant Engine Repository

Once the project has been created, create a repository as shown below.

  • From the confirmation screen of Project creation, click on Create respository button.
  • Owner and Project fields are prefilled.
  • Enter a Repository name. My repository name is Rest R Ant Engine.
  • Uncheck Access level if you want to create an open source repository.
  • Inclue a default Readme file. I picked Yes, with a template.
  • Pick Version Control. I picked Git.
  • Click Advanced Settings.
  • Enter some description.
  • Click on Check boxes Issue Tracking and Wiki if you want to use base issue tracking and wiki provided by BitBBucket.
  • Finally pick language. I will be coding Rest R Ant Engine using Java.
  • Click Create Repository button.
Create Repository
  • A new repository is successfully created.
Create Repository Success

Integrate Repository with Trello

BitBucket allows you to integrate a repository with Trello. Using this feature one can create repository branches based on Trello tasks. The following are instructions to link Trello with Bitbucket.

  • Login to Trello with your Trello Userid/password.
  • Create a new Team called Rest R Ant.
  • Logout of Trello.
  • Now Login to Bitbucket and go to your repository.
  • Click on Boards on the left navigation menu.
Link Trello 1
  • Click on Connect to Trello button.
Link Trello 2
  • Let Bitbucket access your Trello account.
Link Trello 3
  • Click on Create new board button.
Link Trello 4
  • Fill the popup with Rest R Ant Engine as Board name and Rest R Ant as Team and click Create button.
Link Trello 5
  • Set board access such that all repository Write users can access the board.
Link Trello 6
  • Now you can see Trello board from within BitBucket.
Link Trello 7