Add unit test 23/37323/2
authoryangyan <yangyanyj@chinamobile.com>
Wed, 21 Mar 2018 05:14:34 +0000 (13:14 +0800)
committeryangyan <yangyanyj@chinamobile.com>
Wed, 21 Mar 2018 05:22:54 +0000 (13:22 +0800)
Issue-ID: VFC-836
Change-Id: I02e1c0aae643b16fdb88981d6973e72021e0125c
Signed-off-by: yangyan <yangyanyj@chinamobile.com>
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThread.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MessageUtil.java
ems/boco/src/main/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgType.java
ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/AlarmTaskThreadTest.java
ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/HeartBeatTest.java [new file with mode: 0644]
ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/collector/alarm/MsgTest.java [new file with mode: 0644]
ems/boco/src/test/java/org/onap/vfc/nfvo/emsdriver/taskscheduler/CollectOderJobTest.java [new file with mode: 0644]

index 154401a..6c6cebe 100644 (file)
@@ -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();
index 21c6246..fc5cd71 100644 (file)
@@ -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) {
index f022e99..4762d8b 100644 (file)
@@ -41,7 +41,6 @@ public enum MsgType {
     }
 
     public static MsgType getMsgTypeValue(int msgTypeValue) {
-
         for (MsgType msgType : MsgType.values()) {
             if (msgType.value == msgTypeValue) {
                 return msgType;
index 24635e1..fc6c755 100644 (file)
  */
 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 (file)
index 0000000..301d876
--- /dev/null
@@ -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 (file)
index 0000000..6e24720
--- /dev/null
@@ -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 (file)
index 0000000..b7847b4
--- /dev/null
@@ -0,0 +1,54 @@
+/*\r
+ * Copyright 2017 BOCO Corporation.  CMCC Technologies Co., Ltd\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package org.onap.vfc.nfvo.emsdriver.taskscheduler;\r
+\r
+import org.junit.Test;\r
+import org.onap.vfc.nfvo.emsdriver.commons.model.CollectVo;\r
+import org.quartz.Job;\r
+\r
+public class CollectOderJobTest {\r
+       \r
+       @Test\r
+       public void executeCollectOderJob(){\r
+               CollectVo collectVo = new CollectVo();\r
+               collectVo.setEmsName("zteEms");\r
+               collectVo.setType("ems-p");\r
+               collectVo.setIP("127.0.0.1");\r
+               collectVo.setCrontab("*/5 * * * * ?");\r
+               \r
+                 String jobName = collectVo.getEmsName() + "_" + collectVo.getType() + collectVo.getIP();\r
+          Job job = new CollectOderJob();\r
+          String jobClass = job.getClass().getName();\r
+          String time = collectVo.getCrontab();\r
+          if (time != null && !"".equals(time)) {\r
+              QuartzManager.addJob(jobName, jobClass, time, collectVo);\r
+              try {\r
+                               Thread.sleep(10);\r
+                       } catch (InterruptedException e) {\r
+                               // TODO Auto-generated catch block\r
+                               e.printStackTrace();\r
+                       } finally{\r
+                     QuartzManager.removeJob(jobName);                         \r
+                       }\r
+          } else {\r
+              System.out.println("type =[" + collectVo.getType() + "]ip=[" + collectVo.getIP() + "] crontab is null");\r
+          }\r
+               \r
+       }\r
+\r
+       \r
+}\r