Skip to content

Commit

Permalink
talent-solution: added missing custom attributes (#2327)
Browse files Browse the repository at this point in the history
* second part 2 of Cloud Talent solution

* renamed lengthy class names

* added missing custom attributes into createJob

* removed the extra space

* Update jobs/v4/src/main/java/com/example/jobs/JobSearchCreateJobCustomAttributes.java

Co-Authored-By: Noah Negrey <[email protected]>

* fixed getjob test

Co-authored-by: Noah Negrey <[email protected]>
  • Loading branch information
munkhuushmgl and nnegrey authored Mar 18, 2020
1 parent 8186869 commit 9c7a4a7
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
// [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;
import com.google.cloud.talent.v4beta1.TenantOrProjectName;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Arrays;

public class JobSearchCreateJobCustomAttributes {

public static void createJob() throws IOException {
Expand All @@ -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 <i>wonderful</i> 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);
Expand Down
2 changes: 1 addition & 1 deletion jobs/v4/src/test/java/JobSearchCreateJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
74 changes: 74 additions & 0 deletions jobs/v4/src/test/java/JobSearchCreateJobWithCustomAttrTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
2 changes: 1 addition & 1 deletion jobs/v4/src/test/java/JobSearchGetJobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9c7a4a7

Please sign in to comment.