@@ -0,0 +1,13 @@
|
||||
package com.baeldung.micronaut.vs.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@ComponentScan("com.baeldung.micronaut.vs.springboot")
|
||||
public class CompareApplication {
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(CompareApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.micronaut.vs.springboot.controller;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.management.MemoryMXBean;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.micronaut.vs.springboot.service.ArithmeticService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/math")
|
||||
public class ArithmeticController {
|
||||
@Autowired
|
||||
private ArithmeticService arithmeticService;
|
||||
|
||||
@GetMapping("/sum/{number1}/{number2}")
|
||||
public float getSum(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
|
||||
return arithmeticService.add(number1, number2);
|
||||
}
|
||||
|
||||
@GetMapping("/subtract/{number1}/{number2}")
|
||||
public float getDifference(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
|
||||
return arithmeticService.subtract(number1, number2);
|
||||
}
|
||||
|
||||
@GetMapping("/multiply/{number1}/{number2}")
|
||||
public float getMultiplication(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
|
||||
return arithmeticService.multiply(number1, number2);
|
||||
}
|
||||
|
||||
@GetMapping("/divide/{number1}/{number2}")
|
||||
public float getDivision(@PathVariable("number1") float number1, @PathVariable("number2") float number2) {
|
||||
return arithmeticService.divide(number1, number2);
|
||||
}
|
||||
|
||||
@GetMapping("/memory")
|
||||
public String getMemoryStatus() {
|
||||
MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
|
||||
String memoryStats = "";
|
||||
|
||||
String init = String.format("Initial: %.2f GB \n",
|
||||
(double)memoryBean.getHeapMemoryUsage().getInit() /1073741824);
|
||||
String usedHeap = String.format("Used: %.2f GB \n",
|
||||
(double)memoryBean.getHeapMemoryUsage().getUsed() /1073741824);
|
||||
String maxHeap = String.format("Max: %.2f GB \n",
|
||||
(double)memoryBean.getHeapMemoryUsage().getMax() /1073741824);
|
||||
String committed = String.format("Committed: %.2f GB \n",
|
||||
(double)memoryBean.getHeapMemoryUsage().getCommitted() /1073741824);
|
||||
memoryStats += init;
|
||||
memoryStats += usedHeap;
|
||||
memoryStats += maxHeap;
|
||||
memoryStats += committed;
|
||||
|
||||
return memoryStats;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.micronaut.vs.springboot.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ArithmeticService {
|
||||
public float add(float number1, float number2) {
|
||||
return number1 + number2;
|
||||
}
|
||||
|
||||
public float subtract(float number1, float number2) {
|
||||
return number1 - number2;
|
||||
}
|
||||
|
||||
public float multiply(float number1, float number2) {
|
||||
return number1 * number2;
|
||||
}
|
||||
|
||||
public float divide(float number1, float number2) {
|
||||
if (number2 == 0) {
|
||||
throw new IllegalArgumentException("'number2' cannot be zero");
|
||||
}
|
||||
return number1 / number2;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.baeldung.micronaut.vs.springboot.controller;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||
|
||||
import com.baeldung.micronaut.vs.springboot.CompareApplication;
|
||||
|
||||
@SpringBootTest(classes = CompareApplication.class)
|
||||
@AutoConfigureMockMvc
|
||||
public class ArithmeticControllerUnitTest {
|
||||
@Autowired
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenAdd_thenCorrectAnswerReturned() throws Exception {
|
||||
Float expected = Float.valueOf(10 + 20);
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/sum/10/20")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(expected.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenSubtract_thenCorrectAnswerReturned() throws Exception {
|
||||
Float expected = Float.valueOf(20 - 10);
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/subtract/20/10")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(expected.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenMultiply_thenCorrectAnswerReturned() throws Exception {
|
||||
Float expected = Float.valueOf(20 * 10);
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/multiply/20/10")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(expected.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNumbers_whenDivide_thenCorrectAnswerReturned() throws Exception {
|
||||
Float expected = Float.valueOf(20 / 10);
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/divide/20/10")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(expected.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenMemory_thenMemoryStringReturned() throws Exception {
|
||||
this.mockMvc.perform(MockMvcRequestBuilders.get("/math/memory")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(containsString("Initial:")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user