diff --git a/spring-aop/src/main/java/org/baeldung/logger/AdderAfterAspect.java b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterAspect.java new file mode 100644 index 0000000000..59afa38f06 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterAspect.java @@ -0,0 +1,13 @@ +package org.baeldung.logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AdderAfterAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void afterAdvice() throws Throwable { + logger.info("I'm done calling the method"); + } +} diff --git a/spring-aop/src/main/java/org/baeldung/logger/AdderAfterReturnAspect.java b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterReturnAspect.java new file mode 100644 index 0000000000..a2b1959374 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterReturnAspect.java @@ -0,0 +1,13 @@ +package org.baeldung.logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AdderAfterReturnAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void afterReturn(final Object returnValue) throws Throwable { + logger.info("value return was {}", returnValue); + } +} diff --git a/spring-aop/src/main/java/org/baeldung/logger/AdderAfterThrowAspect.java b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterThrowAspect.java new file mode 100644 index 0000000000..1f19af05e4 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/AdderAfterThrowAspect.java @@ -0,0 +1,13 @@ +package org.baeldung.logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AdderAfterThrowAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void afterThrow(final Exception exception) throws Throwable { + logger.info("Exception thrown was {}", exception.getMessage()); + } +} diff --git a/spring-aop/src/main/java/org/baeldung/logger/AdderAroundAspect.java b/spring-aop/src/main/java/org/baeldung/logger/AdderAroundAspect.java new file mode 100644 index 0000000000..fceba87d15 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/AdderAroundAspect.java @@ -0,0 +1,18 @@ +package org.baeldung.logger; + +import java.util.Arrays; +import org.aspectj.lang.ProceedingJoinPoint; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AdderAroundAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public Object aroundAdvice(final ProceedingJoinPoint joinPoint) throws Throwable { + logger.info("Arguments passed to method are: " + Arrays.toString(joinPoint.getArgs())); + final Object result = joinPoint.proceed(); + logger.info("Result from method is: " + result); + return result; + } +} diff --git a/spring-aop/src/main/java/org/baeldung/logger/AdderBeforeAspect.java b/spring-aop/src/main/java/org/baeldung/logger/AdderBeforeAspect.java new file mode 100644 index 0000000000..750e7ba122 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/AdderBeforeAspect.java @@ -0,0 +1,13 @@ +package org.baeldung.logger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class AdderBeforeAspect { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + + public void beforeAdvice() throws Throwable { + logger.info("I would be executed just before method starts"); + } +} diff --git a/spring-aop/src/main/java/org/baeldung/logger/SampleAdder.java b/spring-aop/src/main/java/org/baeldung/logger/SampleAdder.java new file mode 100644 index 0000000000..96fda1b0b9 --- /dev/null +++ b/spring-aop/src/main/java/org/baeldung/logger/SampleAdder.java @@ -0,0 +1,12 @@ +package org.baeldung.logger; + +public class SampleAdder { + + public int add(int a, int b) { + if (a < 0 || b < 0) { + throw new IllegalArgumentException("Make sure all the arguments are greater than zero."); + } + return a + b; + } + +} diff --git a/spring-aop/src/main/resources/org.baeldung.logger/springAop-applicationContext.xml b/spring-aop/src/main/resources/org.baeldung.logger/springAop-applicationContext.xml new file mode 100644 index 0000000000..058beaa7a1 --- /dev/null +++ b/spring-aop/src/main/resources/org.baeldung.logger/springAop-applicationContext.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-aop/src/test/java/org/baeldung/logger/CalculatorTest.java b/spring-aop/src/test/java/org/baeldung/logger/CalculatorTest.java new file mode 100644 index 0000000000..b1c88c67df --- /dev/null +++ b/spring-aop/src/test/java/org/baeldung/logger/CalculatorTest.java @@ -0,0 +1,29 @@ +package org.baeldung.logger; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(value = {"classpath:springAop-applicationContext.xml"}) +public class CalculatorTest { + + @Autowired + private SampleAdder sampleAdder; + + @Test + public void whenAddValidValues_returnsSucessfully() { + final int addedValue = sampleAdder.add(12, 12); + + assertEquals(24, addedValue); + } + + @Test (expected = IllegalArgumentException.class) + public void whenAddInValidValues_throwsException() { + sampleAdder.add(12, -12); + } + +} diff --git a/spring-aop/src/test/resources/springAop-applicationContext.xml b/spring-aop/src/test/resources/springAop-applicationContext.xml new file mode 100644 index 0000000000..4d88bd3711 --- /dev/null +++ b/spring-aop/src/test/resources/springAop-applicationContext.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-actuator/.gitignore b/spring-boot-actuator/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/spring-boot-actuator/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/spring-boot-actuator/README.MD b/spring-boot-actuator/README.MD new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-actuator/pom.xml b/spring-boot-actuator/pom.xml new file mode 100644 index 0000000000..bec126a247 --- /dev/null +++ b/spring-boot-actuator/pom.xml @@ -0,0 +1,121 @@ + + 4.0.0 + com.baeldung + spring-boot-actuator + 0.0.1-SNAPSHOT + jar + spring-boot + This is simple boot application for Spring boot actuator test + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.2.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + spring-boot-actuator + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + org.baeldung.MainApplication + UTF-8 + 1.8 + + + diff --git a/spring-boot-actuator/src/main/java/org/baeldung/MainApplication.java b/spring-boot-actuator/src/main/java/org/baeldung/MainApplication.java new file mode 100644 index 0000000000..7c9054dbf8 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/MainApplication.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.baeldung.config.MainConfig; + +@SpringBootApplication +public class MainApplication { + + public static void main(String args[]) { + SpringApplication.run(MainConfig.class, args); + } +} diff --git a/spring-boot-actuator/src/main/java/org/baeldung/config/MainConfig.java b/spring-boot-actuator/src/main/java/org/baeldung/config/MainConfig.java new file mode 100644 index 0000000000..27c97cc006 --- /dev/null +++ b/spring-boot-actuator/src/main/java/org/baeldung/config/MainConfig.java @@ -0,0 +1,17 @@ +package org.baeldung.config; + +import java.util.Collections; +import org.springframework.boot.actuate.info.InfoContributor; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; + +@EnableAutoConfiguration +public class MainConfig { + + public MainConfig() {} + + @Bean + public InfoContributor getInfoContributor() { + return (infoBuilder) -> infoBuilder.withDetail("applicationInfo", Collections.singletonMap("ActiveUserCount", "10")); + } +} diff --git a/spring-boot-actuator/src/main/resources/application.properties b/spring-boot-actuator/src/main/resources/application.properties new file mode 100644 index 0000000000..835c78eda2 --- /dev/null +++ b/spring-boot-actuator/src/main/resources/application.properties @@ -0,0 +1 @@ +info.app.name=Sample application \ No newline at end of file diff --git a/spring-boot-actuator/src/test/java/org/baeldung/config/ActuatorInfoIntegrationTest.java b/spring-boot-actuator/src/test/java/org/baeldung/config/ActuatorInfoIntegrationTest.java new file mode 100644 index 0000000000..0b5e3b3eef --- /dev/null +++ b/spring-boot-actuator/src/test/java/org/baeldung/config/ActuatorInfoIntegrationTest.java @@ -0,0 +1,32 @@ +package org.baeldung.config; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import static org.junit.Assert.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = MainConfig.class) +@TestPropertySource(properties = { "security.basic.enabled=false" }) +public class ActuatorInfoIntegrationTest { + + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void whenGetInfo_thenAdditionalInfoReturned() throws IOException { + final String expectedResponse = new String(Files.readAllBytes(Paths.get("src/test/resources/expectedResponse.json"))); + final ResponseEntity responseEntity = this.restTemplate.getForEntity("/info", String.class); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals(expectedResponse, responseEntity.getBody()); + } +} \ No newline at end of file diff --git a/spring-boot-actuator/src/test/resources/expectedResponse.json b/spring-boot-actuator/src/test/resources/expectedResponse.json new file mode 100644 index 0000000000..caa0bdbbf8 --- /dev/null +++ b/spring-boot-actuator/src/test/resources/expectedResponse.json @@ -0,0 +1 @@ +{"app":{"name":"Sample application"},"applicationInfo":{"ActiveUserCount":"10"}} \ No newline at end of file