Skip to content
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

Completed TODO steps and tests passed. #23

Open
wants to merge 1 commit into
base: feature/aspect-oriented-programming
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
@@ -1,12 +1,15 @@
package com.trendyol.bootcamp.spring.ch05.aspect;

import com.trendyol.bootcamp.spring.ch05.exception.RewardDataAccessException;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;


@Aspect
@Aspect
@Component
public class DBExceptionHandlingAspect {

public static final String EMAIL_FAILURE_MSG = "Failed sending an email to Mister Smith : ";
Expand All @@ -19,7 +22,10 @@ public class DBExceptionHandlingAspect {
// - Configure this advice method to enable logging of
// exceptions thrown by Repository class methods.
// - Select the advice type that seems most appropriate.

@AfterThrowing(
pointcut = "execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.*(..))",
throwing = "e"
)
public void implExceptionHandling(RewardDataAccessException e) {
// Log a failure warning
logger.warn(EMAIL_FAILURE_MSG + e + "\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

// TODO-02: Use AOP to log a message before
// any repository's find...() method is invoked.
Expand All @@ -15,7 +19,8 @@
// - Optionally place @Autowired annotation on the constructor
// where `MonitorFactory` dependency is being injected.
// (It is optional since there is only a single constructor in the class.)

@Aspect
@Component
public class LoggingAspect {
public final static String BEFORE = "'Before'";
public final static String AROUND = "'Around'";
Expand All @@ -34,7 +39,7 @@ public LoggingAspect(MonitorFactory monitorFactory) {
// - Decide which advice type is most appropriate
// - Write a pointcut expression that selects only find* methods on
// our repository classes.

@Before("execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.find*(..))")
public void implLogging(JoinPoint joinPoint) {
// Do not modify this log message or the test will fail
logger.info(BEFORE + " advice implementation - " + joinPoint.getTarget().getClass() + //
Expand All @@ -47,18 +52,17 @@ public void implLogging(JoinPoint joinPoint) {
// - Mark this method as an around advice.
// - Write a pointcut expression to match on all update* methods
// on all Repository classes.

@Around("execution(* com.trendyol.bootcamp.spring.ch05.repository.*.*Repository.update*(..))")
public Object monitor(ProceedingJoinPoint repositoryMethod) throws Throwable {
String name = createJoinPointTraceName(repositoryMethod);
Monitor monitor = monitorFactory.start(name);
try {
// Invoke repository method ...

return repositoryMethod.proceed();
// TODO-08: Add the logic to proceed with the target method invocation.
// - Be sure to return the target method's return value to the caller
// and delete the line below.

return new String("Delete this line after completing TODO-08");

} finally {
monitor.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import com.trendyol.bootcamp.spring.ch05.monitor.jamon.JamonMonitorFactory;
import com.trendyol.bootcamp.spring.ch05.monitor.MonitorFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

// TODO-04: Update Aspect related configuration
// - Add a class-level annotation to scan for components
Expand All @@ -14,6 +16,8 @@
// application since it will be automatically added through
// auto configuration.)
@Configuration
@ComponentScan("com.trendyol.bootcamp.spring.ch05.aspect")
@EnableAspectJAutoProxy
public class AspectsConfig {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.trendyol.bootcamp.spring.ch05;

import com.trendyol.bootcamp.spring.ch05.config.AspectsConfig;
import com.trendyol.bootcamp.spring.ch05.config.RewardsConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -15,7 +16,7 @@
* and you should see one line of LoggingAspect output in the console.
*/
@Configuration
@Import({RewardsConfig.class})
@Import({RewardsConfig.class, AspectsConfig.class})
public class SystemTestConfig {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ public class TestConstants {
// TODO-01: Enable checking of console output in our Tests.
// - Change the value below to true

public static final boolean CHECK_CONSOLE_OUTPUT = false;
public static final boolean CHECK_CONSOLE_OUTPUT = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void testRewardForDining(CaptureSystemOutput.OutputCapture capture) {

// TODO-06: Run this test. It should pass AND you should see TWO lines of
// log output from the LoggingAspect on the console
int expectedMatches = 2;
int expectedMatches = 4;
checkConsoleOutput(capture, expectedMatches);

// TODO-09: Save all your work, and change the expected matches value above from 2 to 4.
Expand Down