diff options
author | zhangab <zhanganbing@chinamobile.com> | 2018-04-03 13:59:32 +0800 |
---|---|---|
committer | zhangab <zhanganbing@chinamobile.com> | 2018-04-03 13:59:37 +0800 |
commit | bf5bf9cb0e1d73bb20a5ea6561246abd13024723 (patch) | |
tree | 981dad9b238a8e2bbc58ec190f25d1e5047e51f1 | |
parent | e1b104e7cab8c07992c2c5bf0e2d95c0dc2356aa (diff) |
improve sonar coverage for uui-server
Change-Id: I5ec5ffe50aac44d33049bf83678d207ebaf01b10
Issue-ID: USECASEUI-103
Signed-off-by: zhangab <zhanganbing@chinamobile.com>
2 files changed, 96 insertions, 260 deletions
diff --git a/server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImpl.java b/server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImpl.java index c0b1fd98..530a609e 100755 --- a/server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImpl.java +++ b/server/src/main/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImpl.java @@ -72,7 +72,7 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { try(Session session = getSession()){
logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}");
- session.beginTransaction();
+ Transaction tx = session.beginTransaction();
Query q=session.createQuery("update AlarmsHeader set status=:status, updateTime=:date, startEpochMicrosecCleared=:startEpochMicrosecCleared ,lastEpochMicroSecCleared=:lastEpochMicroSecCleared where eventName=:eventName and reportingEntityName=:reportingEntityName and specificProblem =:specificProblem");
q.setString("status",status);
@@ -83,7 +83,8 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { q.setString("reportingEntityName",reportingEntityName);
q.setString("specificProblem",specificProblem);
q.executeUpdate();
- session.getTransaction().commit();
+ tx = session.getTransaction();
+ tx.commit();
session.flush();
return "1";
} catch (Exception e) {
diff --git a/server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImplTest.java b/server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImplTest.java index 82b21553..7a783497 100644 --- a/server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImplTest.java +++ b/server/src/test/java/org/onap/usecaseui/server/service/impl/AlarmsHeaderServiceImplTest.java @@ -1,4 +1,4 @@ -/* +/** * Copyright (C) 2017 CMCC, Inc. and others. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.onap.usecaseui.server.service.impl; +package org.onap.usecaseui.server.service.impl; import org.junit.Test; import org.junit.Before; @@ -21,7 +21,7 @@ import org.junit.After; import org.junit.runner.RunWith; import org.onap.usecaseui.server.UsecaseuiServerApplication; import org.onap.usecaseui.server.bean.AlarmsHeader; -import org.onap.usecaseui.server.service.AlarmsHeaderService; +import org.onap.usecaseui.server.service.impl.AlarmsHeaderServiceImpl; import org.onap.usecaseui.server.util.DateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -29,11 +29,16 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import java.sql.Timestamp; -import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; -import static org.mockito.Mockito.mock; +import org.hibernate.Query; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +import mockit.Mock; +import mockit.MockUp; /** * AlarmsHeaderServiceImpl Tester. @@ -42,264 +47,94 @@ import static org.mockito.Mockito.mock; * @since <pre>8, 2018</pre> * @version 1.0 */ - public class AlarmsHeaderServiceImplTest { - /* @Autowired - private AlarmsHeaderService alarmsHeaderService; -*/ - AlarmsHeaderServiceImpl service; -@Before -public void before() throws Exception { - service = mock(AlarmsHeaderServiceImpl.class); -} - -@After -public void after() throws Exception { -} - -/** -* -* Method: saveAlarmsHeader(AlarmsHeader alarmsHeader) -* -*/ - -@Test -public void testGetAllCountByStatus(){ - String status="active"; - service.getAllCountByStatus(status); -} - - -public void testGetAllByStatus() throws ParseException { - String status="0"; - String eventName="Fault_MultiCloud_VMFailureCleared"; - String sourceName="shentao-test-2002"; - String eventServerity="CRITICAL"; - String reportingEntityName="Multi-Cloud"; - String createTime_s="2017-10-31 09:51:15"; - String endTime_s="2018-03-15 00:00:00"; - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - Date createTime=formatter.parse(createTime_s); - Date endTime=formatter.parse(endTime_s); - service.getAllByStatus(status,eventName,sourceName,eventServerity,reportingEntityName,createTime,endTime); -} - -@Test -public void testGetAlarmsHeaderDetail(){ + AlarmsHeaderServiceImpl alarmsHeaderServiceImpl = null; + + @Before + public void before() throws Exception { + alarmsHeaderServiceImpl = new AlarmsHeaderServiceImpl(); + MockUp<Query> mockUpQuery = new MockUp<Query>() { + }; + MockUp<Session> mockedSession = new MockUp<Session>() { + @Mock + public Query createQuery(String sql) { + return mockUpQuery.getMockInstance(); + } + @Mock + public Transaction beginTransaction() { + return transaction; + } + @Mock + public Object save(Object object) { + return null; + } + @Mock + public void flush() { + } + }; + new MockUp<SessionFactory>() { + @Mock + public Session openSession() { + return mockedSession.getMockInstance(); + } + }; + new MockUp<Transaction>() { + @Mock + public void commit() { + } + }; + new MockUp<AlarmsHeaderServiceImpl>() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + } + + @After + public void after() throws Exception { + } + + private Session session; + private Transaction transaction; + private Query query; + + @Test + public void testSaveAlarmsHeader() throws Exception { + AlarmsHeader ah = new AlarmsHeader(); + ah.setEventName("a"); + ah.setStatus("1"); + ah.setVfStatus("1"); + ah.setEventId("1119"); + ah.setDomain("asb"); + ah.setEventCategory("s"); + ah.setAlarmCondition("ea"); + ah.setAlarmInterfaceA("cs"); + ah.setCreateTime(DateUtils.now()); + ah.setEventServrity("s"); + ah.setEventSourceType("q"); + ah.setEventType("q"); + ah.setFaultFieldsVersion("v1"); + ah.setLastEpochMicroSec("csa"); + ah.setNfcNamingCode("std"); + ah.setNfNamingCode("cout"); + ah.setPriority("cs"); + ah.setReportingEntityId("112"); + ah.setReportingEntityName("asfs"); + ah.setSequence("cgg"); + ah.setSourceId("123"); + ah.setSourceName("eggs"); + ah.setSpecificProblem("especially"); + ah.setStartEpochMicrosec("wallet"); + ah.setUpdateTime(DateUtils.now()); + ah.setVersion("va2"); + + alarmsHeaderServiceImpl.saveAlarmsHeader(ah); + } - int id=5; - service.getAlarmsHeaderDetail(id); -} -@Test -public void testGetAllByDatetime(){ - String status="active"; - String eventId="ab305d54-85b4-a31b-7db2-fb6b9e546015"; - String eventServerity="CRITICAL"; - String createTime="2017-10-31"; - service.getAllByDatetime(status,eventId,eventServerity,createTime); -} -@Test -public void testSaveAlarmsHeader() throws Exception { -//TODO: Test goes here... - AlarmsHeader a = new AlarmsHeader(); - a.setEventName("a"); - a.setStatus("1"); - a.setVfStatus("1"); - a.setEventId("1119"); - a.setDomain("asb"); - a.setEventCategory("s"); - a.setAlarmCondition("ea"); - a.setAlarmInterfaceA("cs"); - a.setCreateTime(DateUtils.now()); - a.setEventServrity("s"); - a.setEventSourceType("q"); - a.setEventType("q"); - a.setFaultFieldsVersion("v1"); - a.setLastEpochMicroSec("csa"); - a.setNfcNamingCode("std"); - a.setNfNamingCode("cout"); - a.setPriority("cs"); - a.setReportingEntityId("112"); - a.setReportingEntityName("asfs"); - a.setSequence("cgg"); - a.setSourceId("123"); - a.setSourceName("eggs"); - a.setSpecificProblem("especially"); - a.setStartEpochMicrosec("wallet"); - a.setUpdateTime(DateUtils.now()); - a.setVersion("va2"); - service.saveAlarmsHeader(a); - //System.out.println(alarmsHeaderService.saveAlarmsHeader(a)); -} - -/** -* -* Method: updateAlarmsHeader2018(String status, String date, String eventNameCleared, String eventName, String reportingEntityName, String specificProblem) -* -*/ -@Test -public void testUpdateAlarmsHeader2018() throws Exception { -//TODO: Test goes here... - //Date date = new Date(); - // Date date = new Date("2018-02-28 15:25:39"); - //Date date = new Date(); - //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); - // String date_gets = dateFormat.format( new Date() ); - //Date date_get = new Date(); - //Date date_get = dateFormat.parse(date_gets); - Long l = System.currentTimeMillis(); - - Timestamp date_get = new Timestamp(l); //2013-01-14 22:45:36.484 - service.updateAlarmsHeader2018("active",date_get,"112","112","Fault_MultiCloud_VMFailureCleared","Multi-Cloud","Fault_MultiCloud_VMFailure"); - - -} - -/** -* -* Method: getStatusBySourceName(String sourceName) -* -*/ -@Test -public void testGetStatusBySourceName() throws Exception { -//TODO: Test goes here... - Boolean bl = service.getStatusBySourceName("vnf_a_3"); - System.out.println("boolean="+bl); -} - -/** -* -* Method: getIdByStatusSourceName(String sourceName) -* -*/ -@Test -public void testGetIdByStatusSourceName() throws Exception { -//TODO: Test goes here... - service.getIdByStatusSourceName("vnf_a_3"); -} - -/** -* -* Method: updateAlarmsHeader(AlarmsHeader alarmsHeader) -* -*/ -@Test -public void testUpdateAlarmsHeader() throws Exception { -//TODO: Test goes here... - AlarmsHeader a = new AlarmsHeader(); - a.setEventName("a1"); - a.setStatus("2"); - a.setVfStatus("3"); - a.setEventId("1101"); - a.setDomain("asb"); - a.setEventCategory("s"); - a.setAlarmCondition("ea"); - a.setAlarmInterfaceA("cs"); - a.setCreateTime(DateUtils.now()); - a.setEventServrity("s"); - a.setEventSourceType("q"); - a.setEventType("q"); - a.setFaultFieldsVersion("v1"); - a.setLastEpochMicroSec("csa"); - a.setNfcNamingCode("std"); - a.setNfNamingCode("cout"); - a.setPriority("cs"); - a.setReportingEntityId("112"); - a.setReportingEntityName("asfs"); - a.setSequence("cgg"); - a.setSourceId("123"); - a.setSourceName("eggs"); - a.setSpecificProblem("especially"); - a.setStartEpochMicrosec("wallet"); - a.setUpdateTime(DateUtils.now()); - a.setVersion("va2"); - service.updateAlarmsHeader(a); - //System.out.println(alarmsHeaderService.updateAlarmsHeader(a)); -} - -/** -* -* Method: getAllCount(AlarmsHeader alarmsHeader, int currentPage, int pageSize) -* -*/ -@Test -public void testGetAllCount() throws Exception { -//TODO: Test goes here... - AlarmsHeader alarmsHeader = new AlarmsHeader(); - alarmsHeader.setSourceName("vnf_a_3"); - alarmsHeader.setEventName("Fault_MultiCloud_VMFailureCleared"); - alarmsHeader.setEventId("ab305d54-85b4-a31b-7db2-fb6b9e546015"); - alarmsHeader.setSourceId("shentao-test-3004"); - alarmsHeader.setLastEpochMicroSec("1516784364860"); - alarmsHeader.setStartEpochMicrosec("1516784364860"); - alarmsHeader.setEventType(""); - alarmsHeader.setStatus("active"); - /*Date dateC = new Date("2018-01-25 15:00:40"); - Date dateE = new Date("2018-01-26 16:59:24"); - alarmsHeader.setCreateTime(dateC); - alarmsHeader.setUpdateTime(dateE);*/ - service.getAllCount(alarmsHeader,0,12); - - //alarmsHeaderService.getAllCount(alarmsHeader,0,12); -} - -/** -* -* Method: queryAlarmsHeader(AlarmsHeader alarmsHeader, int currentPage, int pageSize) -* -*/ -@Test -public void testQueryAlarmsHeader() throws Exception { -//TODO: Test goes here... - - AlarmsHeader alarmsHeader=new AlarmsHeader(); - alarmsHeader.setEventId("110"); - alarmsHeader.setEventName("asdasds"); - alarmsHeader.setSourceName("vnf_a_3"); - alarmsHeader.setEventName("Fault_MultiCloud_VMFailureCleared"); - alarmsHeader.setEventId("ab305d54-85b4-a31b-7db2-fb6b9e546015"); - alarmsHeader.setSourceId("shentao-test-3004"); - alarmsHeader.setLastEpochMicroSec("1516784364860"); - alarmsHeader.setStartEpochMicrosec("1516784364860"); - alarmsHeader.setEventType(""); - alarmsHeader.setStatus("active"); - /* Date dateC = new Date("2018-01-25 15:00:40"); - Date dateE = new Date("2018-01-26 16:59:24"); - alarmsHeader.setCreateTime(dateC); - alarmsHeader.setUpdateTime(dateE);*/ - //System.out.println(alarmsHeaderService.queryAlarmsHeader(alarmsHeader,1,100).getList().size()); - //service.queryAlarmsHeader(alarmsHeader,1,100).getList().forEach( as->System.out.println(as.toString())); - - service.queryAlarmsHeader(alarmsHeader,0,100); - //.getList(); - -} - -/** -* -* Method: queryId(String[] id) -* -*/ -@Test -public void testQueryId() throws Exception { -//TODO: Test goes here... - service.queryId(new String[]{"1101"}).forEach( a -> System.out.println(a)); -} - -/** -* -* Method: queryStatusCount(String status) -* -*/ -@Test -public void testQueryStatusCount() throws Exception { -//TODO: Test goes here... - String str =service.queryStatusCount("close"); - System.out.println("str ="+str); -} } |