Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

chore(samples): Retail Tutorials. Events (write, rejoin, purge) #303

Merged
merged 21 commits into from
Mar 16, 2022

Conversation

sborisenkox
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> ☕️

@sborisenkox sborisenkox requested a review from a team as a code owner February 18, 2022 12:54
@product-auto-label product-auto-label bot added the api: retail Issues related to the googleapis/java-retail API. label Feb 18, 2022
@generated-files-bot
Copy link

Warning: This pull request is touching the following templated files:

  • .kokoro/build.sh
  • samples/pom.xml

@product-auto-label product-auto-label bot added the samples Issues that are directly related to samples. label Feb 18, 2022
@snippet-bot
Copy link

snippet-bot bot commented Feb 18, 2022

Here is the summary of changes.

You are about to add 3 region tags.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Feb 18, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Feb 18, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 18, 2022
@kweinmeister kweinmeister added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 18, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Feb 18, 2022
Copy link

@kurtisvg kurtisvg left a comment

Choose a reason for hiding this comment

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

I've only reviewed the first sample. but similar changes should probably be made throughout

.kokoro/nightly/tutorials-samples.cfg Outdated Show resolved Hide resolved
Comment on lines 9 to 28
env_vars: {
key: "JOB_TYPE"
value: "tutorials-samples"
}

# TODO: remove this after we've migrated all tests and scripts
env_vars: {
key: "PROJECT_NUMBER"
value: "779844219229"
}

env_vars: {
key: "PROJECT_ID"
value: "java-docs-samples-testing"
}

env_vars: {
key: "GOOGLE_APPLICATION_CREDENTIALS"
value: "secret_manager/java-docs-samples-service-account"
}

Choose a reason for hiding this comment

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

In general we usually try to avoid putting secrets here and instead put them in secret-manager.

Copy link

Choose a reason for hiding this comment

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

This issue is still open.

Copy link
Contributor

Choose a reason for hiding this comment

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

This is in secret manager. This just configures the env var for ADC

owlbot.py Outdated Show resolved Hide resolved
samples/interactive-tutorials/pom.xml Outdated Show resolved Hide resolved
samples/interactive-tutorials/pom.xml Outdated Show resolved Hide resolved
public static void callPurgeUserEvents()
throws IOException, ExecutionException, InterruptedException {
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation =
UserEventServiceClient.create().purgeUserEventsAsync(getPurgeUserEventRequest());

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.


System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);

return purgeUserEventsRequest;

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

we need to return the operation name to assert it in the test

Copy link

Choose a reason for hiding this comment

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

From the style guide:

Snippet methods should specify a return type of void and avoid returning any value wherever possible. Instead, show the user how to interact with a returned object programmatically by printing some example attributes to the console.

Assert the behavior is correct from the output to console, since that is what the user will use to verify the output is correct.

// [START retail_purge_user_event]

/*
* Import user events into a catalog from inline source using Retail API

Choose a reason for hiding this comment

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

The comment does not look correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

// [START retail_rejoin_user_event]

/*
* Import user events into a catalog from inline source using Retail API

Choose a reason for hiding this comment

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

Comment not correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

// [START retail_write_user_event]

/*
* Import user events into a catalog from inline source using Retail API

Choose a reason for hiding this comment

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

Same here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 1, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 1, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 1, 2022
@Neenu1995 Neenu1995 requested a review from kurtisvg March 2, 2022 17:19
@Neenu1995 Neenu1995 dismissed kurtisvg’s stale review March 2, 2022 17:19

Re-requested review.


System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);

return purgeUserEventsRequest;
Copy link

Choose a reason for hiding this comment

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

From the style guide:

Snippet methods should specify a return type of void and avoid returning any value wherever possible. Instead, show the user how to interact with a returned object programmatically by printing some example attributes to the console.

Assert the behavior is correct from the output to console, since that is what the user will use to verify the output is correct.

String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String visitorId = "test_visitor_id";

writeUserEvent(visitorId);
Copy link

Choose a reason for hiding this comment

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

Why is this included here?

Copy link

Choose a reason for hiding this comment

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

It looks like this is something that is used for testing? It doesn't need to be included in main, the test should set whatever resources it needs on the testing side.

Copy link
Contributor

Choose a reason for hiding this comment

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

the visitor Id is a required value for the writeUserEvent method and is a filter value for the purgeUserEvent method using filtering
So it sud be here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed constant visitorId value to random generated.

Copy link

Choose a reason for hiding this comment

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

I think there's some misunderstand about the Method Structure:

The "main" method should just be a collection of inputs the user would need to provide to run the sample. It should use examples that the user can edit and replace with their own data to quickly and easily run the sample. We shouldn't need to rely on it executing for the testing - we should rely directly on the snippet method instead.

It should essentially look like this:

public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String defaultCatalog =
        String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
    // This should be a comment describing where the visitor id is or a link to a different sample if it's created in that sample 
    String visitorId = "my-visitor-id";
   callPurgeUserEvents(visitorId, defaultCatalog);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you mean projectId should have placeholder value? E.g my-project-id ?
If I do so, the build will fail, and in github CI checks fails also.
visitorId can have any String value, that's okay.

Choose a reason for hiding this comment

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

It should have a placeholder value - it's failing because we're using the main for testing instead of calling callPurgeUserEvents directly for testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

Comment on lines 59 to 71
private static PurgeUserEventsRequest getPurgeUserEventRequest(
String visitorId, String defaultCatalog) {
PurgeUserEventsRequest purgeUserEventsRequest =
PurgeUserEventsRequest.newBuilder()
// TO CHECK ERROR HANDLING SET INVALID FILTER HERE:
.setFilter(String.format("visitorId=\"%s\"", visitorId))
.setParent(defaultCatalog)
.setForce(true)
.build();

System.out.printf("Purge user events request: %s%n", purgeUserEventsRequest);

return purgeUserEventsRequest;
Copy link

Choose a reason for hiding this comment

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

I don't think this needs to be in a separate function - it's only used once and should just be declared inline before it's used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, inlined it.

Comment on lines 9 to 28
env_vars: {
key: "JOB_TYPE"
value: "tutorials-samples"
}

# TODO: remove this after we've migrated all tests and scripts
env_vars: {
key: "PROJECT_NUMBER"
value: "779844219229"
}

env_vars: {
key: "PROJECT_ID"
value: "java-docs-samples-testing"
}

env_vars: {
key: "GOOGLE_APPLICATION_CREDENTIALS"
value: "secret_manager/java-docs-samples-service-account"
}
Copy link

Choose a reason for hiding this comment

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

This issue is still open.

@Before
public void setUp() throws IOException, InterruptedException, ExecutionException {
Process exec =
Runtime.getRuntime().exec("mvn compile exec:java -Dexec.mainClass=events.PurgeUserEvent");
Copy link

Choose a reason for hiding this comment

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

Why are we running this with Runtime.getRuntime()? Can't we just call the snippet function directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To check the response, we should get output from the console and assert then.
This approach is working.

Copy link

Choose a reason for hiding this comment

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

I think it's better to avoid exec and just capture the stdout directly. Please see this example for the standard way we do it: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/storage/cloud-client/src/test/java/com/example/storage/QuickstartSampleIT.java#L47-L67

Copy link
Contributor

Choose a reason for hiding this comment

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

this is the command the user will use to ru the code sample, isn't it better to repeat the user actions here in the test, to simulate the command running and the checking the output?

Copy link

Choose a reason for hiding this comment

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

Typically, main is just a stub to make it easier for the user to provide input. That input should be virtually identical to args needed to run the method directly. In general we try to avoid using environment variables to configure snippets, because it's often easier for users to just edit the sample directly and run in whichever way is most comfortable for them (especially for java, it's not always from a terminal).

Choose a reason for hiding this comment

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

This comment is still open.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed test logic.

@kweinmeister kweinmeister added the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 10, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 10, 2022
@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 10, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 10, 2022
@kweinmeister kweinmeister added owlbot:run Add this label to trigger the Owlbot post processor. and removed owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 14, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 14, 2022
@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 14, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 14, 2022
OperationFuture<PurgeUserEventsResponse, PurgeMetadata> purgeOperation =
userEventServiceClient.purgeUserEventsAsync(purgeUserEventsRequest);

System.out.printf("The purge operation was started: %s%n", purgeOperation.getName());

Choose a reason for hiding this comment

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

nit: are we sure we only want to show the user starting this operation, and not waiting for the results?

Copy link
Contributor

Choose a reason for hiding this comment

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

This operation is running from several hours up to 3 Days, so we will only show the operation Id as a proof, that it was started

@Before
public void setUp() throws IOException, InterruptedException, ExecutionException {
Process exec =
Runtime.getRuntime().exec("mvn compile exec:java -Dexec.mainClass=events.PurgeUserEvent");

Choose a reason for hiding this comment

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

This comment is still open.

String.format("projects/%s/locations/global/catalogs/default_catalog", projectId);
String visitorId = "test_visitor_id";

writeUserEvent(visitorId);

Choose a reason for hiding this comment

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

It should have a placeholder value - it's failing because we're using the main for testing instead of calling callPurgeUserEvents directly for testing.

Copy link

@kurtisvg kurtisvg left a comment

Choose a reason for hiding this comment

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

marking approved, but please note this comment chain about putting placeholders for the user in "main": #303 (comment)

This is generally a preferred approach because it doesn't encourage users to set environment variables, which can be tricky in some environments.

@kweinmeister kweinmeister added owlbot:run Add this label to trigger the Owlbot post processor. and removed owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 15, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 15, 2022
@kweinmeister kweinmeister added kokoro:force-run Add this label to force Kokoro to re-run the tests. owlbot:run Add this label to trigger the Owlbot post processor. labels Mar 15, 2022
@gcf-owl-bot gcf-owl-bot bot removed the owlbot:run Add this label to trigger the Owlbot post processor. label Mar 15, 2022
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Mar 15, 2022
@kweinmeister kweinmeister merged commit 159b57a into googleapis:main Mar 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api: retail Issues related to the googleapis/java-retail API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants