Browse Source
* Json util code integration,remove the remote module json util. * update code checkstyle. * update code checkstyle. * add code checkstyle. * add test class. * update JSONUtils class. * update JSONUtils class.pull/3/MERGE
zhuangchong
4 years ago
committed by
GitHub
29 changed files with 402 additions and 310 deletions
@ -1,92 +0,0 @@ |
|||||||
/* |
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
* contributor license agreements. See the NOTICE file distributed with |
|
||||||
* this work for additional information regarding copyright ownership. |
|
||||||
* The ASF licenses this file to You 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. |
|
||||||
*/ |
|
||||||
package org.apache.dolphinscheduler.remote.utils; |
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException; |
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper; |
|
||||||
import org.slf4j.Logger; |
|
||||||
import org.slf4j.LoggerFactory; |
|
||||||
|
|
||||||
import java.io.IOException; |
|
||||||
import java.nio.charset.StandardCharsets; |
|
||||||
|
|
||||||
/** |
|
||||||
* json serialize or deserialize |
|
||||||
*/ |
|
||||||
public class JsonSerializer { |
|
||||||
private static final ObjectMapper objectMapper = new ObjectMapper(); |
|
||||||
private static final Logger logger = LoggerFactory.getLogger(JsonSerializer.class); |
|
||||||
|
|
||||||
private JsonSerializer(){ |
|
||||||
|
|
||||||
} |
|
||||||
/** |
|
||||||
* serialize to byte |
|
||||||
* |
|
||||||
* @param obj object |
|
||||||
* @param <T> object type |
|
||||||
* @return byte array |
|
||||||
*/ |
|
||||||
public static <T> byte[] serialize(T obj) { |
|
||||||
String json = ""; |
|
||||||
try { |
|
||||||
json = objectMapper.writeValueAsString(obj); |
|
||||||
} catch (JsonProcessingException e) { |
|
||||||
logger.error("serializeToString exception!", e); |
|
||||||
} |
|
||||||
|
|
||||||
return json.getBytes(Constants.UTF8); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* serialize to string |
|
||||||
* @param obj object |
|
||||||
* @param <T> object type |
|
||||||
* @return string |
|
||||||
*/ |
|
||||||
public static <T> String serializeToString(T obj) { |
|
||||||
String json = ""; |
|
||||||
try { |
|
||||||
json = objectMapper.writeValueAsString(obj); |
|
||||||
} catch (JsonProcessingException e) { |
|
||||||
logger.error("serializeToString exception!", e); |
|
||||||
} |
|
||||||
|
|
||||||
return json; |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* deserialize |
|
||||||
* |
|
||||||
* @param src byte array |
|
||||||
* @param clazz class
|
|
||||||
* @param <T> deserialize type |
|
||||||
* @return deserialize type |
|
||||||
*/ |
|
||||||
public static <T> T deserialize(byte[] src, Class<T> clazz) { |
|
||||||
|
|
||||||
String json = new String(src, StandardCharsets.UTF_8); |
|
||||||
try { |
|
||||||
return objectMapper.readValue(json, clazz); |
|
||||||
} catch (IOException e) { |
|
||||||
logger.error("deserialize exception!", e); |
|
||||||
return null; |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -1,58 +0,0 @@ |
|||||||
/* |
|
||||||
* Licensed to the Apache Software Foundation (ASF) under one or more |
|
||||||
* contributor license agreements. See the NOTICE file distributed with |
|
||||||
* this work for additional information regarding copyright ownership. |
|
||||||
* The ASF licenses this file to You 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. |
|
||||||
*/ |
|
||||||
|
|
||||||
package org.apache.dolphinscheduler.remote; |
|
||||||
|
|
||||||
|
|
||||||
import org.apache.dolphinscheduler.remote.utils.JsonSerializer; |
|
||||||
import org.junit.Assert; |
|
||||||
import org.junit.Test; |
|
||||||
|
|
||||||
public class JsonSerializerTest { |
|
||||||
|
|
||||||
@Test |
|
||||||
public void testSerialize(){ |
|
||||||
TestObj testObj = new TestObj(); |
|
||||||
testObj.setAge(12); |
|
||||||
byte[] serializeByte = JsonSerializer.serialize(testObj); |
|
||||||
|
|
||||||
//
|
|
||||||
TestObj deserialize = JsonSerializer.deserialize(serializeByte, TestObj.class); |
|
||||||
|
|
||||||
Assert.assertEquals(testObj.getAge(), deserialize.getAge()); |
|
||||||
} |
|
||||||
|
|
||||||
static class TestObj { |
|
||||||
|
|
||||||
private int age; |
|
||||||
|
|
||||||
public int getAge() { |
|
||||||
return age; |
|
||||||
} |
|
||||||
|
|
||||||
public void setAge(int age) { |
|
||||||
this.age = age; |
|
||||||
} |
|
||||||
|
|
||||||
@Override |
|
||||||
public String toString() { |
|
||||||
return "TestObj{" + |
|
||||||
"age=" + age + |
|
||||||
'}'; |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1,101 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.server.master.processor; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.dao.entity.TaskInstance; |
||||||
|
import org.apache.dolphinscheduler.remote.command.Command; |
||||||
|
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||||
|
import org.apache.dolphinscheduler.remote.command.TaskExecuteAckCommand; |
||||||
|
import org.apache.dolphinscheduler.server.master.cache.impl.TaskInstanceCacheManagerImpl; |
||||||
|
import org.apache.dolphinscheduler.server.master.processor.queue.TaskResponseEvent; |
||||||
|
import org.apache.dolphinscheduler.server.master.processor.queue.TaskResponseService; |
||||||
|
import org.apache.dolphinscheduler.service.bean.SpringApplicationContext; |
||||||
|
import org.apache.dolphinscheduler.service.process.ProcessService; |
||||||
|
|
||||||
|
import java.net.InetSocketAddress; |
||||||
|
import java.util.Date; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.junit.runner.RunWith; |
||||||
|
import org.mockito.Mockito; |
||||||
|
import org.powermock.api.mockito.PowerMockito; |
||||||
|
import org.powermock.core.classloader.annotations.PrepareForTest; |
||||||
|
import org.powermock.modules.junit4.PowerMockRunner; |
||||||
|
|
||||||
|
import io.netty.channel.Channel; |
||||||
|
|
||||||
|
/** |
||||||
|
* task ack processor test |
||||||
|
*/ |
||||||
|
@RunWith(PowerMockRunner.class) |
||||||
|
@PrepareForTest({SpringApplicationContext.class, TaskResponseEvent.class}) |
||||||
|
public class TaskAckProcessorTest { |
||||||
|
|
||||||
|
private TaskAckProcessor taskAckProcessor; |
||||||
|
private TaskResponseService taskResponseService; |
||||||
|
private TaskInstanceCacheManagerImpl taskInstanceCacheManager; |
||||||
|
private ProcessService processService; |
||||||
|
private TaskExecuteAckCommand taskExecuteAckCommand; |
||||||
|
private TaskResponseEvent taskResponseEvent; |
||||||
|
private Channel channel; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void before() { |
||||||
|
PowerMockito.mockStatic(SpringApplicationContext.class); |
||||||
|
|
||||||
|
taskResponseService = PowerMockito.mock(TaskResponseService.class); |
||||||
|
PowerMockito.when(SpringApplicationContext.getBean(TaskResponseService.class)).thenReturn(taskResponseService); |
||||||
|
|
||||||
|
taskInstanceCacheManager = PowerMockito.mock(TaskInstanceCacheManagerImpl.class); |
||||||
|
PowerMockito.when(SpringApplicationContext.getBean(TaskInstanceCacheManagerImpl.class)).thenReturn(taskInstanceCacheManager); |
||||||
|
|
||||||
|
processService = PowerMockito.mock(ProcessService.class); |
||||||
|
PowerMockito.when(SpringApplicationContext.getBean(ProcessService.class)).thenReturn(processService); |
||||||
|
|
||||||
|
taskAckProcessor = new TaskAckProcessor(); |
||||||
|
|
||||||
|
channel = PowerMockito.mock(Channel.class); |
||||||
|
taskResponseEvent = PowerMockito.mock(TaskResponseEvent.class); |
||||||
|
|
||||||
|
taskExecuteAckCommand = new TaskExecuteAckCommand(); |
||||||
|
taskExecuteAckCommand.setStatus(1); |
||||||
|
taskExecuteAckCommand.setExecutePath("/dolphinscheduler/worker"); |
||||||
|
taskExecuteAckCommand.setHost("localhost"); |
||||||
|
taskExecuteAckCommand.setLogPath("/temp/worker.log"); |
||||||
|
taskExecuteAckCommand.setStartTime(new Date()); |
||||||
|
taskExecuteAckCommand.setTaskInstanceId(1); |
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testProcess() { |
||||||
|
Command command = taskExecuteAckCommand.convert2Command(); |
||||||
|
Assert.assertEquals(CommandType.TASK_EXECUTE_ACK,command.getType()); |
||||||
|
InetSocketAddress socketAddress = new InetSocketAddress("localhost",12345); |
||||||
|
PowerMockito.when(channel.remoteAddress()).thenReturn(socketAddress); |
||||||
|
PowerMockito.mockStatic(TaskResponseEvent.class); |
||||||
|
|
||||||
|
PowerMockito.when(TaskResponseEvent.newAck(Mockito.any(), Mockito.any(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyInt())) |
||||||
|
.thenReturn(taskResponseEvent); |
||||||
|
TaskInstance taskInstance = PowerMockito.mock(TaskInstance.class); |
||||||
|
PowerMockito.when(processService.findTaskInstanceById(Mockito.any())).thenReturn(taskInstance); |
||||||
|
|
||||||
|
taskAckProcessor.process(channel,command); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,64 @@ |
|||||||
|
/* |
||||||
|
* Licensed to the Apache Software Foundation (ASF) under one or more |
||||||
|
* contributor license agreements. See the NOTICE file distributed with |
||||||
|
* this work for additional information regarding copyright ownership. |
||||||
|
* The ASF licenses this file to You 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. |
||||||
|
*/ |
||||||
|
|
||||||
|
package org.apache.dolphinscheduler.server.master.processor; |
||||||
|
|
||||||
|
import org.apache.dolphinscheduler.remote.command.Command; |
||||||
|
import org.apache.dolphinscheduler.remote.command.CommandType; |
||||||
|
import org.apache.dolphinscheduler.remote.command.TaskKillResponseCommand; |
||||||
|
|
||||||
|
import java.util.ArrayList; |
||||||
|
|
||||||
|
import org.junit.Assert; |
||||||
|
import org.junit.Before; |
||||||
|
import org.junit.Test; |
||||||
|
import org.powermock.api.mockito.PowerMockito; |
||||||
|
|
||||||
|
import io.netty.channel.Channel; |
||||||
|
|
||||||
|
/** |
||||||
|
* task response processor test |
||||||
|
*/ |
||||||
|
public class TaskKillResponseProcessorTest { |
||||||
|
|
||||||
|
private TaskKillResponseProcessor taskKillResponseProcessor; |
||||||
|
|
||||||
|
private TaskKillResponseCommand taskKillResponseCommand; |
||||||
|
|
||||||
|
private Channel channel; |
||||||
|
|
||||||
|
@Before |
||||||
|
public void before() { |
||||||
|
taskKillResponseProcessor = new TaskKillResponseProcessor(); |
||||||
|
channel = PowerMockito.mock(Channel.class); |
||||||
|
taskKillResponseCommand = new TaskKillResponseCommand(); |
||||||
|
taskKillResponseCommand.setAppIds( |
||||||
|
new ArrayList<String>() {{ add("task_1"); }}); |
||||||
|
taskKillResponseCommand.setHost("localhost"); |
||||||
|
taskKillResponseCommand.setProcessId(1); |
||||||
|
taskKillResponseCommand.setStatus(1); |
||||||
|
taskKillResponseCommand.setTaskInstanceId(1); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
@Test |
||||||
|
public void testProcess() { |
||||||
|
Command command = taskKillResponseCommand.convert2Command(); |
||||||
|
Assert.assertEquals(CommandType.TASK_KILL_RESPONSE,command.getType()); |
||||||
|
taskKillResponseProcessor.process(channel,command); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue