Skip to content

Commit

Permalink
Remove references to 'bookshelf' in background-processing, and fix te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
dzlier-gcp committed Nov 25, 2019
1 parent debeab1 commit 0e156b1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 28 deletions.
5 changes: 4 additions & 1 deletion .kokoro/tests/diff_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ gradle -v
# Setup required environment variables
export GOOGLE_APPLICATION_CREDENTIALS=${KOKORO_GFILE_DIR}/service-acct.json
export GOOGLE_CLOUD_PROJECT=java-docs-samples-testing
export FIRESTORE_CLOUD_PROJECT=java-docs-testing-firestore

# Activate service account
gcloud auth activate-service-account\
--key-file=$GOOGLE_APPLICATION_CREDENTIALS \
Expand Down Expand Up @@ -64,7 +66,8 @@ for file in **/pom.xml; do
-Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn \
-Dmaven.test.redirectTestOutputToFile=true \
-Dbigtable.projectID="${GOOGLE_CLOUD_PROJECT}" \
-Dbigtable.instanceID=instance
-Dbigtable.instanceID=instance \
-Dwebdriver.chrome.driver=/usr/local/bin/chromedriver
EXIT=$?

if [ $EXIT -ne 0 ]; then
Expand Down
2 changes: 2 additions & 0 deletions .kokoro/tests/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ gcloud auth activate-service-account\
echo "********* gcloud config ********"
gcloud config list

export FIRESTORE_CLOUD_PROJECT=java-docs-testing-firestore

echo "******** build everything ********"
cd github/getting-started-java
mvn -B --fail-at-end -q clean verify | grep -E -v "(^\[INFO\] Download|^\[INFO\].*skipping)"
Expand Down
11 changes: 6 additions & 5 deletions background/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ To run your project locally:

export PUBSUB_TOPIC=<your-topic-name>
export PUBSUB_VERIFICATION_TOKEN=<your-verification-token>
export FIRESTORE_CLOUD_PROJECT=<your-project-id>
* Create a Pub/Sub topic:

gcloud pubsub topics create $PUBSUB_TOPIC
Expand All @@ -34,7 +35,7 @@ To run your project locally:
You will see that nothing has changed. This because there is no subscription on that Pub/Sub topic
yet. Since you can't set up a Pub/Sub push subscription to post requests to `localhost`, you can
instead send a manual request with `curl` (from a second terminal, in the
`getting-started-java/bookshelf/background` directory):
`getting-started-java/background` directory):

curl -H "Content-Type: application/json" -i --data @sample_message.json \
"localhost:8080/pubsub/push?token=$PUBSUB_VERIFICATION_TOKEN"
Expand All @@ -52,7 +53,7 @@ To build your image:
mvn clean package jib:build
* Deploy the app to Cloud Run:

gcloud beta run deploy bookshelf --image gcr.io/<MY_PROJECT>/bookshelf \
gcloud beta run deploy background --image gcr.io/<MY_PROJECT>/background \
--platform managed --region us-central1 --memory 512M \
--update-env-vars PUBSUB_TOPIC=$PUBSUB_TOPIC,PUBSUB_VERIFICATION_TOKEN=$PUBSUB_VERIFICATION_TOKEN

Expand All @@ -66,9 +67,9 @@ To build your image:
--ack-deadline 30

This command will output a link to visit the page, hereafter called <CLOUD_RUN_ENDPOINT>.
* Now fill out the `+ Request Translation` form again, this time <CLOUD_RUN_ENDPOINT>. It will
redirect you back to /translate.
* The new request will take a moment to show, so refresh after a minute.
* Now fill out the `+ Request Translation` form again, this time at <CLOUD_RUN_ENDPOINT>. When you
click `Submit` it will redirect you back to /translate.
* The new request will take a moment to show, so refresh after a minute or two.

[jib]: https://github.com/GoogleContainerTools/jib

Expand Down
43 changes: 35 additions & 8 deletions background/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ Copyright 2019 Google LLC
</parent>

<properties>
<!-- [START bookshelf_config] -->
<gcloud.appId>MY_PROJECT</gcloud.appId>
<!-- [END bookshelf_config] -->

<failOnMissingWebXml>false</failOnMissingWebXml>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down Expand Up @@ -74,8 +72,7 @@ Copyright 2019 Google LLC
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
<scope>compile</scope>
<version>28.0-jre</version>
</dependency>

<dependency>
Expand All @@ -100,17 +97,19 @@ Copyright 2019 Google LLC
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.3.1</version>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.3.1</version>
<version>3.5.3</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>bookshelf-cloud-run</finalName>
<finalName>background-processing</finalName>
<!-- Optional - for hot reload of the web application when using an IDE Eclipse / IDEA -->
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes
</outputDirectory>
Expand All @@ -120,16 +119,44 @@ Copyright 2019 Google LLC
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
</plugin>

<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>1.7.0</version>
<configuration>
<to>
<image>gcr.io/${gcloud.appId}/bookshelf</image>
<image>gcr.io/${gcloud.appId}/background</image>
</to>
</configuration>
</plugin>
<!-- Integration tests need the servlet up to work -->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>${jetty.version}</version>
<configuration>
<stopKey>stopPlzKThxBai</stopKey>
<stopPort>9283</stopPort>
<stopWait>60</stopWait>
</configuration>
<executions>
<execution>
<id>start-server</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-server</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public void contextDestroyed(javax.servlet.ServletContextEvent event) {}

@Override
public void contextInitialized(ServletContextEvent event) {
String firestoreProjectId = System.getenv("FIRESTORE_CLOUD_PROJECT");
Firestore firestore = (Firestore) event.getServletContext().getAttribute("firestore");
if (firestore == null) {
firestore =
FirestoreOptions.getDefaultInstance().toBuilder()
.setProjectId(System.getenv("GOOGLE_CLOUD_PROJECT"))
.setProjectId(firestoreProjectId)
.build()
.getService();
event.getServletContext().setAttribute("firestore", firestore);
Expand All @@ -58,7 +59,7 @@ public void contextInitialized(ServletContextEvent event) {
publisher =
Publisher.newBuilder(
ProjectTopicName.newBuilder()
.setProject(ServiceOptions.getDefaultProjectId())
.setProject(firestoreProjectId)
.setTopic(topicId)
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/*
* I can't figure out how to test server-side logging in a selenium test, so just sanity-check that
* it hasn't broken anything.
*/
@RunWith(JUnit4.class)
@SuppressWarnings("checkstyle:abbreviationaswordinname")
public class UserJourneyTestIT {
Expand All @@ -52,7 +52,7 @@ public class UserJourneyTestIT {
private static final String SOURCE_LANG_CODE = "en";
private static final String TARGET_LANG_CODE = "es";

private static DriverService service;
private static ChromeDriverService service;
private WebDriver driver;

@BeforeClass
Expand All @@ -63,7 +63,7 @@ public static void setupClass() throws Exception {

@AfterClass
public static void tearDownClass() throws ExecutionException, InterruptedException {
// Clear the firestore list if we're not using the local emulator
// Clear the firestore list
Firestore firestore = FirestoreOptions.getDefaultInstance().getService();
for (QueryDocumentSnapshot docSnapshot :
firestore.collection("translations").get().get().getDocuments()) {
Expand All @@ -79,7 +79,9 @@ public static void tearDownClass() throws ExecutionException, InterruptedExcepti

@Before
public void setup() {
driver = new ChromeDriver();
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability("Platform", Platform.ANY);
driver = new ChromeDriver(service, caps);
}

@After
Expand All @@ -89,13 +91,13 @@ public void tearDown() {

private WebElement checkLandingPage() {
WebElement button = driver.findElement(By.cssSelector("a.btn"));
assertEquals("Request Translation", button.getText());
assertEquals("Request translation", button.getText());

WebElement heading = driver.findElement(By.cssSelector("body>.container h3"));
assertEquals("Translations", heading.getText());

List<WebElement> list = driver.findElements(By.cssSelector("body>.container tr"));
assertEquals("Should be no entries in translation list.", 1, list.size());
assertEquals("Should be no entries in translation list.", 0, list.size());

return button;
}
Expand Down Expand Up @@ -140,14 +142,14 @@ public void userJourney() {
WebElement button = checkLandingPage();

button.click();
new WebDriverWait(driver, Duration.ofSeconds(10))
new WebDriverWait(driver, 10)
.until(ExpectedConditions.urlMatches(".*/create$")::apply);

checkRequestTranslationPage();

submitForm();
new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.urlMatches(".*/translate$")::apply);
new WebDriverWait(driver, 10)
.until(ExpectedConditions.urlMatches(".*/")::apply);
} catch (Exception e) {
System.err.println(driver.getPageSource());
throw e;
Expand Down

0 comments on commit 0e156b1

Please sign in to comment.