Skip to main content

Posts

Showing posts with the label JAVA

RADUS#5 - Unit testing controller

  As we have written few methods in our controller, let's write quick unit tests for couple of methods: @RunWith (SpringRunner . class) @WebMvcTest (TodoController . class) public class TodoControllerTest { @Autowired private MockMvc mvc; @MockBean private TodoServiceImpl todoService; private final String BASE_PATH_V1 = "/api/1/0" ; @Test public void testHello() throws Exception { mvc . perform( get(BASE_PATH_V1 + "/hello" )) . andExpect(status() . isOk()) . andExpect(content() . string( "Hello Rest demo" )); } @Test public void testGetUsers() throws Exception { // Mock setup Map < Integer, String > expected = new HashMap <> (); expected . put( 1 , "Todo - 1" ); expected . put( 2 , "Todo - 2" ); Mockito . when(todoService . getTodos()) . thenReturn(expected); //...

RADUS#3 - CRUD and REST API's (focus on HTTP Methods, Annotations and no DB)

  90% of the time services does CRUD operations with REST over HTTP protocol so in this post we'll look into different HTTP methods and annotations we can use to communicate with the service to perform crud operations.  We'll be building a simple todo API where you can: Create a todo (method: HTTP POST, endpoint: **/api/1/0/todos) Read list of todos (method: HTTP GET, endpoint: **/api/1/0/todos) Read a single todo (method: HTTP GET, endpoint: **/api/1/0/todos/{id}) Delete a todo (method: HTTP DELETE, endpoint: **/api/1/0/todos/{id}) **To keep things simple and not shift the focus out of the controller class, we will store all these todos in in-memory using a simple Map controller code using java: package com . artifactsbyrake . restdemo . controller ; import org.springframework.http.HttpStatus ; import org.springframework.http.ResponseEntity ; import org.springframework.util.ObjectUtils ; import org.springframework.web.bind.annotation.* ; import org.springframework...

RADUS#2 - Important classes and Annotations

Once we extract the project into our IDE you'll notice couple of classes already created. Let's focus on one of the important class called Application class, which does all the magic to run your spring boot application package com . artifactsbyrake . restdemo ; import org.springframework.boot.SpringApplication ; import org.springframework.boot.autoconfigure.SpringBootApplication ; @SpringBootApplication public class RestdemoApplication { public static void main ( String [] args ) { SpringApplication . run ( RestdemoApplication . class , args ); } } Notice the main method in the above class which is used to launch the spring application Also pay attention to the annotation " @SpringBootApplication ", this is used to identify the Application class in spring boot What does spring boot do when it runs the app? To run an application, spring does the following things on application start-up: Create's instance of Spring's ApplicationCo...

RADUS#1 - Create Spring Boot App

  To develop REST API's we will be using a spring boot to quickly bootstrap an application and add all the dependencies needed to develop the API's Different ways of creating a spring boot app:  CLI (Command Line Interface) IDE (Eclipse, IDEA, STS...) The above two are not so popular but most developers create spring application using the web app  start.spring.io start.spring.io  provides different options while creating the application. You can choose: Project: Maven, Gradle Language: Java, Kotlin, Groovy Spring Boot Version Project Metadata Group Artifact Name Description Package Name Packaging (Jar, War) Java version Dependencies - Based on the application type you're developing you can choose different dependencies to include in your project Here's a screenshot of an example project i'm creating:   select all the options as shown above click Generate and this will download a zip file of the project  import the project into an IDE that supports Spring Bo...

Settings.xml for Maven, JFrog

For development and deployment of applications we always use an artifactory in real-time world to host artifacts needed for your build and also as a target to deploy artifacts generated in the build process. For Maven, to communicate with artifactory we need a settings.xml file which is usually located at "/User/rake/.m2/settings.xml" this file consists of how to authenticate to the artifactory servers and authorizations to read/ write to different locations like release, snapshots e.t.c... Settings.xml can be generated using the artifactory you're using which in my case is JFrog , but here's a sample settings file for your reference incase you're feeling lazy☺ <?xml version="1.0" encoding="UTF-8"?> <settings xsi:schemaLocation= "http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns= "http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi= "http://www.w3.org/2001/XM...

Web Services N Buzz words

WebService: A service that is available to access across internet or intranet. Provides communication between different applications built on different platforms. Why Webservices? To avoid reinventing the wheel by exposing existing functionalities built on any platform to be exposed to consumers on internet or developers within same network. Webservices are loosely coupled they can updated without disturbing other modules in application. Ease of integration for applications built across enterprise. Web Service Jargon: WSDL (Web Service Description Language) Interface that describes the implementation details of web service. A contract between producer and consumer for exchanging data. Written in standard XML format. WSDL consists information about function names, arguments, return types and End point to be used. SOAP (Simple Object Access Protocol) Protocol to exchange structured information in XML format. UDDI (Universal Description, Discovery a...