Skip to content

Develop #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
* created on : 02/Sep/2020
*/
@Aspect
public class LoggableAspect {
public class LoggableMethodExecutionTimeAspect {
private final Logger log;

public LoggableAspect(String loggerName){
public LoggableMethodExecutionTimeAspect(String loggerName){
super();
log = LoggerFactory.getLogger(loggerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @author : vishnu.g
* created on : 02/Sep/2020
*/
@ConfigurationProperties("logging.method.exec")
@ConfigurationProperties("logging.track")
public class LoggingProperties {
private String loggerName = "AuditLogger";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* created on : 03/Sep/2020
*/
@Configuration
@ConditionalOnProperty(name = "logging.api.enabled", havingValue = "true", matchIfMissing = true)
@ConditionalOnProperty(name = "logging.track.count.enable", havingValue = "true", matchIfMissing = true)
public class MethodCallCounterAutoConfigure {

private final LoggingProperties properties;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.vishnu.boot.logging.autoconfigure;

import com.vishnu.service.aop.LoggableAspect;
import com.vishnu.service.aop.LoggableMethodExecutionTimeAspect;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
Expand All @@ -13,18 +13,18 @@
* created on : 02/Sep/2020
*/
@Configuration
@ConditionalOnClass(LoggableAspect.class)
@ConditionalOnClass(LoggableMethodExecutionTimeAspect.class)
@EnableConfigurationProperties(LoggingProperties.class)
public class LoggingAutoconfigure {
public class MethodExecutionTimeAutoConfiguration {

private final LoggingProperties properties;

public LoggingAutoconfigure(LoggingProperties properties) {
public MethodExecutionTimeAutoConfiguration(LoggingProperties properties) {
this.properties = properties;
}

@Bean
public LoggableAspect loggableAspect(){
return new LoggableAspect(properties.getLoggerName());
public LoggableMethodExecutionTimeAspect loggableMethodExecutionTimeAspect(){
return new LoggableMethodExecutionTimeAspect(properties.getLoggerName());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.vishnu.boot.logging.autoconfigure.LoggingAutoconfigure
# Auto configure classes.
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.vishnu.boot.logging.autoconfigure.MethodExecutionTimeAutoConfiguration,\
com.vishnu.boot.logging.autoconfigure.MethodCallCounterAutoConfigure
3 changes: 2 additions & 1 deletion spring-rest-demo/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
logging.api.enabled=true
logging.track.loggerName=MyLogger
logging.track.count.enable=true
36 changes: 36 additions & 0 deletions spring-rest-demo/src/test/java/com/vishnu/ApiControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* spring-rest-demo : com.vishnu
*/
package com.vishnu;

import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;


/**
* spring-rest-demo : com.vishnu
*
* @author vishnu.g
*
* 01-Oct-2020
*/
@WebMvcTest
public class ApiControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
public void shouldReturnDefaultMessage() throws Exception {
this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
.andExpect(content().string(containsString("Hello world !!!")));
}
}
38 changes: 38 additions & 0 deletions spring-rest-demo/src/test/java/com/vishnu/HttpRequestTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* spring-rest-demo : com.vishnu
*/
package com.vishnu;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;

/**
* Test class to have a sanity check, by asserting the behavior of the application.
*
* @author : vishnu.g
* created on : 02/Sep/2020
*/
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {

@LocalServerPort
private int port;


private final TestRestTemplate restTemplate;

public HttpRequestTest(TestRestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Test
public void greetingShouldReturnDefaultMessage() throws Exception {
assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/api",
String.class)).contains("Hello world !!!");
}
}
30 changes: 30 additions & 0 deletions spring-rest-demo/src/test/java/com/vishnu/SmokeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* spring-rest-demo : com.vishnu
*/
package com.vishnu;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.vishnu.controllers.ApiController;

/**
* Simple smoke test class to check the context is creating the controller.
*
* @author : vishnu.g
* created on : 02/Sep/2020
*/
@SpringBootTest
public class SmokeTest {

@Autowired
private ApiController controller;

@Test
public void contextLoads() throws Exception {
assertThat(controller).isNotNull();
}
}