SAMZA-1329: Switch SamzaTaskProxy to use LocalityManager.
[samza.git] / samza-rest / src / test / java / org / apache / samza / rest / resources / TestTasksResource.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.samza.rest.resources;
20
21 import com.google.common.collect.ImmutableMap;
22 import java.io.IOException;
23 import java.util.Map;
24 import javax.ws.rs.core.Application;
25 import javax.ws.rs.core.Response;
26 import org.apache.samza.config.MapConfig;
27 import org.apache.samza.rest.SamzaRestApplication;
28 import org.apache.samza.rest.SamzaRestConfig;
29 import org.apache.samza.rest.model.Task;
30 import org.apache.samza.rest.proxy.task.TaskResourceConfig;
31 import org.apache.samza.rest.resources.mock.MockJobProxy;
32 import org.apache.samza.rest.resources.mock.MockResourceFactory;
33 import org.apache.samza.rest.resources.mock.MockTaskProxy;
34 import org.apache.samza.rest.resources.mock.MockTaskProxyFactory;
35 import org.apache.samza.serializers.model.SamzaObjectMapper;
36 import org.codehaus.jackson.map.ObjectMapper;
37 import org.codehaus.jackson.type.TypeReference;
38 import org.glassfish.jersey.test.JerseyTest;
39 import org.junit.Test;
40
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertTrue;
43
44
45 public class TestTasksResource extends JerseyTest {
46 private ObjectMapper objectMapper = SamzaObjectMapper.getObjectMapper();
47
48 @Override
49 protected Application configure() {
50 Map<String, String> configMap = ImmutableMap.of(TaskResourceConfig.CONFIG_TASK_PROXY_FACTORY,
51 MockTaskProxyFactory.class.getName(),
52 SamzaRestConfig.CONFIG_REST_RESOURCE_FACTORIES,
53 MockResourceFactory.class.getName());
54 SamzaRestConfig config = new SamzaRestConfig(new MapConfig(configMap));
55 return new SamzaRestApplication(config);
56 }
57
58 @Test
59 public void testGetTasks() throws IOException {
60 String requestUrl = String.format("v1/jobs/%s/%s/tasks", "testJobName", "testJobId");
61 Response response = target(requestUrl).request().get();
62 assertEquals(200, response.getStatus());
63 Task[] tasks = objectMapper.readValue(response.readEntity(String.class), Task[].class);
64 assertEquals(2, tasks.length);
65
66 assertEquals(MockTaskProxy.TASK_1_PREFERRED_HOST, tasks[0].getPreferredHost());
67 assertEquals(MockTaskProxy.TASK_1_CONTAINER_ID, tasks[0].getContainerId());
68 assertEquals(MockTaskProxy.TASK_1_NAME, tasks[0].getTaskName());
69 assertEquals(MockTaskProxy.PARTITIONS, tasks[0].getPartitions());
70
71 assertEquals(MockTaskProxy.TASK_2_PREFERRED_HOST, tasks[1].getPreferredHost());
72 assertEquals(MockTaskProxy.TASK_2_CONTAINER_ID, tasks[1].getContainerId());
73 assertEquals(MockTaskProxy.TASK_2_NAME, tasks[1].getTaskName());
74 assertEquals(MockTaskProxy.PARTITIONS, tasks[1].getPartitions());
75 }
76
77 @Test
78 public void testGetTasksWithInvalidJobName() throws IOException {
79 String requestUrl = String.format("v1/jobs/%s/%s/tasks", "BadJobName", MockJobProxy.JOB_INSTANCE_4_ID);
80 Response resp = target(requestUrl).request().get();
81 assertEquals(400, resp.getStatus());
82 final Map<String, String> errorMessage = objectMapper.readValue(resp.readEntity(String.class), new TypeReference<Map<String, String>>() {});
83 assertTrue(errorMessage.get("message"), errorMessage.get("message").contains("Invalid arguments for getTasks. "));
84 resp.close();
85 }
86
87 @Test
88 public void testGetTasksWithInvalidJobId() throws IOException {
89 String requestUrl = String.format("v1/jobs/%s/%s/tasks", MockJobProxy.JOB_INSTANCE_1_NAME, "BadJobId");
90 Response resp = target(requestUrl).request().get();
91 assertEquals(400, resp.getStatus());
92 final Map<String, String> errorMessage = objectMapper.readValue(resp.readEntity(String.class), new TypeReference<Map<String, String>>() {});
93 assertTrue(errorMessage.get("message"), errorMessage.get("message").contains("Invalid arguments for getTasks. "));
94 resp.close();
95 }
96 }