diff options
author | yangyan <yangyanyj@chinamobile.com> | 2018-03-21 13:14:34 +0800 |
---|---|---|
committer | yangyan <yangyanyj@chinamobile.com> | 2018-03-21 13:22:54 +0800 |
commit | 844b0ab933e484c3c3367ad89481c7dc1f6a6565 (patch) | |
tree | 62527d38edcda53452ecd3dd9a5785723c1c6511 | |
parent | cae4110224a71f7678a13231c9050939c5d73f98 (diff) |
Add unit test
Issue-ID: VFC-836
Change-Id: I02e1c0aae643b16fdb88981d6973e72021e0125c
Signed-off-by: yangyan <yangyanyj@chinamobile.com>
7 files changed, 200 insertions, 22 deletions
diff --git a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java index 154401a..6c6cebe 100644 --- a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java +++ b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java @@ -60,7 +60,7 @@ public class AlarmTaskThread extends Thread { @Override public void run() { try { - alarmChannel = MessageChannelFactory.getMessageChannel(Constant.RESULT_CHANNEL_KEY); + alarmChannel = MessageChannelFactory.getMessageChannel(Constant.RESULT_CHANNEL_KEY); this.init(); while (!this.isStop) { String body; @@ -79,10 +79,9 @@ public class AlarmTaskThread extends Thread { public String receive() throws IOException { - try{ + try{ Msg msg = null; String retString = null; - while (retString == null && !this.isStop) { msg = MessageUtil.readOneMsg(is); log.debug("msg = " + msg.toString(true)); @@ -91,10 +90,8 @@ public class AlarmTaskThread extends Thread { log.debug("receive login ack"); boolean suc = this.ackLoginAlarm(msg); if (suc) { - if (reqId == Integer.MAX_VALUE) - reqId=0; - + reqId=0; reqId++; Msg msgheart = MessageUtil.putHeartBeatMsg(reqId); heartBeat = new HeartBeat(socket, msgheart); @@ -115,7 +112,6 @@ public class AlarmTaskThread extends Thread { log.debug("received alarm message"); retString = msg.getBody(); } - if (retString == null) { Thread.sleep(100); } @@ -224,11 +220,9 @@ public class AlarmTaskThread extends Thread { } public void close() { - if (heartBeat != null) { heartBeat.setStop(true); } - if (is != null) { try { is.close(); @@ -238,7 +232,6 @@ public class AlarmTaskThread extends Thread { is = null; } } - if (dos != null) { try { dos.close(); @@ -248,7 +241,6 @@ public class AlarmTaskThread extends Thread { dos = null; } } - if (socket != null) { try { socket.close(); diff --git a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java index 21c6246..fc5cd71 100644 --- a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java +++ b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java @@ -30,8 +30,7 @@ public class MessageUtil { public static Msg putLoginFtp(String user, String passwd) { String body = String.format(Msg.REQ_LOGIN_ALARM, user, passwd, "ftp"); - return new Msg(body, MsgType.REQ_LOGIN_ALARM); - + return new Msg(body, MsgType.REQ_LOGIN_ALARM); } public static Msg putSyncMsg(int reqId, int alarmSeq) { @@ -68,7 +67,6 @@ public class MessageUtil { public static Msg readOneMsg(BufferedInputStream is) throws IOException { byte[] inputB = new byte[9]; - Msg msg = new Msg(); try( DataInputStream dis = new DataInputStream(is); @@ -92,7 +90,6 @@ public class MessageUtil { } catch (Exception e) { throw new IOException("readOneMsg",e); } - return msg; } @@ -104,9 +101,7 @@ public class MessageUtil { oos.writeByte(msg.getMsgType().value); oos.writeInt(Msg.creatMsgTimeStamp()); oos.writeShort(msg.getBodyLenNow()); - dout.write(byteOutStream.toByteArray()); - dout.write(msg.getBodyBytes()); dout.flush(); } catch (Exception e) { diff --git a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java index f022e99..4762d8b 100644 --- a/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java +++ b/ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java @@ -41,7 +41,6 @@ public enum MsgType { } public static MsgType getMsgTypeValue(int msgTypeValue) { - for (MsgType msgType : MsgType.values()) { if (msgType.value == msgTypeValue) { return msgType; diff --git a/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThreadTest.java b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThreadTest.java index 24635e1..fc6c755 100644 --- a/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThreadTest.java +++ b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThreadTest.java @@ -15,13 +15,13 @@ */ package org.onap.vfc.nfvo.emsdriver.collector.alarm; -import org.junit.Before; -import org.junit.Test; -import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo; +import static org.junit.Assert.assertNotNull; import java.io.IOException; -import static org.junit.Assert.assertNotNull; +import org.junit.Before; +import org.junit.Test; +import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo; public class AlarmTaskThreadTest { @@ -75,4 +75,18 @@ public class AlarmTaskThreadTest { assertNotNull(alarm); } + @Test + public void runAlarmTaskThread(){ + try { + taskThread.run(); + Thread.sleep(3000); + taskThread.setStop(true); + server.stop(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + } diff --git a/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/HeartBeatTest.java b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/HeartBeatTest.java new file mode 100644 index 0000000..301d876 --- /dev/null +++ b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/HeartBeatTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd + * + * 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. + */ +package org.onap.vfc.nfvo.emsdriver.collector.alarm; + +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; + +import org.junit.Before; +import org.junit.Test; + +public class HeartBeatTest { + + private AlarmSocketServer server; + + @Before + public void setUp() throws IOException { + new Thread() { + public void run() { + server = new AlarmSocketServer(); + server.socketServer(); + } + }.start(); + + } + + @Test + public void runHeartBeatThread(){ + try { + Socket socket = new Socket("127.0.0.1",12345); + Msg msg = MessageUtil.putHeartBeatMsg(1); + HeartBeat heartBeat = new HeartBeat(socket,msg); + heartBeat.run(); + Thread.sleep(10); + heartBeat.setStop(true); + server.stop(); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + +} diff --git a/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgTest.java b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgTest.java new file mode 100644 index 0000000..6e24720 --- /dev/null +++ b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgTest.java @@ -0,0 +1,60 @@ +/* + * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd + * + * 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. + */ +package org.onap.vfc.nfvo.emsdriver.collector.alarm; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.UnsupportedEncodingException; +import java.util.Date; + +import org.junit.Test; + + +public class MsgTest { + + @Test + public void newBodyfromBytesTest() { + try { + Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM); + String str = "test"; + msg.newBodyfromBytes(str.getBytes()); + } catch (UnsupportedEncodingException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + @Test + public void setTimestampTest() { + Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM); + Date date = new Date(); + int secondes = date.getSeconds(); + msg.setTimeStamp(secondes); + int timestamp = msg.getTimeStamp(); + assertEquals(secondes,timestamp); + } + + @Test + public void toStringTest(){ + Msg msg = new Msg("Alarm",MsgType.REALTIME_ALARM); + msg.setBody("NewAlarm"); + String msgStr = msg.toString(); + System.out.println("msg to string is "+msgStr); + assertNotNull(msgStr); + } + +} diff --git a/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/taskscheduler/CollectOderJobTest.java b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/taskscheduler/CollectOderJobTest.java new file mode 100644 index 0000000..b7847b4 --- /dev/null +++ b/ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/taskscheduler/CollectOderJobTest.java @@ -0,0 +1,54 @@ +/*
+ * Copyright 2017 BOCO Corporation. CMCC Technologies Co., Ltd
+ *
+ * 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.
+ */
+
+package org.onap.vfc.nfvo.emsdriver.taskscheduler;
+
+import org.junit.Test;
+import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;
+import org.quartz.Job;
+
+public class CollectOderJobTest {
+
+ @Test
+ public void executeCollectOderJob(){
+ CollectVo collectVo = new CollectVo();
+ collectVo.setEmsName("zteEms");
+ collectVo.setType("ems-p");
+ collectVo.setIP("127.0.0.1");
+ collectVo.setCrontab("*/5 * * * * ?");
+
+ String jobName = collectVo.getEmsName() + "_" + collectVo.getType() + collectVo.getIP();
+ Job job = new CollectOderJob();
+ String jobClass = job.getClass().getName();
+ String time = collectVo.getCrontab();
+ if (time != null && !"".equals(time)) {
+ QuartzManager.addJob(jobName, jobClass, time, collectVo);
+ try {
+ Thread.sleep(10);
+ } catch (InterruptedException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally{
+ QuartzManager.removeJob(jobName);
+ }
+ } else {
+ System.out.println("type =[" + collectVo.getType() + "]ip=[" + collectVo.getIP() + "] crontab is null");
+ }
+
+ }
+
+
+}
|