improve sonar coverage for uui-server 95/41895/1
authorzhangab <zhanganbing@chinamobile.com>
Tue, 10 Apr 2018 07:28:07 +0000 (15:28 +0800)
committerzhangab <zhanganbing@chinamobile.com>
Tue, 10 Apr 2018 07:28:12 +0000 (15:28 +0800)
Change-Id: Ic61b63494a9a96e0058b22cf8c3f126854b079dd
Issue-ID: USECASEUI-103
Signed-off-by: zhangab <zhanganbing@chinamobile.com>
server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsInformationServiceImpl.java
server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImplTest.java
server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsInformationServiceImplTest.java

index cf5a3ee..c099e1f 100755 (executable)
@@ -243,13 +243,7 @@ public class AlarmsInformationServiceImpl implements AlarmsInformationService {
                        return list;\r
                }catch (Exception e){\r
                        logger.error("exception occurred while performing PerformanceInformationServiceImpl queryDateBetween. LIST:" + e.getMessage());\r
-\r
                        return null;\r
                }\r
-\r
-\r
-\r
        }\r
-\r
-\r
 }\r
index 27d40e0..a3fc719 100644 (file)
@@ -32,7 +32,6 @@ import org.springframework.test.context.web.WebAppConfiguration;
 import java.sql.Timestamp;
 import java.text.SimpleDateFormat;
 import java.util.*;
-
 import java.io.*;
 import org.hibernate.Query;
 import org.hibernate.Session;
@@ -98,6 +97,10 @@ public class AlarmsHeaderServiceImplTest {
                                AlarmsHeader ah = new AlarmsHeader();
                                return Arrays.asList(ah);
                        }
+                       @Mock
+                       public Object uniqueResult() {
+                               return "0";
+                       }
                };
                MockUp<Session> mockedSession = new MockUp<Session>() {
                        @Mock
@@ -154,12 +157,6 @@ public class AlarmsHeaderServiceImplTest {
 
        @Test
        public void testGetStatusBySourceName() throws Exception {
-               new MockUp<Query>() {
-                       @Mock
-                       public Object uniqueResult() {
-                               return "active";
-                       }
-               };
                alarmsHeaderServiceImpl.getStatusBySourceName("sourceName");
        }
 
@@ -176,12 +173,6 @@ public class AlarmsHeaderServiceImplTest {
 
        @Test
        public void testGetAllCountByStatus() throws Exception {
-               new MockUp<Query>() {
-                       @Mock
-                       public Object uniqueResult() {
-                               return "1";
-                       }
-               };
                alarmsHeaderServiceImpl.getAllCountByStatus("status");
        }
 
@@ -197,24 +188,12 @@ public class AlarmsHeaderServiceImplTest {
 
        @Test
        public void testGetAllByDatetime() throws Exception {
-               new MockUp<Query>() {
-                       @Mock
-                       public Object uniqueResult() {
-                               return "1";
-                       }
-               };
                alarmsHeaderServiceImpl.getAllCountByStatus("status");
                alarmsHeaderServiceImpl.getAllByDatetime("status", "eventId", "eventServrity", "createTime");
        }
 
        @Test
        public void testGetAllCount() throws Exception {
-               new MockUp<Query>() {
-                       @Mock
-                       public Object uniqueResult() {
-                               return "1";
-                       }
-               };
                AlarmsHeader ah = new AlarmsHeader();
                ah.setVersion("va2");
                ah.setEventName("a");
index 976935d..0993f67 100644 (file)
  */
 package org.onap.usecaseui.server.service.impl;
 
-import org.hibernate.Query;
-import org.hibernate.Session;
-import org.hibernate.SessionFactory;
-import org.hibernate.Transaction;
-
-import mockit.Mock;
-import mockit.MockUp;
-
 import org.junit.Test; 
 import org.junit.Before; 
 import org.junit.After;
 import org.junit.runner.RunWith;
 import org.onap.usecaseui.server.UsecaseuiServerApplication;
 import org.onap.usecaseui.server.bean.AlarmsInformation;
-import org.onap.usecaseui.server.service.AlarmsInformationService;
+import org.onap.usecaseui.server.service.impl.AlarmsInformationServiceImpl;
 import org.onap.usecaseui.server.util.DateUtils;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 import org.springframework.test.context.junit4.SpringRunner;
 import org.springframework.test.context.web.WebAppConfiguration;
-
 import javax.annotation.Resource;
 
-import static org.mockito.Mockito.*;
+import java.sql.Timestamp;
+import java.text.SimpleDateFormat;
+import java.util.*;
+import java.io.*;
+import org.hibernate.Query;
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+
+import mockit.Mock;
+import mockit.MockUp;
 
 /** 
 * AlarmsInformationServiceImpl Tester. 
@@ -47,38 +48,83 @@ import static org.mockito.Mockito.*;
 * @since <pre> 8, 2018</pre>
 * @version 1.0 
 */
-
 public class AlarmsInformationServiceImplTest {
 
-    /*@Resource(name = "AlarmsInformationService")
-    AlarmsInformationService alarmsInformationService;*/
-    AlarmsInformationServiceImpl service;
-@Before
-public void before() throws Exception {
-    service = mock(AlarmsInformationServiceImpl.class);
-} 
-
-@After
-public void after() throws Exception { 
-} 
-
-private Session session;
-private Transaction transaction;
-private Query query;
-/**
- * mockupUtil 
- */
-public void mockupUtil(){
-        MockUp<Query> mockUpQuery = new MockUp<Query>() {
-     };
+       AlarmsInformationServiceImpl alarmsInformationServiceImpl = null;
+       private static final long serialVersionUID = 1L;
+
+       @Before
+       public void before() throws Exception {
+               alarmsInformationServiceImpl = new AlarmsInformationServiceImpl();
+
+               MockUp<Transaction> mockUpTransaction = new MockUp<Transaction>() {
+                       @Mock
+                       public void commit() {
+                       }
+               };
+               MockUp<Query> mockUpQuery = new MockUp<Query>() {
+               };
+               new MockUp<Query>() {
+                       @Mock
+                       public Query setString(String name, String value) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public Query setDate(String name, Date value) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public Query setInteger(String name, int value) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public int executeUpdate() {
+                               return 0;
+                       }
+                       @Mock
+                       public Query setMaxResults(int value) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public Query setFirstResult(int firstResult) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public Query setParameterList(String name, Object[] values) {
+                               return mockUpQuery.getMockInstance();
+                       }
+                       @Mock
+                       public List<AlarmsInformation> list() {
+                               AlarmsInformation ai = new AlarmsInformation();
+                               return Arrays.asList(ai);
+                       }
+                       @Mock
+                       public Object uniqueResult() {
+                               return "0";
+                       }
+               };
                MockUp<Session> mockedSession = new MockUp<Session>() {
-         @Mock
-         public Query createQuery(String sql) {
-             return mockUpQuery.getMockInstance();
-         }
+                       @Mock
+                       public Query createQuery(String sql) {
+                               return mockUpQuery.getMockInstance();
+                       }
                        @Mock
                        public Transaction beginTransaction() {
-                               return transaction;
+                               return mockUpTransaction.getMockInstance();
+                       }
+                       @Mock
+                       public Transaction getTransaction() {
+                               return mockUpTransaction.getMockInstance();
+                       }
+                       @Mock
+                       public Serializable save(Object object) {
+                               return (Serializable) serialVersionUID;
+                       }
+                       @Mock
+                       public void flush() {
+                       }
+                       @Mock
+                       public void update(Object object) {
                        }
                };
                new MockUp<SessionFactory>() {
@@ -87,111 +133,163 @@ public void mockupUtil(){
                                return mockedSession.getMockInstance();
                        }
                };
-               new MockUp<Transaction>() {
+               new MockUp<AlarmsInformationServiceImpl>() {
                        @Mock
-                       public void commit() {
+                       private Session getSession() {
+                               return mockedSession.getMockInstance();
+                       }
+                       @Mock
+                       private int getAllCount(AlarmsInformation alarmsInformation, int currentPage, int pageSize) {
+                               return 10;
                        }
                };
-     new MockUp<AlarmsInformationServiceImpl>() {
-         @Mock
-         private Session getSession() {
-             return mockedSession.getMockInstance();
-         }
-     };
-}
-/** 
-* 
-* Method: saveAlarmsInformation(AlarmsInformation alarmsInformation) 
-* 
-*/ 
-@Test
-public void testSaveAlarmsInformation() throws Exception { 
-//TODO: Test goes here...
-    AlarmsInformation a = new AlarmsInformation();
-    a.setEventId("1119");
-    a.setName("efw");
-    a.setValue("fre");
-    a.setCreateTime(DateUtils.now());
-    a.setUpdateTime(DateUtils.now());
-    mockupUtil();
-    service.saveAlarmsInformation(a);
-} 
+       }
 
-/** 
-* 
-* Method: updateAlarmsInformation(AlarmsInformation alarmsInformation) 
-* 
-*/ 
-@Test
-public void testUpdateAlarmsInformation() throws Exception { 
-//TODO: Test goes here...
-    AlarmsInformation a = new AlarmsInformation();
-    a.setEventId("110");
-    a.setName("1");
-    a.setValue("fko");
-    a.setUpdateTime(DateUtils.now());
-    a.setCreateTime(DateUtils.now());
-    mockupUtil();
-    service.updateAlarmsInformation(a);
-} 
+       @After
+       public void after() throws Exception {
+       }
 
-/** 
-* 
-* Method: getAllCount(AlarmsInformation alarmsInformation, int currentPage, int pageSize) 
-* 
-*/ 
-@Test
-public void testGetAllCount() throws Exception { 
-//TODO: Test goes here...
-    AlarmsInformation larmsInformation = new AlarmsInformation();
-    larmsInformation.setName("vnf_a_3");
+       @Test
+       public void testSaveAlarmsInformation() throws Exception {
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.saveAlarmsInformation(ai);
+       }
 
-    mockupUtil();
-    service.getAllCount(larmsInformation,0,12);
-} 
+       @Test
+       public void testUpdateAlarmsInformation() throws Exception {
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.updateAlarmsInformation(ai);
+       }
 
-/** 
-* 
-* Method: queryAlarmsInformation(AlarmsInformation alarmsInformation, int currentPage, int pageSize) 
-* 
-*/ 
-@Test
-public void testQueryAlarmsInformation() throws Exception { 
-//TODO: Test goes here...
-    AlarmsInformation a = new AlarmsInformation();
-    a.setEventId("110");
-    mockupUtil();
-    service.queryAlarmsInformation(a,1,100);
-         //   .getList().forEach( al -> System.out.println(al.getEventId()));
-} 
+       @Test
+       public void testGetAllCount() throws Exception {
+               AlarmsInformation ai = new AlarmsInformation();
+               ai.setName("name");
+               ai.setValue("value");
+               ai.setEventId("eventId");
+               ai.setCreateTime(DateUtils.now());
+               ai.setUpdateTime(DateUtils.now());
+               alarmsInformationServiceImpl.getAllCount(ai, 1, 10);
+       }
 
-/** 
-* 
-* Method: queryId(String[] id) 
-* 
-*/ 
-@Test
-public void testQueryId() throws Exception { 
-//TODO: Test goes here...
-       mockupUtil();
-    service.queryId(new String[]{"110"});
-            //.forEach(ai -> System.out.println(ai));
-} 
+       @Test
+       public void testQueryAlarmsInformation() throws Exception {
+               AlarmsInformation ai = new AlarmsInformation();
+               ai.setName("name");
+               ai.setValue("value");
+               ai.setEventId("eventId");
+               ai.setCreateTime(DateUtils.now());
+               ai.setUpdateTime(DateUtils.now());
+               alarmsInformationServiceImpl.queryAlarmsInformation(ai, 1, 10);
+       }
 
-/** 
-* 
-* Method: queryDateBetween(String sourceId, String startTime, String endTime) 
-* 
-*/ 
-@Test
-public void testQueryDateBetween() throws Exception { 
-//TODO: Test goes here...
-       mockupUtil();
-    service.queryDateBetween("MME40","","");
-            //.forEach( in -> {
-       // System.out.println(in);
-   // });
-} 
-
-
-} 
+       @Test
+       public void testQueryId() throws Exception {
+               String[] id = {};
+               alarmsInformationServiceImpl.queryId(id);
+       }
+
+       @Test
+       public void testQueryDateBetween() throws Exception {
+               new MockUp<List>() {
+                       @Mock
+                       private Iterator iterator() {
+                               String[] strlist1 = {"time1", "count1"};
+                               String[] strlist2 = {"time2", "count2"};
+                               List<String[]> list = new ArrayList<String[]>();
+                               list.add(strlist1);
+                               list.add(strlist2);
+                               return list.iterator();
+                       }
+               };
+               alarmsInformationServiceImpl.queryDateBetween("sourceId", "startTime", "endTime");
+       }
+
+       @Test
+       public void testGetAllAlarmsInformationByeventId() throws Exception {
+               alarmsInformationServiceImpl.getAllAlarmsInformationByeventId("eventId");
+       }
+
+       @Test(expected = Exception.class)
+       public void testSaveAlarmsInformationException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.saveAlarmsInformation(ai);
+       }
+
+       @Test(expected = Exception.class)
+       public void testUpdateAlarmsInformationException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.updateAlarmsInformation(ai);
+       }
+
+       @Test(expected = Exception.class)
+       public void testGetAllCountException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.getAllCount(ai, 1, 10);
+       }
+
+       @Test(expected = Exception.class)
+       public void testQueryAlarmsInformationException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               AlarmsInformation ai = null;
+               alarmsInformationServiceImpl.queryAlarmsInformation(ai, 1, 10);
+       }
+
+       @Test(expected = Exception.class)
+       public void testQueryIdException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               String[] id = {};
+               alarmsInformationServiceImpl.queryId(id);
+       }
+
+       @Test(expected = Exception.class)
+       public void testQueryDateBetweenException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               alarmsInformationServiceImpl.queryDateBetween("sourceId", "startTime", "endTime");
+       }
+
+       @Test(expected = Exception.class)
+       public void testGetAllAlarmsInformationByeventIdException() throws Exception {
+               new MockUp<AlarmsInformationServiceImpl>() {
+                       @Mock
+                       private Session getSession() throws Exception {
+                               throw new Exception();
+                       }
+               };
+               alarmsInformationServiceImpl.getAllAlarmsInformationByeventId("eventId");
+       }
+
+}