diff --git a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java index a08f7ae0e8f..3ac81bce4a8 100644 --- a/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java +++ b/jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java @@ -19,6 +19,7 @@ // [START job_search_create_job_custom_attributes] import com.google.cloud.talent.v4beta1.CreateJobRequest; +import com.google.cloud.talent.v4beta1.CustomAttribute; import com.google.cloud.talent.v4beta1.Job; import com.google.cloud.talent.v4beta1.JobServiceClient; import com.google.cloud.talent.v4beta1.TenantName; @@ -26,6 +27,9 @@ import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; + public class JobSearchCreateJobCustomAttributes { public static void createJob() throws IOException { @@ -39,21 +43,32 @@ public static void createJob() throws IOException { // Create Job with Custom Attributes. public static void createJob( - String projectId, - String tenantId, - String companyId, - String requisitionId) - throws IOException { + String projectId, + String tenantId, + String companyId, + String requisitionId) + throws IOException { try (JobServiceClient jobServiceClient = JobServiceClient.create()) { TenantOrProjectName parent = TenantName.of(projectId, tenantId); + + // Custom attribute can be string or numeric value, and can be filtered in search queries. + // https://cloud.google.com/talent-solution/job-search/docs/custom-attributes + CustomAttribute customAttribute = CustomAttribute.newBuilder() + .addStringValues("Internship") + .addStringValues("Apprenticeship") + .setFilterable(true) + .build(); + Job job = Job.newBuilder() .setCompany(companyId) - .setTitle("Software Developer II") + .setTitle("Software Developer I") .setDescription("This is a description of this wonderful job!") + .putCustomAttributes("FOR_STUDENTS", customAttribute) .setRequisitionId(requisitionId) .setLanguageCode("en-US") .build(); + CreateJobRequest request = CreateJobRequest.newBuilder().setParent(parent.toString()).setJob(job).build(); Job response = jobServiceClient.createJob(request); diff --git a/jobs/v4/src/test/java/JobSearchCreateJobTest.java b/jobs/v4/src/test/java/JobSearchCreateJobTest.java index eabdb54cb10..821caba5d33 100644 --- a/jobs/v4/src/test/java/JobSearchCreateJobTest.java +++ b/jobs/v4/src/test/java/JobSearchCreateJobTest.java @@ -48,7 +48,7 @@ public void setUp() { @Test public void testCreateJob() throws IOException { - // retrieve a tenant. + // create a job. JobSearchCreateJob.createJob( PROJECT_ID, TENANT_ID, COMPANY_ID, POST_UNIQUE_ID, "http://www.jobUrl.com"); String got = bout.toString(); diff --git a/jobs/v4/src/test/java/JobSearchCreateJobWithCustomAttrTest.java b/jobs/v4/src/test/java/JobSearchCreateJobWithCustomAttrTest.java new file mode 100644 index 00000000000..a1c3a5d1d2a --- /dev/null +++ b/jobs/v4/src/test/java/JobSearchCreateJobWithCustomAttrTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2020 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import static com.google.common.truth.Truth.assertThat; + +import com.example.jobs.JobSearchCreateJobCustomAttributes; +import com.example.jobs.JobSearchDeleteJob; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.UUID; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class JobSearchCreateJobWithCustomAttrTest { + private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); + private static final String TENANT_ID = "50c14f00-dc38-4812-989b-d9b59c7fdf07"; + private static final String COMPANY_ID = "bdad284d-9aca-4cb9-af09-ce65afcc5d6a"; + private static final String POST_UNIQUE_ID = + String.format( + "TEST_POST_ID_%s", + UUID.randomUUID().toString().substring(0, 20)); // Posting ID. Unique per job. + + private String jobId; + private ByteArrayOutputStream bout; + private PrintStream out; + + @Before + public void setUp() { + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @Test + public void testCreateJob() throws IOException { + // create a job with custom attributes. + JobSearchCreateJobCustomAttributes.createJob( + PROJECT_ID, TENANT_ID, COMPANY_ID, POST_UNIQUE_ID); + String got = bout.toString(); + + assertThat(got).contains("Created job:"); + jobId = JobSearchGetJobTest.extractLastId(got.split("\n")[0].trim()); + + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + System.setOut(out); + } + + @After + public void tearDown() throws IOException { + // delete that job. + JobSearchDeleteJob.deleteJob(PROJECT_ID, TENANT_ID, jobId); + String got = bout.toString(); + assertThat(got).contains("Deleted job"); + System.setOut(null); + } +} diff --git a/jobs/v4/src/test/java/JobSearchGetJobTest.java b/jobs/v4/src/test/java/JobSearchGetJobTest.java index 663931dc237..df9c1a1d1ed 100644 --- a/jobs/v4/src/test/java/JobSearchGetJobTest.java +++ b/jobs/v4/src/test/java/JobSearchGetJobTest.java @@ -28,7 +28,7 @@ public class JobSearchGetJobTest { private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT"); private static final String TENANT_ID = "50c14f00-dc38-4812-989b-d9b59c7fdf07"; - private static final String JOB_ID = "76652042166117062"; + private static final String JOB_ID = "122880074199245510"; private ByteArrayOutputStream bout; private PrintStream out;