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

Update Functional Tests, use Core TimeUtils date formatter, remove Jetifier #50

Merged
merged 16 commits into from
Dec 22, 2022

Conversation

cacheung
Copy link
Contributor

@cacheung cacheung commented Dec 19, 2022

Update Functional Tests:

  • Change the folder structure, move all Util files under to util folder
  • Update all the test constants to ConsentTestConstants
  • Update Util files for 2.0 changes

Updated to use Core TimeUtils date format.

Update the remaining powermock code and dependencies.

Remove Jetifier.

Description

Related Issue

Motivation and Context

How Has This Been Tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • I have signed the Adobe Open Source CLA.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Update Functional test
…white box code

Use Core TimeUtils for data formatter and remove remaining powermock white box code
Update Functional Tests
Test clean up
remove a commented code
metaDataContents.put(ConsentConstants.EventDataKey.TIME, TimeUtils.getIso8601DateTimeZoneISO8601());
metaDataContents.put(
ConsentConstants.EventDataKey.TIME,
TimeUtils.getISO8601UTCDateWithMilliseconds(new Date(timeStamp))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Core TimeUtils here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be addressed with the latest update

…tions()

Fix functional tests, add wait time and remove extra resetTestExpectations()
@codecov
Copy link

codecov bot commented Dec 19, 2022

Codecov Report

Merging #50 (f3a3dfd) into dev-v2.0.0 (8a6fa05) will increase coverage by 8.89%.
The diff coverage is 82.86%.

@@              Coverage Diff               @@
##           dev-v2.0.0      #50      +/-   ##
==============================================
+ Coverage       75.88%   84.77%   +8.89%     
==============================================
  Files              11        4       -7     
  Lines             398      256     -142     
  Branches           44       30      -14     
==============================================
- Hits              302      217      -85     
+ Misses             67       23      -44     
+ Partials           29       16      -13     
Flag Coverage Δ
functional-tests 66.80% <67.14%> (+10.26%) ⬆️
unit-tests 82.81% <77.14%> (+9.45%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
.../adobe/marketing/mobile/edge/consent/Consents.java 76.47% <57.14%> (-2.80%) ⬇️
...m/adobe/marketing/mobile/edge/consent/Consent.java 78.05% <77.78%> (+16.35%) ⬆️
...arketing/mobile/edge/consent/ConsentExtension.java 88.89% <93.94%> (+17.34%) ⬆️
.../marketing/mobile/edge/consent/ConsentManager.java 100.00% <100.00%> (ø)

Copy link
Contributor

@timkimadobe timkimadobe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Use Jitpack Assurance and Remove Jetifier
@cacheung cacheung changed the title Update Functional Tests and use Core TimeUtils date formatter. Update Functional Tests, use Core TimeUtils date formatter, remove Jetifier Dec 20, 2022
@@ -31,6 +29,8 @@ static final class EventDataKey {
private EventDataKey() {}
}

private ConsentConstants() {}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you move constructor to line 20.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved constructor for ConsentConstants above static final class definitions!

private final ConsentManager consentManager;

// package private vars
ConsentManager consentManager;
Copy link

@prudrabhat prudrabhat Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest injecting this variable via constructor and retain its final nature while making it testable. That way you can mock this directly in your test as well as eliminate the whitebox mock.

	protected ConsentExtension(final ExtensionApi extensionApi) {
		this(
			extensionApi,
			ServiceProvider
				.getInstance()
				.getDataStoreService()
				.getNamedCollection(ConsentConstants.DataStoreKey.DATASTORE_NAME)
		);
	}

	@VisibleForTesting
        ConsentExtension(final ExtensionApi extensionApi, final NamedCollection namedCollection) {
		this(extensionApi, new ConsentManager(namedCollection))
	} 

	protected ConsentExtension(final ExtensionApi extensionApi, final ConsentManager consentManager) {
		super(extensionApi);
		this.consentManager = consentManager;
	} 

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Prashanth! Refactored this with the suggested implementation and updated the related test cases

… injection

Update test helper method that sets up persistence using new constructor
Update ConsentConstants constructor position
Copy link
Contributor

@timkimadobe timkimadobe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @prudrabhat! I noticed during the implementation of the refactor for ConsentExtension constructor, when giving protected status even without the @VisibleForTesting annotation, it is still accessible to the test cases. So I defined the constructor like this:

protected ConsentExtension(final ExtensionApi extensionApi, final NamedCollection namedCollection)

Also, I wasn't able to fully remove the setupExistingConsents helper function in ConsentExtensionTest because it handles the alternate test case setup flow where the mocked NamedCollection needs to have its getString API mocked with the provided jsonString to simulate the situation where there are consents already in persistence.

I noticed in the existing ConsentExtension logic that this fetch from persistence by the ConsentManager is only done once on initialization, so having this helper method to set the API mock with the required consents value and reinstantiate the ConsentExtension to trigger the read from persistence using the mocked NamedCollection was helpful to keep all the test cases which require this logical flow working. What do you think?

I also noticed that there is a similar case in ConsentManager where the property Consents defaultConsents is being accessed directly by test cases in order to perform the required assertions; is this also something we want to refactor in a similar way to the mutable package private consentManager?

@@ -31,6 +29,8 @@ static final class EventDataKey {
private EventDataKey() {}
}

private ConsentConstants() {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved constructor for ConsentConstants above static final class definitions!

private final ConsentManager consentManager;

// package private vars
ConsentManager consentManager;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you Prashanth! Refactored this with the suggested implementation and updated the related test cases

* @param configuration the initial configuration update that needs to be applied
* @throws InterruptedException if the wait time for extension registration has elapsed
*/
public static void registerExtensions(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Move to TestHelper.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved function and registration timeout constant to TestHelper, and refactored all usages!

* @param source the source of the event.
* @param count the number of events expected to be received.
*/
public static void setExpectedEvent(final String type, final String source, final int count) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the ability to set an "expected" event was removed from the MonitorExtension. If this functionality isn't needed, then can you remove the expectedEvents variable and any handling of checking for or returning expected events?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed expectedEvents property and all associated logic!


import static org.junit.Assert.fail;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.adobe.marketing.mobile.edge.consent.ConsentTestConstants;
import java.util.ArrayList;

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the TestPersistenceHelper class needed anymore? Can the test cases use a mocked DataStoreService instead of reading the persisted values from shared preferences?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If separating test cases from the actual SharedPreferences is something we want to pursue, is that something we could cover in a separate task? I feel that the scope is a bit large seeing that many different test cases interact with SharedPreferences by updating/reading values and we want to make sure to properly handle all those cases and also test the integration of Consent with the actual SharedPreferences

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, totally fine to take this up in a separate PR.

@@ -49,14 +50,27 @@ protected ConsentExtension(final ExtensionApi extensionApi) {
}

/**
* Constructor used for testing.
* Convenience constructor that instantiates a new {@link ConsentManager} using the passed {@link NamedCollection}.
* <br><br>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a single <p> tag to add a line break in JavaDoc.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Updated to use the <p> tag

private Consents defaultConsents; // holds on to default consents obtained from configuration response

// package private vars
Consents defaultConsents; // holds on to default consents obtained from configuration response
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this package-private for testing or production? Can @VisbleForTesting be used here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's package-private for testing purposes! I've added the @VisibleForTesting annotation as suggested

/**
* Sets up existing consents values in persistence by setting up a mocked return value for the
* mocked {@link NamedCollection} using the passed JSON consents {@link String}.
* <br><br>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use <p> to add line break in JavaDocs.

Copy link
Contributor

@timkimadobe timkimadobe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @kevinlind! I've addressed the feedback with the latest updates, with one outstanding question on the refactor of TestPersistenceHelper

* @param configuration the initial configuration update that needs to be applied
* @throws InterruptedException if the wait time for extension registration has elapsed
*/
public static void registerExtensions(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved function and registration timeout constant to TestHelper, and refactored all usages!

* @param source the source of the event.
* @param count the number of events expected to be received.
*/
public static void setExpectedEvent(final String type, final String source, final int count) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed expectedEvents property and all associated logic!

private Consents defaultConsents; // holds on to default consents obtained from configuration response

// package private vars
Consents defaultConsents; // holds on to default consents obtained from configuration response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's package-private for testing purposes! I've added the @VisibleForTesting annotation as suggested

@@ -49,14 +50,27 @@ protected ConsentExtension(final ExtensionApi extensionApi) {
}

/**
* Constructor used for testing.
* Convenience constructor that instantiates a new {@link ConsentManager} using the passed {@link NamedCollection}.
* <br><br>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! Updated to use the <p> tag


import static org.junit.Assert.fail;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.adobe.marketing.mobile.edge.consent.ConsentTestConstants;
import java.util.ArrayList;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If separating test cases from the actual SharedPreferences is something we want to pursue, is that something we could cover in a separate task? I feel that the scope is a bit large seeing that many different test cases interact with SharedPreferences by updating/reading values and we want to make sure to properly handle all those cases and also test the integration of Consent with the actual SharedPreferences

metaDataContents.put(ConsentConstants.EventDataKey.TIME, TimeUtils.getIso8601DateTimeZoneISO8601());
metaDataContents.put(
ConsentConstants.EventDataKey.TIME,
TimeUtils.getISO8601UTCDateWithMilliseconds(new Date(timeStamp))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be addressed with the latest update

@timkimadobe timkimadobe merged commit ac20c32 into adobe:dev-v2.0.0 Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants