forked from opensearch-project/job-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRestGetJobDetailsActionTests.java
159 lines (140 loc) · 5.63 KB
/
RestGetJobDetailsActionTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
package org.opensearch.jobscheduler.rest.action;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.junit.Before;
import org.mockito.Mockito;
import org.opensearch.core.action.ActionListener;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.jobscheduler.JobSchedulerPlugin;
import org.opensearch.jobscheduler.rest.request.GetJobDetailsRequest;
import org.opensearch.jobscheduler.utils.JobDetailsService;
import org.opensearch.rest.RestHandler;
import org.opensearch.rest.RestRequest;
import org.opensearch.test.OpenSearchTestCase;
import org.opensearch.test.rest.FakeRestChannel;
import org.opensearch.test.rest.FakeRestRequest;
import org.opensearch.transport.client.node.NodeClient;
@ThreadLeakScope(ThreadLeakScope.Scope.NONE)
public class RestGetJobDetailsActionTests extends OpenSearchTestCase {
private RestGetJobDetailsAction action;
private JobDetailsService jobDetailsService;
private String getJobDetailsPath;
private String updateJobDetailsPath;
private String jobIndex;
private String jobType;
private String jobRunnerAction;
private String jobParameterAction;
private String extensionUniqueId;
private String requestBody;
@Before
public void setUp() throws Exception {
super.setUp();
this.jobDetailsService = Mockito.mock(JobDetailsService.class);
this.action = new RestGetJobDetailsAction(jobDetailsService);
this.getJobDetailsPath = String.format(Locale.ROOT, "%s/%s", JobSchedulerPlugin.JS_BASE_URI, "_job_details");
this.updateJobDetailsPath = String.format(
Locale.ROOT,
"%s/%s/{%s}",
JobSchedulerPlugin.JS_BASE_URI,
"_job_details",
GetJobDetailsRequest.DOCUMENT_ID
);
this.jobIndex = "sample-index-name";
this.jobType = "sample-job-type";
this.jobRunnerAction = "sample-job-runner-action";
this.jobParameterAction = "sample-job-parameter-action";
this.extensionUniqueId = "sample-extension";
this.requestBody = "{\""
+ GetJobDetailsRequest.JOB_INDEX
+ "\":\""
+ this.jobIndex
+ "\",\""
+ GetJobDetailsRequest.JOB_TYPE
+ "\":\""
+ this.jobType
+ "\",\""
+ GetJobDetailsRequest.JOB_RUNNER_ACTION
+ "\":\""
+ this.jobRunnerAction
+ "\",\""
+ GetJobDetailsRequest.JOB_PARAMETER_ACTION
+ "\":\""
+ this.jobParameterAction
+ "\",\""
+ GetJobDetailsRequest.EXTENSION_UNIQUE_ID
+ "\":\""
+ this.extensionUniqueId
+ "\"}";
}
public void testGetNames() {
String name = action.getName();
assertEquals(action.GET_JOB_DETAILS_ACTION, name);
}
public void testGetRoutes() {
List<RestHandler.Route> routes = action.routes();
assertEquals(getJobDetailsPath, routes.get(0).getPath());
assertEquals(updateJobDetailsPath, routes.get(1).getPath());
}
public void testPrepareGetJobDetailsRequest() throws IOException {
String documentId = null;
Map<String, String> params = new HashMap<>();
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath(getJobDetailsPath)
.withParams(params)
.withContent(new BytesArray(requestBody), XContentType.JSON)
.build();
final FakeRestChannel channel = new FakeRestChannel(request, true, 0);
Mockito.doNothing()
.when(jobDetailsService)
.processJobDetails(
documentId,
jobIndex,
jobType,
jobParameterAction,
jobRunnerAction,
extensionUniqueId,
ActionListener.wrap(response -> {}, exception -> {})
);
action.prepareRequest(request, Mockito.mock(NodeClient.class));
assertEquals(channel.responses().get(), 0);
assertEquals(channel.errors().get(), 0);
}
public void testPrepareUpdateJobDetailsRequest() throws IOException {
String documentId = "document-id";
Map<String, String> params = new HashMap<>();
params.put(GetJobDetailsRequest.DOCUMENT_ID, documentId);
FakeRestRequest request = new FakeRestRequest.Builder(xContentRegistry()).withMethod(RestRequest.Method.PUT)
.withPath(updateJobDetailsPath)
.withParams(params)
.withContent(new BytesArray(requestBody), XContentType.JSON)
.build();
final FakeRestChannel channel = new FakeRestChannel(request, true, 0);
Mockito.doNothing()
.when(jobDetailsService)
.processJobDetails(
documentId,
jobIndex,
jobType,
jobParameterAction,
jobRunnerAction,
extensionUniqueId,
ActionListener.wrap(response -> {}, exception -> {})
);
action.prepareRequest(request, Mockito.mock(NodeClient.class));
assertEquals(channel.responses().get(), 0);
assertEquals(channel.errors().get(), 0);
}
}