Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import com.azure.communication.jobrouter.models.RouterJobItem;
import com.azure.communication.jobrouter.models.RouterWorker;
import com.azure.communication.jobrouter.models.RouterWorkerItem;
import com.azure.communication.jobrouter.models.UnassignJobResult;
import com.azure.communication.jobrouter.models.WorkerStateSelector;
import com.azure.communication.jobrouter.models.options.CloseJobOptions;
import com.azure.communication.jobrouter.models.options.CreateJobOptions;
import com.azure.communication.jobrouter.models.options.CreateWorkerOptions;
import com.azure.communication.jobrouter.models.options.UnassignJobOptions;
import com.azure.communication.jobrouter.models.options.UpdateJobOptions;
import com.azure.communication.jobrouter.models.options.UpdateWorkerOptions;
import com.azure.core.annotation.ReturnType;
Expand Down Expand Up @@ -502,6 +504,62 @@ Mono<Response<Void>> closeJobWithResponse(CloseJobOptions closeJobOptions, Conte
}
}

/**
* Unassigns a job from a worker.
*
* @param unassignJobOptions Options object for unassign job operation.
* @return result as mentioned in {@link com.azure.communication.jobrouter.models.UnassignJobResult}.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws CommunicationErrorResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<UnassignJobResult> unassignJob(UnassignJobOptions unassignJobOptions) {
try {
return withContext(context -> unassignJobWithResponse(unassignJobOptions, context)
.flatMap(
(Response<UnassignJobResult> res) -> {
if (res.getValue() != null) {
return Mono.just(res.getValue());
} else {
return Mono.empty();
}
}));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}

/**
* Unassigns a job from the assigned worker.
*
* @param unassignJobOptions Options object for unassign job operation.
* @return result as mentioned in {@link com.azure.communication.jobrouter.models.UnassignJobResult}.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws CommunicationErrorResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<UnassignJobResult>> unassignJobWithResponse(UnassignJobOptions unassignJobOptions) {
try {
return withContext(context -> unassignJobWithResponse(unassignJobOptions, context));
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}

Mono<Response<UnassignJobResult>> unassignJobWithResponse(UnassignJobOptions unassignJobOptions, Context context) {
try {
return jobRouter.unassignJobActionWithResponseAsync(
unassignJobOptions.getJobId(),
unassignJobOptions.getAssignmentId(),
context
);
} catch (RuntimeException ex) {
return monoError(LOGGER, ex);
}
}

/**
* Retrieves list of jobs based on filter parameters.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
import com.azure.communication.jobrouter.models.RouterJobItem;
import com.azure.communication.jobrouter.models.RouterWorker;
import com.azure.communication.jobrouter.models.RouterWorkerItem;
import com.azure.communication.jobrouter.models.UnassignJobResult;
import com.azure.communication.jobrouter.models.WorkerStateSelector;
import com.azure.communication.jobrouter.models.options.CloseJobOptions;
import com.azure.communication.jobrouter.models.options.CreateJobOptions;
import com.azure.communication.jobrouter.models.options.CreateWorkerOptions;
import com.azure.communication.jobrouter.models.options.UnassignJobOptions;
import com.azure.communication.jobrouter.models.options.UpdateJobOptions;
import com.azure.communication.jobrouter.models.options.UpdateWorkerOptions;
import com.azure.core.annotation.ReturnType;
Expand Down Expand Up @@ -300,6 +302,35 @@ public Response<Void> closeJobWithResponse(CloseJobOptions closeJobOptions, Cont
return this.client.closeJobWithResponse(closeJobOptions, context).block();
}

/**
* Unassigns a job from assigned worker.
*
* @param unassignJobOptions Options object for close job operation.
* @return UnassignJobResult
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws CommunicationErrorResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public UnassignJobResult unassignJob(UnassignJobOptions unassignJobOptions) {
return this.client.unassignJob(unassignJobOptions).block();
}

/**
* Unassigns a job from assigned worker.
*
* @param unassignJobOptions Options object for close job operation.
* @param context The context to associate with this operation.
* @return void.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws CommunicationErrorResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<UnassignJobResult> unassignJobWithResponse(UnassignJobOptions unassignJobOptions, Context context) {
return this.client.unassignJobWithResponse(unassignJobOptions, context).block();
}

/**
* Retrieves list of jobs based on filter parameters.
*
Expand Down Expand Up @@ -489,7 +520,7 @@ public Response<RouterWorker> createWorkerWithResponse(CreateWorkerOptions creat
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public RouterWorker updateWorkerOptions(UpdateWorkerOptions updateWorkerOptions) {
public RouterWorker updateWorker(UpdateWorkerOptions updateWorkerOptions) {
return this.client.updateWorker(updateWorkerOptions).block();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public static RouterJob convertCreateJobOptionsToRouterJob(CreateJobOptions crea
return new RouterJob()
.setChannelId(createJobOptions.getChannelId())
.setChannelReference(createJobOptions.getChannelReference())
.setQueueId(createJobOptions.getQueueId())
.setLabels(labels)
.setNotes(createJobOptions.getNotes())
.setPriority(createJobOptions.getPriority())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static RouterWorker convertUpdateWorkerOptionsToRouterWorker(UpdateWorker
.collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().getValue())) : null;

Map<String, QueueAssignment> queueAssignmentsMap = updateWorkerOptions.getQueueAssignments();
Map<String, Object> queueAssignments = queueAssignmentsMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue()));
Map<String, Object> queueAssignments = queueAssignmentsMap != null ? queueAssignmentsMap.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue())) : null;
return new RouterWorker()
.setLabels(labels)
.setTags(updateWorkerOptions.getTags())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.communication.jobrouter.models.options;

/**
* Request options to unassign a job from a worker.
*/
public final class UnassignJobOptions {
/**
* id of job to close.
*/
private final String jobId;

/**
* id of assignment associated with the job.
*/
private final String assignmentId;

/**
* Constructor for UnassignJobOptions.
* @param jobId jobId to unassign.
* @param assignmentId assignmentId of the job.
*/
public UnassignJobOptions(String jobId, String assignmentId) {
this.jobId = jobId;
this.assignmentId = assignmentId;
}

/**
* Get jobId.
* @return jobId
*/
public String getJobId() {
return this.jobId;
}

/**
* Get assignmentId.
* @return assignmentId
*/
public String getAssignmentId() {
return this.assignmentId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.communication.jobrouter;

import com.azure.communication.jobrouter.models.AcceptJobOfferResult;
import com.azure.communication.jobrouter.models.ChannelConfiguration;
import com.azure.communication.jobrouter.models.DistributionPolicy;
import com.azure.communication.jobrouter.models.JobOffer;
import com.azure.communication.jobrouter.models.JobQueue;
import com.azure.communication.jobrouter.models.LabelValue;
import com.azure.communication.jobrouter.models.QueueAssignment;
import com.azure.communication.jobrouter.models.RouterJob;
import com.azure.communication.jobrouter.models.RouterWorker;
import com.azure.communication.jobrouter.models.UnassignJobResult;
import com.azure.communication.jobrouter.models.options.CreateJobOptions;
import com.azure.communication.jobrouter.models.options.CreateWorkerOptions;
import com.azure.communication.jobrouter.models.options.UnassignJobOptions;
import com.azure.core.http.HttpClient;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RouterJobLiveTests extends JobRouterTestBase {
private RouterClient routerClient;

private RouterAdministrationClient routerAdminClient;

@ParameterizedTest
@MethodSource("com.azure.core.test.TestBase#getHttpClients")
public void unassignJob(HttpClient httpClient) {
// Setup
routerClient = getRouterClient(httpClient);
routerAdminClient = getRouterAdministrationClient(httpClient);
String testName = "unassign-job-2";
/**
* Setup queue
*/
String distributionPolicyId = String.format("%s-%s-DistributionPolicy", JAVA_LIVE_TESTS, testName);
DistributionPolicy distributionPolicy = createDistributionPolicy(routerAdminClient, distributionPolicyId);

String queueId = String.format("%s-%s-Queue", JAVA_LIVE_TESTS, testName);
JobQueue jobQueue = createQueue(routerAdminClient, queueId, distributionPolicy.getId());

/**
* Setup worker
*/
Map<String, LabelValue> labels = new HashMap<String, LabelValue>() {
{
put("Label", new LabelValue("Value"));
}
};

Map<String, Object> tags = new HashMap<String, Object>() {
{
put("Tag", "Value");
}
};

ChannelConfiguration channelConfiguration = new ChannelConfiguration();
channelConfiguration.setCapacityCostPerJob(1);
Map<String, ChannelConfiguration> channelConfigurations = new HashMap<String, ChannelConfiguration>() {
{
put("channel1", channelConfiguration);
}
};

Map<String, QueueAssignment> queueAssignments = new HashMap<String, QueueAssignment>() {
{
put(jobQueue.getId(), new QueueAssignment());
}
};

String workerId = String.format("%s-%s-Worker", JAVA_LIVE_TESTS, testName);
CreateWorkerOptions createWorkerOptions = new CreateWorkerOptions(workerId, 10)
.setLabels(labels)
.setTags(tags)
.setAvailableForOffers(true)
.setChannelConfigurations(channelConfigurations)
.setQueueAssignments(queueAssignments);

RouterWorker worker = routerClient.createWorker(createWorkerOptions);

String jobId = String.format("%s-%s-Job", JAVA_LIVE_TESTS, testName);
CreateJobOptions createJobOptions = new CreateJobOptions(jobId, "channel1", queueId);

RouterJob job = routerClient.createJob(createJobOptions);

List<JobOffer> jobOffers = new ArrayList<>();
long startTimeMillis = System.currentTimeMillis();
while (true) {
worker = routerClient.getWorker(workerId);
jobOffers = worker.getOffers();
if (jobOffers.size() > 0 || System.currentTimeMillis() - startTimeMillis > 10000) {
break;
}
}

assertTrue(jobOffers.size() == 1);

JobOffer offer = jobOffers.get(0);

AcceptJobOfferResult acceptJobOfferResult = routerClient.acceptJobOffer(workerId, offer.getId());

String assignmentId = acceptJobOfferResult.getAssignmentId();

// Action
UnassignJobOptions unassignJobOptions = new UnassignJobOptions(jobId, assignmentId);
UnassignJobResult unassignJobResult = routerClient.unassignJob(unassignJobOptions);

// Verify
assertEquals(1, unassignJobResult.getUnassignmentCount());

// Cleanup
routerClient.cancelJob(jobId, "Done.", "test");
routerClient.deleteJob(jobId);
routerClient.deleteWorker(workerId);
routerAdminClient.deleteQueue(queueId);
routerAdminClient.deleteDistributionPolicy(distributionPolicyId);
}
}
Loading