Skip to content

Commit

Permalink
Switch to assertj in MaskingHelper tests. Improve related javadocs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-punko committed Mar 10, 2024
1 parent 76de993 commit 0d15d76
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 42 deletions.
6 changes: 6 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.25.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jasypt</groupId>
Expand Down
10 changes: 6 additions & 4 deletions common/src/main/java/by/andd3dfx/masking/Masked.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package by.andd3dfx.masking;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Marks class to mask its fields marked by {@link by.andd3dfx.masking.MaskedProperty}
* Marks class to mask its fields marked by {@link MaskedProperty}.
* <p>
* Processing performed by {@link MaskingHelper}.
*/
@Target(TYPE)
@Retention(RUNTIME)
Expand Down
10 changes: 6 additions & 4 deletions common/src/main/java/by/andd3dfx/masking/MaskedProperty.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package by.andd3dfx.masking;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Mark class field to be masked.
* See also {@link by.andd3dfx.masking.Masked}
* Class should be marked with {@link Masked} annotation.
* <p>
* Processing performed by {@link MaskingHelper}.
*/
@Target(FIELD)
@Retention(RUNTIME)
Expand Down
13 changes: 7 additions & 6 deletions common/src/main/java/by/andd3dfx/masking/MaskingHelper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package by.andd3dfx.masking;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.Arrays;
Expand All @@ -8,20 +12,17 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.SneakyThrows;

/**
* Helper for masking secure string fields.
* <p>
* Annotate required class with {@link by.andd3dfx.masking.Masked}
* and its fields with {@link by.andd3dfx.masking.MaskedProperty}.
* Annotate required class with {@link Masked} and its fields with {@link MaskedProperty}.
* <p>
* Use {@link MaskedProperty#pattern()} and {@link MaskedProperty#replacement()} when needed.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class MaskingHelper {

private MaskingHelper() {
}

@SneakyThrows
public static <T> T mask(T object) {
if (extractMaskedAnnotation(object).isEmpty()) {
Expand Down
46 changes: 24 additions & 22 deletions common/src/test/java/by/andd3dfx/masking/MaskingHelperTest.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package by.andd3dfx.masking;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

import by.andd3dfx.masking.dto.PortfolioResponse;
import by.andd3dfx.masking.dto.WithoutAnyAnnotation;
import by.andd3dfx.masking.dto.WithoutMasked;
import by.andd3dfx.masking.dto.WithoutMaskedProperty;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.junit.Test;

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

public class MaskingHelperTest {

Expand Down Expand Up @@ -63,18 +63,20 @@ public void mask() {
.integer(345)
.integerList(Arrays.asList(90, 89, 88))
.baseInt(21)
.baseIntArray(new int[] {56, 45})
.baseIntArray(new int[]{56, 45})
.build();

PortfolioResponse result = MaskingHelper.mask(object);

checkPortfolioResponseAsserts(result, "Alien", "123456******5678");
assertThat(result.getStringList(), is(Arrays.asList("one", "two", "1116788912560023")));
assertThat(result.getMaskedStringList(), is(Arrays.asList("***", "***", "***")));
assertThat(result.getStringSet(), is(Set.of("Axton", "Maya", "1116788912567823")));
assertThat(result.getMaskedStringSet(), is(Set.of("Axton", "Brick", "121678-ZZZ-7823")));
assertThat(result.getStringToStringMap(), is(Map.of("Bob", "1116788912560099", "Mary", "1116788912560088")));
assertThat(result.getMaskedStringToStringMap(), is(Map.of("Bob", "111678-XYZ-0077", "Mary", "111678-XYZ-0066")));
assertThat(result.getStringList()).isEqualTo(Arrays.asList("one", "two", "1116788912560023"));
assertThat(result.getMaskedStringList()).isEqualTo(Arrays.asList("***", "***", "***"));
assertThat(result.getStringSet()).isEqualTo(Set.of("Axton", "Maya", "1116788912567823"));
assertThat(result.getMaskedStringSet()).isEqualTo(Set.of("Axton", "Brick", "121678-ZZZ-7823"));
assertThat(result.getStringToStringMap()).isEqualTo(
Map.of("Bob", "1116788912560099", "Mary", "1116788912560088"));
assertThat(result.getMaskedStringToStringMap()).isEqualTo(
Map.of("Bob", "111678-XYZ-0077", "Mary", "111678-XYZ-0066"));

checkPortfolioResponseAsserts(result.getPortfolioResponse(),
"inner str field", "455678******7823");
Expand All @@ -91,16 +93,16 @@ public void mask() {
checkPortfolioResponseAsserts(result.getStringToPortfolioResponseMap().get("K2"),
"K2 str field", "458978******8978");

assertThat(result.getInteger(), is(345));
assertThat(result.getIntegerList(), is(Arrays.asList(90, 89, 88)));
assertThat(result.getBaseInt(), is(21));
assertThat(result.getBaseIntArray(), is(new int[] {56, 45}));
assertThat(result.getInteger()).isEqualTo(345);
assertThat(result.getIntegerList()).isEqualTo(Arrays.asList(90, 89, 88));
assertThat(result.getBaseInt()).isEqualTo(21);
assertThat(result.getBaseIntArray()).isEqualTo(new int[]{56, 45});
}

private void checkPortfolioResponseAsserts(PortfolioResponse innerPortfolioResponse, String string, String maskedStringWithPattern) {
assertThat(innerPortfolioResponse.getString(), is(string));
assertThat(innerPortfolioResponse.getMaskedString(), is("***"));
assertThat(innerPortfolioResponse.getMaskedStringWithPattern(), is(maskedStringWithPattern));
assertThat(innerPortfolioResponse.getString()).isEqualTo(string);
assertThat(innerPortfolioResponse.getMaskedString()).isEqualTo("***");
assertThat(innerPortfolioResponse.getMaskedStringWithPattern()).isEqualTo(maskedStringWithPattern);
}

@Test
Expand All @@ -111,7 +113,7 @@ public void maskForClassWithoutAnnotations() {

WithoutAnyAnnotation result = MaskingHelper.mask(object);

assertThat(result.getString(), is("Alien"));
assertThat(result.getString()).isEqualTo("Alien");
}

@Test
Expand All @@ -123,8 +125,8 @@ public void maskForClassWithoutMaskedAnnotation() {

WithoutMasked result = MaskingHelper.mask(object);

assertThat(result.getString(), is("Alien"));
assertThat(result.getMaskedString(), is("Very Important Info"));
assertThat(result.getString()).isEqualTo("Alien");
assertThat(result.getMaskedString()).isEqualTo("Very Important Info");
}

@Test
Expand All @@ -135,6 +137,6 @@ public void maskForClassWithoutMaskedPropertyAnnotation() {

WithoutMaskedProperty result = MaskingHelper.mask(object);

assertThat(result.getString(), is("Very Important Info"));
assertThat(result.getString()).isEqualTo("Very Important Info");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package by.andd3dfx.masking.dto;

import java.util.List;
import java.util.Map;
import java.util.Set;
import by.andd3dfx.masking.Masked;
import by.andd3dfx.masking.MaskedProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import by.andd3dfx.masking.Masked;
import by.andd3dfx.masking.MaskedProperty;

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

@Builder
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package by.andd3dfx.masking.dto;

import by.andd3dfx.masking.MaskedProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import by.andd3dfx.masking.MaskedProperty;

@Builder
@AllArgsConstructor
Expand Down

0 comments on commit 0d15d76

Please sign in to comment.