Cloud Kube | Simple REST Endpoint and Test

Cloud Kube | Simple REST Endpoint and Test

Kubernetes Application Hosted in the Cloud

full course
  1. Kubernetes Application Hosted in the Cloud
  2. Cloud Kube | Create Github Repo
  3. Cloud Kube | Simple REST Endpoint and Test
  4. Cloud Kube | Build Pipeline Initialization
  5. Cloud Kube | Docker Build and Registry
  6. Cloud Kube | Helm Initialization and Chart Publishing
  7. Cloud Kube | Setup Cloud Hosting
  8. Kube Cloud | Automate Kube Deploy

In the previous article we created the skeleton repo and project. In this article we’ll be building a very basic endpoint and component test.

Although I do hate hello world examples my intent is to get us to a point where we can focus on the build and deploy path. The easiest way to do that is via a basic microservice that we can validate our deployment with. Then we’ll iterate on our application behavior. A basic hello world endpoint will be sufficient for that. Additionally, this is how I would recommend building out any project that does not already have a build and deploy pipeline created.

Create a Basic Microservice Endpoint

Create a new directory under src/main/java/com/bullyrooks/cloud_application called /controller. Create a new java class there called HelloWorldController.

Create a new directory under controller called /dto and create a new java class called HelloWorldResponse in that directory.

In HelloWorldResponse add this code:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class HelloWorldResponse {
    private String message;
}

In HelloWorldController add this code:

@RestController
public class HelloWorldController {

    @GetMapping("/helloworld")
    public HelloWorldResponse getHelloWorld(){
        return HelloWorldResponse.builder().message("Hello World!").build();
    }
}

Manual Testing

Start your application inside intellij. Intellij should have already created an executable configuration for spring boot. Click the green start arrow

you should see it start up in the terminal

2022-01-02 13:57:27.624  INFO 23284 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2022-01-02 13:57:27.631  INFO 23284 --- [  restartedMain] c.b.cloud_application.CloudApplication   : Started CloudApplication in 1.339 seconds (JVM running for 1.961)

And you should be able to hit your endpoint with a GET at http://localhost:8080/helloworld. I recommend using postman

Automated Testing

Stop your running application. Create a new directory called /controller under src/test/java/com/bullyrooks/cloud_application. In that directory create a test class called HelloWorldControllerTest

Add this code to that class:

package com.bullyrooks.cloud_application.controller;

import com.bullyrooks.cloud_application.controller.dto.HelloWorldResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;

import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
@AutoConfigureMockMvc
public class HelloWorldControllerTest {
    @LocalServerPort
    int randomServerPort;

    @Test
    void testAddCustomerSuccess() throws URISyntaxException, JsonProcessingException {
        RestTemplate restTemplate = new RestTemplate();
        String baseUrl = "http://localhost:" + randomServerPort + "/helloworld";
        URI uri = new URI(baseUrl);

        HttpHeaders headers = new HttpHeaders();
        headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

        ResponseEntity<HelloWorldResponse> result = restTemplate.getForEntity(uri, HelloWorldResponse.class);

        //Verify request succeed
        assertEquals(200, result.getStatusCodeValue());
        HelloWorldResponse response = result.getBody();
        assertEquals("Hello World!", response.getMessage());
    }
}

Right click on your test class and select Run HelloWorldControllerTest

You should see it pass

This is a good place to stop. Lets get this up on a branch.

$ git checkout -b microservice_dev
Switched to a new branch 'microservice_dev'

$ git add .

$ git commit -m "endpoint and test"
[microservice_dev 655eb72] endpoint and test
 4 files changed, 76 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/com/bullyrooks/cloud_application/controller/HelloWorldController.java
 create mode 100644 src/main/java/com/bullyrooks/cloud_application/controller/dto/HelloWorldResponse.java
 create mode 100644 src/test/java/com/bullyrooks/cloud_application/controller/HelloWorldControllerTest.java

$  git push --set-upstream origin microservice_dev
Enumerating objects: 31, done.
Counting objects: 100% (31/31), done.
Delta compression using up to 4 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (20/20), 2.30 KiB | 1.15 MiB/s, done.
Total 20 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
remote: 
remote: Create a pull request for 'microservice_dev' on GitHub by visiting:
remote:      https://github.com/bullyrooks/cloud_application/pull/new/microservice_dev
remote:
To github.com-bullyrook:bullyrooks/cloud_application.git
 * [new branch]      microservice_dev -> microservice_dev
Branch 'microservice_dev' set up to track remote branch 'microservice_dev' from 'origin'.

We’re not going to push to main just yet. I want to keep this work on a feature branch for now so that I can build out my CICD pipeline.

0 comments on “Cloud Kube | Simple REST Endpoint and TestAdd yours →

Leave a Reply

Your email address will not be published. Required fields are marked *