-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mehmet.ari
committed
Mar 7, 2022
1 parent
7424771
commit 606e16a
Showing
23 changed files
with
493 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...potent-core/src/main/java/com/trendyol/jdempotent/core/annotation/JdempotentProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.trendyol.jdempotent.core.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* | ||
* That annotation needs to get the field name differently and generate the hash | ||
* | ||
*/ | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target(ElementType.FIELD) | ||
public @interface JdempotentProperty { | ||
String value() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/chain/AnnotationChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.trendyol.jdempotent.core.chain; | ||
|
||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
|
||
public abstract class AnnotationChain { | ||
protected AnnotationChain nextChain; | ||
|
||
public abstract KeyValuePair process(ChainData chainData) throws IllegalAccessException; | ||
|
||
public void next(AnnotationChain nextChain) { | ||
this.nextChain = nextChain; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/chain/JdempotentDefaultChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.trendyol.jdempotent.core.chain; | ||
|
||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class JdempotentDefaultChain extends AnnotationChain { | ||
|
||
@Override | ||
public KeyValuePair process(ChainData chainData) throws IllegalAccessException { | ||
Field declaredField = chainData.getDeclaredField(); | ||
declaredField.setAccessible(true); | ||
return new KeyValuePair(declaredField.getName(),declaredField.get(chainData.getArgs())); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ore/src/main/java/com/trendyol/jdempotent/core/chain/JdempotentIgnoreAnnotationChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.trendyol.jdempotent.core.chain; | ||
|
||
import com.trendyol.jdempotent.core.annotation.JdempotentIgnore; | ||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class JdempotentIgnoreAnnotationChain extends AnnotationChain { | ||
@Override | ||
public KeyValuePair process(ChainData chainData) throws IllegalAccessException { | ||
Field declaredField = chainData.getDeclaredField(); | ||
declaredField.setAccessible(true); | ||
JdempotentIgnore annotation = declaredField.getAnnotation(JdempotentIgnore.class); | ||
if(annotation != null){ | ||
return new KeyValuePair(); | ||
} | ||
return super.nextChain.process(chainData); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...nt-core/src/main/java/com/trendyol/jdempotent/core/chain/JdempotentNoAnnotationChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.trendyol.jdempotent.core.chain; | ||
|
||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class JdempotentNoAnnotationChain extends AnnotationChain { | ||
|
||
@Override | ||
public KeyValuePair process(ChainData chainData) throws IllegalAccessException { | ||
if (chainData.getDeclaredField().getDeclaredAnnotations().length == 0) { | ||
Field declaredField = chainData.getDeclaredField(); | ||
declaredField.setAccessible(true); | ||
return new KeyValuePair(declaredField.getName(),declaredField.get(chainData.getArgs())); | ||
} | ||
return super.nextChain.process(chainData); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...e/src/main/java/com/trendyol/jdempotent/core/chain/JdempotentPropertyAnnotationChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.trendyol.jdempotent.core.chain; | ||
|
||
import com.trendyol.jdempotent.core.annotation.JdempotentProperty; | ||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class JdempotentPropertyAnnotationChain extends AnnotationChain { | ||
|
||
@Override | ||
public KeyValuePair process(ChainData chainData) throws IllegalAccessException { | ||
Field declaredField = chainData.getDeclaredField(); | ||
declaredField.setAccessible(true); | ||
JdempotentProperty annotation = declaredField.getAnnotation(JdempotentProperty.class); | ||
if(annotation != null){ | ||
return new KeyValuePair(annotation.value(),declaredField.get(chainData.getArgs())); | ||
} | ||
return super.nextChain.process(chainData); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/model/ChainData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.trendyol.jdempotent.core.model; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
public class ChainData { | ||
private Field declaredField; | ||
private Object args; | ||
|
||
public ChainData(){ | ||
} | ||
|
||
public ChainData(Field declaredField, Object args) { | ||
this.declaredField = declaredField; | ||
this.args = args; | ||
} | ||
|
||
public Field getDeclaredField() { | ||
return declaredField; | ||
} | ||
|
||
public void setDeclaredField(Field declaredField) { | ||
this.declaredField = declaredField; | ||
} | ||
|
||
public Object getArgs() { | ||
return args; | ||
} | ||
|
||
public void setArgs(Object args) { | ||
this.args = args; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/model/KeyValuePair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.trendyol.jdempotent.core.model; | ||
|
||
public class KeyValuePair { | ||
private String key; | ||
private Object value; | ||
|
||
public KeyValuePair(){} | ||
|
||
public KeyValuePair(String key, Object value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public void setKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public Object getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Jdempotent-core/src/test/java/aspect/chain/JdempotentDefaultChainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package aspect.chain; | ||
|
||
import aspect.core.IdempotentTestPayload; | ||
import com.trendyol.jdempotent.core.chain.JdempotentDefaultChain; | ||
import com.trendyol.jdempotent.core.model.ChainData; | ||
import com.trendyol.jdempotent.core.model.KeyValuePair; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
@RunWith(SpringRunner.class) | ||
public class JdempotentDefaultChainTest { | ||
|
||
@InjectMocks | ||
private JdempotentDefaultChain jdempotentDefaultChain; | ||
|
||
@Test | ||
public void should_process_with_no_annotation() throws IllegalAccessException, NoSuchFieldException { | ||
//Given | ||
IdempotentTestPayload idempotentTestPayload = new IdempotentTestPayload(); | ||
idempotentTestPayload.setName("value"); | ||
ChainData chainData = new ChainData(); | ||
chainData.setArgs(idempotentTestPayload); | ||
chainData.setDeclaredField(idempotentTestPayload.getClass().getDeclaredField("name")); | ||
|
||
//When | ||
KeyValuePair process = jdempotentDefaultChain.process(chainData); | ||
|
||
//Then | ||
assertEquals("name", process.getKey()); | ||
assertEquals("value", process.getValue()); | ||
} | ||
|
||
@Test | ||
public void should_process_with_another_annotated_property() throws IllegalAccessException, NoSuchFieldException { | ||
//Given | ||
IdempotentTestPayload idempotentTestPayload = new IdempotentTestPayload(); | ||
idempotentTestPayload.setEventId(1l); | ||
ChainData chainData = new ChainData(); | ||
chainData.setArgs(idempotentTestPayload); | ||
chainData.setDeclaredField(idempotentTestPayload.getClass().getDeclaredField("eventId")); | ||
|
||
//When | ||
KeyValuePair process = jdempotentDefaultChain.process(chainData); | ||
|
||
//Then | ||
assertEquals("eventId", process.getKey()); | ||
assertEquals(1l, process.getValue()); | ||
} | ||
} |
Oops, something went wrong.