Skip to content

Commit

Permalink
Extract demo-related code to own sub-package #92
Browse files Browse the repository at this point in the history
  • Loading branch information
xonixx committed Apr 5, 2023
1 parent 1a0116f commit 36813e9
Show file tree
Hide file tree
Showing 19 changed files with 61 additions and 76 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/cmlteam/cmltemplate/Application.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.cmlteam.cmltemplate;

import javax.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import javax.annotation.PostConstruct;

@SpringBootApplication
@ComponentScan(basePackages = "com.cmlteam")
public class Application {
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/com/cmlteam/cmltemplate/config/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.cmlteam.cmltemplate.config;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.sql.DataSource;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -16,10 +19,6 @@
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import javax.sql.DataSource;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Configuration
@EnableAutoConfiguration(exclude = {JacksonAutoConfiguration.class})
@ConfigurationPropertiesScan(basePackages = "com.cmlteam")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmlteam.cmltemplate.config;

import javax.sql.DataSource;
import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -9,8 +10,6 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.sql.DataSource;

/** The purpose here is to make Liquibase run AFTER Hibernate's hbm2ddl */
@Configuration
@ConditionalOnProperty("liquibase.manual")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.cmlteam.cmltemplate.controllers;
package com.cmlteam.cmltemplate.demo;

import com.cmlteam.cmltemplate.model.TestWsUser;
import com.cmlteam.cmltemplate.services.SampleService;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -17,9 +15,9 @@
@Slf4j
@RequiredArgsConstructor
@RestController
public class MainController {
public class DemoApi {

private final SampleService sampleService;
private final DemoService demoService;

@ApiOperation(value = "Test GET endpoint")
@GetMapping(value = "test")
Expand All @@ -30,17 +28,17 @@ public String test() {
@GetMapping(value = "testws")
public String testws() {
RestTemplate template = new RestTemplate();
TestWsUser status =
DemoWsUser status =
Objects.requireNonNull(
template.getForObject(
"https://jsonplaceholder.typicode.com/users/1", TestWsUser.class));
"https://jsonplaceholder.typicode.com/users/1", DemoWsUser.class));
return "" + status.getId();
}

@ApiOperation(value = "Show the DB version")
@GetMapping(value = "testdb")
public String testdb() {
return sampleService.getDbVersion();
return demoService.getDbVersion();
}

@ApiOperation(value = "Test POST endpoint")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.cmlteam.cmltemplate.services;
package com.cmlteam.cmltemplate.demo;

import javax.annotation.PostConstruct;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
public class SampleService {
public class DemoService {
private final JdbcTemplate jdbcTemplate;
private final MongoTemplate mongoTemplate;

@Autowired
public SampleService(JdbcTemplate jdbcTemplate, MongoTemplate mongoTemplate) {
public DemoService(JdbcTemplate jdbcTemplate, MongoTemplate mongoTemplate) {
this.jdbcTemplate = jdbcTemplate;
this.mongoTemplate = mongoTemplate;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cmlteam.cmltemplate.model;
package com.cmlteam.cmltemplate.demo;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
Expand All @@ -11,7 +11,7 @@
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class TestWsUser {
public class DemoWsUser {
private int id;
private String name;
private String email;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cmlteam.cmltemplate.controllers.validation;
package com.cmlteam.cmltemplate.demo.validation;

import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cmlteam.cmltemplate.controllers.validation;
package com.cmlteam.cmltemplate.demo.validation;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Positive;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.cmlteam.cmltemplate.controllers.validation;
package com.cmlteam.cmltemplate.demo.validation;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cmlteam.cmltemplate.services;

import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
Expand All @@ -8,8 +9,6 @@
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import jakarta.validation.constraints.NotBlank;

@Component
@ConfigurationProperties(prefix = "s3")
@ConditionalOnProperty({"s3.enabled"})
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/cmlteam/cmltemplate/services/AwsS3Service.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.cmlteam.cmltemplate.services;

import static java.nio.charset.StandardCharsets.UTF_8;

import com.amazonaws.HttpMethod;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
Expand All @@ -10,12 +12,6 @@
import com.amazonaws.services.s3.model.*;
import com.cmlteam.cmltemplate.util.FileName;
import com.cmlteam.util.Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
Expand All @@ -26,8 +22,11 @@
import java.util.Date;
import java.util.Objects;
import java.util.UUID;

import static java.nio.charset.StandardCharsets.UTF_8;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.cmlteam.cmltemplate.services;

import jakarta.mail.internet.MimeMessage;
import java.io.File;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import jakarta.mail.internet.MimeMessage;
import java.io.File;

@Service
@RequiredArgsConstructor
@Slf4j
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.cmlteam.cmltemplate.services;

import com.cmlteam.cmltemplate.config.RabbitConfig;
import javax.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

/** Sample service to demo RabbitMQ. */
@Service
@RequiredArgsConstructor
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/cmlteam/cmltemplate/util/JsonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public final class JsonUtil {
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/cmlteam/cmltemplate/CorsOffTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.cmlteam.cmltemplate;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.cmlteam.cmltemplate.config.GlobalCorsConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -9,10 +13,6 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(SpringExtension.class)
@WebMvcTest()
@ContextConfiguration(classes = {TestController.class, GlobalCorsConfig.class})
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/cmlteam/cmltemplate/CorsOnTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.cmlteam.cmltemplate;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.cmlteam.cmltemplate.config.GlobalCorsConfig;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -9,10 +13,6 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@ExtendWith(SpringExtension.class)
@WebMvcTest(properties = {"cors.enabled=true"})
@ContextConfiguration(classes = {TestController.class, GlobalCorsConfig.class})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.cmlteam.cmltemplate;
package com.cmlteam.cmltemplate.demo;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.cmlteam.cmltemplate.controllers.MainController;
import com.cmlteam.cmltemplate.services.SampleService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -13,21 +19,13 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.text.MatchesPattern.matchesPattern;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@ExtendWith(SpringExtension.class)
@WebMvcTest(MainController.class)
@ContextConfiguration(classes = {SampleService.class, MainController.class})
public class MainControllerTest {
@WebMvcTest(DemoApi.class)
@ContextConfiguration(classes = {DemoService.class, DemoApi.class})
public class DemoApiTest {
@Autowired private MockMvc mockMvc;

@MockBean private SampleService service;
@MockBean private DemoService service;

@BeforeEach
void setup() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.cmlteam.cmltemplate;
package com.cmlteam.cmltemplate.demo.validation;

import static com.cmlteam.cmltemplate.util.JsonUtil.json;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.cmlteam.cmltemplate.config.BadRequestErrorRenderer;
import com.cmlteam.cmltemplate.controllers.MainController;
import com.cmlteam.cmltemplate.controllers.validation.UserApi;
import com.cmlteam.cmltemplate.demo.DemoApi;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -17,7 +16,7 @@
import org.springframework.test.web.servlet.MockMvc;

@ExtendWith(SpringExtension.class)
@WebMvcTest(MainController.class)
@WebMvcTest(DemoApi.class)
@ContextConfiguration(classes = {UserApi.class, BadRequestErrorRenderer.class})
public class BadRequestTests {
@Autowired private MockMvc mockMvc;
Expand Down
11 changes: 5 additions & 6 deletions src/test/java/com/cmlteam/cmltemplate/util/JsonUtilTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.cmlteam.cmltemplate.util;

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

import java.util.List;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

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

class JsonUtilTest {
private static final String INVALID_JSON = "},z";

Expand Down

0 comments on commit 36813e9

Please sign in to comment.