Skip to content

Commit

Permalink
Changing from code smells
Browse files Browse the repository at this point in the history
  • Loading branch information
andresguedes committed Nov 12, 2024
1 parent 1a75637 commit 98eb816
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

public class TrustlyJsInterfaceTest {

private static final String EVENT_NAME = "event";

@Mock
private TrustlyView mockTrustlyView;

Expand All @@ -45,97 +47,97 @@ public void shouldValidateTrustlyJsInterfaceInstanceIsNotNull() {
public void shouldValidateTrustlyJsInterfacePostMessageWithoutTrustlyViewInstance() {
trustlyJsInterface = new TrustlyJsInterface(null);
trustlyJsInterface.postMessage("PayWithMyBank.event|event");
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutMessage() {
trustlyJsInterface.postMessage(null);
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageNullCommand() {
trustlyJsInterface.postMessage("|event");
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageNullCommandAndNullEvent() {
trustlyJsInterface.postMessage("|");
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithEmptyMessage() {
trustlyJsInterface.postMessage("");
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithNoValidEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.eventNotValid|event");
verify(mockTrustlyView, times(0)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(0)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithValidEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event");
verify(mockTrustlyView, times(1)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithNullEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|null");
verify(mockTrustlyView, times(1)).notifyListener("event", new HashMap<>());
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithNoPassedEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|");
verify(mockTrustlyView).notifyListener("event", new HashMap<>());
verify(mockTrustlyView).notifyListener(EVENT_NAME, new HashMap<>());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithAllValidEvents() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com|123456|47d7-89d3-9628d4cfb65e|bank_selected|100021|123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllEventNames());
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllEventNames());
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutPageEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event||123456|47d7-89d3-9628d4cfb65e|bank_selected|100021|123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("page"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("page"));
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutTransactionIdEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com||47d7-89d3-9628d4cfb65e|bank_selected|100021|123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("transactionId"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("transactionId"));
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutMerchantReferenceEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com|123456||bank_selected|100021|123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("merchantReference"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("merchantReference"));
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutTypeEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com|123456|47d7-89d3-9628d4cfb65e||100021|123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("type"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("type"));
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutDataEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com|123456|47d7-89d3-9628d4cfb65e|bank_selected||123");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("data"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("data"));
}

@Test
public void shouldValidateTrustlyJsInterfacePostMessageWithoutTransferEvent() {
trustlyJsInterface.postMessage("PayWithMyBank.event|event|http://www.url.com|123456|47d7-89d3-9628d4cfb65e|bank_selected|100021|");
verify(mockTrustlyView, times(1)).notifyListener("event", getAllOtherEventNames("transfer"));
verify(mockTrustlyView, times(1)).notifyListener(EVENT_NAME, getAllOtherEventNames("transfer"));
}

@Test
Expand All @@ -148,14 +150,14 @@ public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsWithAllNullValue
@Test
public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsInvalidParamsValue() {
HashMap<String, String> eventDetails = new HashMap<>();
trustlyJsInterface.addToListenerDetails(null, 1, "event", eventDetails);
trustlyJsInterface.addToListenerDetails(null, 1, EVENT_NAME, eventDetails);
assertTrue(eventDetails.isEmpty());
}

@Test
public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsInvalidIndexValue() {
HashMap<String, String> eventDetails = new HashMap<>();
trustlyJsInterface.addToListenerDetails(new String[] {}, 10, "event", eventDetails);
trustlyJsInterface.addToListenerDetails(new String[] {}, 10, EVENT_NAME, eventDetails);
assertTrue(eventDetails.isEmpty());
}

Expand All @@ -176,14 +178,14 @@ public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsInvalidEventDeta
@Test
public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsInvalidParamsWithNullValue() {
HashMap<String, String> eventDetails = new HashMap<>();
trustlyJsInterface.addToListenerDetails(new String[] { null }, 0, "event", eventDetails);
trustlyJsInterface.addToListenerDetails(new String[] { null }, 0, EVENT_NAME, eventDetails);
assertTrue(eventDetails.isEmpty());
}

@Test
public void shouldValidateTrustlyJsInterfaceAddToListenerDetailsInvalidParamsWithNullStringValue() {
HashMap<String, String> eventDetails = new HashMap<>();
trustlyJsInterface.addToListenerDetails(new String[] { "null" }, 0, "event", eventDetails);
trustlyJsInterface.addToListenerDetails(new String[] { "null" }, 0, EVENT_NAME, eventDetails);
assertTrue(eventDetails.isEmpty());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.trustly.android.sdk.interfaces;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -59,12 +58,6 @@ public void shouldThrowExceptionWhenTrustlyInstanceIsCalled() {
assertThrows(NullPointerException.class, () -> Trustly.Instance.create(mockContext));
}

@Test
public void shouldValidateTrustlyInstance() {
Trustly.Instance instance = new Trustly.Instance();
assertNotNull(instance);
}

@Test
public void shouldValidateTrustlySelectBankWidgetWhenIsCalled() {
when(mockTrustly.selectBankWidget(anyMap())).thenReturn(mockTrustly);
Expand Down Expand Up @@ -117,8 +110,7 @@ public void shouldValidateTrustlyHybridWhenIsCalled() {
when(mockTrustly.hybrid(any(), any(), any())).thenReturn(mockTrustly);
Trustly result = trustlyImpl.callHybrid(URL, RETURN_URL,
CANCEL_URL);
verify(mockTrustly, times(1)).hybrid("http://www.url.com",
"http://www.url.com/return", "http://www.url.com/cancel");
verify(mockTrustly, times(1)).hybrid(URL, RETURN_URL, CANCEL_URL);
assertSame(mockTrustly, result);
}

Expand All @@ -127,8 +119,7 @@ public void shouldValidateTrustlyHybridWithoutReturnUrlWhenIsCalled() {
when(mockTrustly.hybrid(any(), any(), any())).thenReturn(mockTrustly);
Trustly result = trustlyImpl.callHybrid(URL, null,
CANCEL_URL);
verify(mockTrustly, times(1)).hybrid("http://www.url.com",
null, "http://www.url.com/cancel");
verify(mockTrustly, times(1)).hybrid(URL, null, CANCEL_URL);
assertSame(mockTrustly, result);
}

Expand All @@ -137,8 +128,7 @@ public void shouldValidateTrustlyHybridWithoutCancelUrlWhenIsCalled() {
when(mockTrustly.hybrid(any(), any(), any())).thenReturn(mockTrustly);
Trustly result = trustlyImpl.callHybrid(URL, RETURN_URL,
null);
verify(mockTrustly, times(1)).hybrid("http://www.url.com",
"http://www.url.com/return", null);
verify(mockTrustly, times(1)).hybrid(URL, RETURN_URL, null);
assertSame(mockTrustly, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public class CidManagerTest {
private static final String CID_PARAM = "CID";
private static final String CID_STORAGE = "CID_STORAGE";
private static final String SESSION_CID = "SESSION_CID";
private static final String ANDROID_ID = "android_id";
private static final String ANDROID_ID_VALUE = "1234-4CD5-0";
private static final String CID_VALUE_1 = "1234-4A94-0";
private static final String SESSION_CID_VALUE_1 = "1234-4A94-";
private static final String SESSION_CID_VALUE_2 = "1234-4A93-";

@Mock
private SharedPreferences.Editor mockSharedPreferencesEditor;
Expand Down Expand Up @@ -80,11 +85,11 @@ public void shouldValidateCidManagerGenerateCIDMethod() {

mockStatic(UUID.class, CALLS_REAL_METHODS).when(UUID::randomUUID).thenReturn(mockUUID);
mockStatic(Calendar.class, CALLS_REAL_METHODS).when(Calendar::getInstance).thenReturn(mockCalendar);
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), "android_id")).thenReturn("1234-4CD5-0");
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), ANDROID_ID)).thenReturn(ANDROID_ID_VALUE);

CidManager.generateCid(mockContext);

verify(mockSharedPreferencesEditor, times(1)).putString("CID", "1234-4A94-0");
verify(mockSharedPreferencesEditor, times(1)).putString("CID", CID_VALUE_1);
verify(mockSharedPreferencesEditor, times(1)).apply();
verify(mockSharedPreferences, times(1)).edit();
}
Expand All @@ -93,14 +98,14 @@ public void shouldValidateCidManagerGenerateCIDMethod() {
public void shouldValidateCidManagerGetOrCreateSessionCidMethod() {
String timeStampNow = Long.toString(Calendar.getInstance().getTimeInMillis(), 36);

when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn("1234-4A94-0");
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn("1234-4A93-" + timeStampNow);
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn(CID_VALUE_1);
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn(SESSION_CID_VALUE_2 + timeStampNow);

mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), "android_id")).thenReturn("1234-4CD5-0");
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), ANDROID_ID)).thenReturn(ANDROID_ID_VALUE);

Map<String, String> result = CidManager.getOrCreateSessionCid(mockContext);

verify(mockSharedPreferencesEditor, times(1)).putString("SESSION_CID", "1234-4A93-" + timeStampNow);
verify(mockSharedPreferencesEditor, times(1)).putString(SESSION_CID, SESSION_CID_VALUE_2 + timeStampNow);
verify(mockSharedPreferences, times(1)).edit();
assertNotEquals(getCIDParams(), result);
}
Expand All @@ -113,16 +118,16 @@ public void shouldValidateCidManagerGetOrCreateSessionCidMoreThanOneHourMethod()

String timeStampNow = Long.toString(mockCalendar.getTimeInMillis(), 36);

when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn("1234-4A94-0");
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn("1234-4A94-" + timeStampNow);
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn(CID_VALUE_1);
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn(SESSION_CID_VALUE_1 + timeStampNow);

mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), "android_id")).thenReturn("1234-4CD5-0");
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), ANDROID_ID)).thenReturn(ANDROID_ID_VALUE);

Map<String, String> result = CidManager.getOrCreateSessionCid(mockContext);

verify(mockSharedPreferencesEditor, times(1)).putString("SESSION_CID", "1234-4A94-" + timeStampNow);
verify(mockSharedPreferencesEditor, times(1)).putString(SESSION_CID, SESSION_CID_VALUE_1 + timeStampNow);
verify(mockSharedPreferences, times(1)).edit();
assertEquals("1234-4A94-0", result.get("SESSION_CID"));
assertEquals(CID_VALUE_1, result.get(SESSION_CID));
}

@Test
Expand All @@ -131,36 +136,36 @@ public void shouldValidateCidManagerGetOrCreateSessionCidNullValueMethod() {
instance.add(Calendar.DATE, 2);
mockCalendar.setTime(instance.getTime());

when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn("1234-4A94-0");
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn("1234-4A94-");
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn(CID_VALUE_1);
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn(SESSION_CID_VALUE_1);

mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), "android_id")).thenReturn("1234-4CD5-0");
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), ANDROID_ID)).thenReturn(ANDROID_ID_VALUE);

Map<String, String> result = CidManager.getOrCreateSessionCid(mockContext);

verify(mockSharedPreferencesEditor, times(1)).putString("SESSION_CID", "1234-4A94-");
verify(mockSharedPreferencesEditor, times(1)).putString(SESSION_CID, SESSION_CID_VALUE_1);
verify(mockSharedPreferences, times(1)).edit();
assertEquals("1234-4A94-", result.get("SESSION_CID"));
assertEquals(SESSION_CID_VALUE_1, result.get(SESSION_CID));
}

@Test
public void shouldValidateCidManagerGetOrCreateSessionCidInvalidValueMethod() {
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn("1234-4A94-0");
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn("1234-4A94-0");
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn(CID_VALUE_1);
when(CidStorage.readDataFrom(mockContext, SESSION_CID)).thenReturn(CID_VALUE_1);

mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), "android_id")).thenReturn("1234-4CD5-0");
mockSettingsSecure.when(() -> Settings.Secure.getString(mockContext.getContentResolver(), ANDROID_ID)).thenReturn(ANDROID_ID_VALUE);

Map<String, String> result = CidManager.getOrCreateSessionCid(mockContext);

verify(mockSharedPreferencesEditor, times(0)).putString("SESSION_CID", "1234-4A94-0");
verify(mockSharedPreferencesEditor, times(0)).putString(SESSION_CID, CID_VALUE_1);
verify(mockSharedPreferences, times(1)).edit();
assertNotEquals("1234-4A94-0", result.get("SESSION_CID"));
assertNotEquals(CID_VALUE_1, result.get(SESSION_CID));
}

@Test
public void shouldValidateCidManagerGetOrCreateSessionCidReadNullSessionCidMethod() {
when(mockSharedPreferences.getString(anyString(), eq(null))).thenReturn(null);
when(CidStorage.readDataFrom(mockContext, CID_PARAM)).thenReturn("1234-4A94-0");
when(CidStorage.readDataFrom(mockContext, CID_PARAM)).thenReturn(CID_VALUE_1);

Map<String, String> result = CidManager.getOrCreateSessionCid(mockContext);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void shouldValidateCidStorageSaveDataMethod() {

CidStorage.saveData(mockContext, SESSION_CID, SESSION_CID_VALUE);

verify(mockSharedPreferencesEditor, times(1)).putString("SESSION_CID", "1724258668");
verify(mockSharedPreferencesEditor, times(1)).putString(SESSION_CID, SESSION_CID_VALUE);
verify(mockSharedPreferencesEditor, times(1)).apply();
verify(mockSharedPreferences, times(1)).edit();
}
Expand All @@ -70,9 +70,9 @@ public void shouldValidateCidStorageReadDataFromMethodWithResult() {

String result = CidStorage.readDataFrom(mockContext, SESSION_CID);

verify(mockSharedPreferences, times(1)).getString("SESSION_CID", null);
verify(mockSharedPreferences, times(1)).getString(SESSION_CID, null);
verify(mockSharedPreferences, times(0)).edit();
assertEquals("1724258668", result);
assertEquals(SESSION_CID_VALUE, result);
}

@Test
Expand All @@ -81,7 +81,7 @@ public void shouldValidateCidStorageReadDataFromMethodWithNullResult() {

String result = CidStorage.readDataFrom(mockContext, SESSION_CID);

verify(mockSharedPreferences, times(1)).getString("SESSION_CID", null);
verify(mockSharedPreferences, times(1)).getString(SESSION_CID, null);
verify(mockSharedPreferences, times(0)).edit();
assertNull(result);
}
Expand Down
Loading

0 comments on commit 98eb816

Please sign in to comment.