diff options
author | zhangab <zhanganbing@chinamobile.com> | 2018-03-29 05:18:46 +0800 |
---|---|---|
committer | zhangab <zhanganbing@chinamobile.com> | 2018-03-29 05:18:57 +0800 |
commit | 1f19ca25e332dd07246e3ff44e36c2277b9c23d8 (patch) | |
tree | 69ad239e56db4ee059b54729c6e26c48642bb77a | |
parent | f9f939f486195c7fec36ccd6d20ac950b886f4de (diff) |
improve sonar coverage for uui-server
Change-Id: Ia0548cee21670d1209561729240bee1efce70b67
Issue-ID: USECASEUI-103
Signed-off-by: zhangab <zhanganbing@chinamobile.com>
2 files changed, 47 insertions, 185 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 3b751452..5f249ad0 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 @@ -1,4 +1,4 @@ -/*
+/**
* Copyright (C) 2017 CMCC, Inc. and others. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,8 +15,6 @@ */
package org.onap.usecaseui.server.service.impl;
-
-
import java.util.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
@@ -35,80 +33,51 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Service;
-
@Service("AlarmsHeaderService")
@Transactional
@org.springframework.context.annotation.Configuration
@EnableAspectJAutoProxy
public class AlarmsHeaderServiceImpl implements AlarmsHeaderService {
-
private static final Logger logger = LoggerFactory.getLogger(AlarmsHeaderServiceImpl.class);
@Autowired
private SessionFactory sessionFactory;
-
+
private Session getSession() {
return sessionFactory.openSession();
}
-
- public String saveAlarmsHeader(AlarmsHeader alarmsHeader) {
- try(Session session = getSession()){
- if (null == alarmsHeader) {
- logger.error("AlarmsHeaderServiceImpl saveAlarmsHeader alarmsHeader is null!");
- }
- logger.info("AlarmsHeaderServiceImpl saveAlarmsHeader: alarmsHeader={}", alarmsHeader);
- Transaction tx = session.beginTransaction();
- session.save(alarmsHeader);
- tx.commit();
- session.flush();
- return "1";
- } catch (Exception e) {
- logger.error("exception occurred while performing AlarmsHeaderServiceImpl saveAlarmsHeader. Details:" + e.getMessage());
- return "0";
- }
-
- }
-
- /*@Override
- public String updateAlarmsHeader2018(String status, String date,String eventNameCleared,String eventName, String reportingEntityName,String specificProblem) {
- try(Session session = sessionFactory.getCurrentSession();){
-
- logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}");
- session.beginTransaction();
- Query q=session.createQuery("update AlarmsHeader set status='"+status+"', updateTime='"+date+"' where eventName='"+eventName+"' and reportingEntityName='"+reportingEntityName+"' and specificProblem ='"+specificProblem+"' ");
- q.executeUpdate();
- session.getTransaction().commit();
+ public String saveAlarmsHeader(AlarmsHeader alarmsHeader) {
+ try(Session session = getSession()){
+ if (null == alarmsHeader) {
+ logger.error("AlarmsHeaderServiceImpl saveAlarmsHeader alarmsHeader is null!");
+ }
+ logger.info("AlarmsHeaderServiceImpl saveAlarmsHeader: alarmsHeader={}", alarmsHeader);
+ Transaction tx = session.beginTransaction();
+ session.save(alarmsHeader);
+ tx.commit();
session.flush();
return "1";
} catch (Exception e) {
- logger.error("exception occurred while performing AlarmsInformationServiceImpl updateAlarmsInformation. Details:" + e.getMessage());
+ logger.error("exception occurred while performing AlarmsHeaderServiceImpl saveAlarmsHeader. Details:" + e.getMessage());
return "0";
}
- }*/
-
+ }
@Override
public String updateAlarmsHeader2018(String status, Timestamp date, String startEpochMicrosecCleared, String lastEpochMicroSecCleared, String eventName, String reportingEntityName, String specificProblem) {
-
- try(Session session = sessionFactory.openSession();){
- //try(Session session = sessionFactory.getCurrentSession();){
+ try(Session session = getSession()){
logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}");
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+"'");
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);
q.setDate("date",date);
-
q.setString("startEpochMicrosecCleared",startEpochMicrosecCleared);
q.setString("lastEpochMicroSecCleared",lastEpochMicroSecCleared);
q.setString("eventName",eventName);
q.setString("reportingEntityName",reportingEntityName);
q.setString("specificProblem",specificProblem);
-
-
q.executeUpdate();
session.getTransaction().commit();
session.flush();
@@ -121,13 +90,10 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { @Override
public Boolean getStatusBySourceName(String sourceName) {
- try(Session session = sessionFactory.openSession()){
+ try(Session session = getSession()){
logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}");
- //Transaction tx = session.beginTransaction();
- //session.beginTransaction();
Query q=session.createQuery("select status from AlarmsHeader where sourceName='"+sourceName+"' and status='active'");
q.setMaxResults(1);
- //tx.commit();
String s=(String)q.uniqueResult();
Boolean status=false;
if(s!=null && "active".equals(s)){
@@ -137,47 +103,32 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { }
session.flush();
return status;
- //return "1";
} catch (Exception e) {
logger.error("exception occurred while performing AlarmsInformationServiceImpl updateAlarmsInformation. Details:" + e.getMessage());
return false;
}
- //return null;
}
@Override
public AlarmsHeader getIdByStatusSourceName(String sourceName) {
- //List<AlarmsHeader> list = new ArrayList<>();
AlarmsHeader s = new AlarmsHeader();
- try(Session session = sessionFactory.openSession()){
+ try(Session session = getSession()){
logger.info("AlarmsInformationServiceImpl updateAlarmsInformation: alarmsInformation={}");
- //Transaction tx = session.beginTransaction();
- //session.beginTransaction();
- //Query q=session.createQuery("select sourceName,createTime from AlarmsHeader where sourceName='"+sourceName+"' and status='active' order by createTime desc");
Query q=session.createQuery("from AlarmsHeader where sourceName='"+sourceName+"' and status='active' order by createTime desc");
-
- //q.setString(1,sourceName);
q.setMaxResults(1);
- //tx.commit();
- //String s=(String)q.uniqueResult();
s = (AlarmsHeader)q.uniqueResult();
- // list= q.list();
-
session.flush();
return s;
- //return "1";
} catch (Exception e) {
logger.error("exception occurred while performing AlarmsInformationServiceImpl updateAlarmsInformation. Details:" + e.getMessage());
return s;
}
- //return null;
}
-
@Override
public String updateAlarmsHeader(AlarmsHeader alarmsHeader) {
- try(Session session = sessionFactory.openSession();){
+ try(Session session = getSession()){
if (null == alarmsHeader){
logger.error("AlarmsHeaderServiceImpl updateAlarmsHeader alarmsHeader is null!");
}
@@ -193,21 +144,16 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { }
}
-
-
-
-
@Override
public int getAllCountByStatus(String status){
- try (Session session = sessionFactory.openSession()){
+ try (Session session = getSession()){
StringBuffer count = new StringBuffer("select count(*) from AlarmsHeader a where 1=1");
if(!"0".equals(status)){
count.append(" and a.status=:status");
}
Query query =session.createQuery(count.toString());
query.setString("status",status);
- //int q = (int)query.uniqueResult();
- long q=(long)query.uniqueResult();
+ long q=(long)query.uniqueResult();
session.flush();
return (int)q;
}catch (Exception e){
@@ -218,7 +164,7 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { @Override
public List<AlarmsHeader> getAllByStatus(String status,String eventName,String sourceName,String eventServerity,String reportingEntityName, Date createTime, Date endTime){
- try (Session session = sessionFactory.openSession()){
+ try (Session session = getSession()){
StringBuffer string = new StringBuffer("from AlarmsHeader a where 1=1");
if(!"0".equals(status)){
string.append(" and a.status=:status");
@@ -247,33 +193,24 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { if( null!=createTime && endTime!= null) {
query.setDate("startTime",createTime);
query.setDate("endTime",endTime);
-
}
List<AlarmsHeader> list =query.list();
-
return list;
-
}catch (Exception e){
-
logger.error("exception occurred while performing AlarmsHeaderServiceImpl getAllCount."+e.getMessage());
return null;
}
-
-
-
}
@Override
public AlarmsHeader getAlarmsHeaderDetail(Integer id) {
- try(Session session = sessionFactory.openSession()) {
-
+ try(Session session = getSession()) {
String string = "from AlarmsHeader a where 1=1 and a.id=:id";
Query q = session.createQuery(string);
q.setInteger("id",id);
AlarmsHeader alarmsHeader =(AlarmsHeader)q.uniqueResult();
session.flush();
return alarmsHeader;
-
}catch (Exception e){
logger.error("exception occurred while performing AlarmsHeaderServiceImpl getAlarmsHeaderDetail."+e.getMessage());
return null;
@@ -282,7 +219,7 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { @Override
public int getAllByDatetime(String status,String eventId, String eventServrity, String createTime) {
- try (Session session = sessionFactory.openSession();){
+ try (Session session = getSession()){
StringBuffer string = new StringBuffer("select count(*) as count from AlarmsHeader a where 1=1");
if(!"0".equals(status) && status!=null){
@@ -291,50 +228,33 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { if(!"0".equals(eventId) && eventId!=null){
string.append(" and a.eventId=:eventId");
}
-
if(!"0".equals(eventServrity) && eventServrity!=null){
string.append(" and a.eventServrity=:eventServrity");
}
-
- /*if( null!=createTime && endTime!= null) {
- string.append(" and a.createTime between :startTime and :endTime");
- }*/
if( null!=createTime) {
string.append(" and to_days(a.createTime) = to_days('"+createTime+"')");
}
- /*string.append(" group by DATE_FORMAT(a.createTime,'%y-%m-%d')");*/
Query query = session.createQuery(string.toString());
query.setString("status",status);
query.setString("eventId",eventId);
query.setString("eventServrity",eventServrity);
- //query.setDate("createTime",createTime);
- /*if( null!=createTime && endTime!= null) {
- query.setDate("startTime",createTime);
- query.setDate("endTime",endTime);
-
- }*/
long l = (long)query.uniqueResult();
int a = (int) l;
- //List<AlarmsHeader> list =query.list();
session.flush();
return a;
-
}catch (Exception e){
-
logger.error("exception occurred while performing AlarmsHeaderServiceImpl getAllCount."+e.getMessage());
return 0;
}
-
}
-
public int getAllCount(AlarmsHeader alarmsHeader,int currentPage,int pageSize) {
- try(Session session = sessionFactory.openSession()){
+ try(Session session = getSession()){
StringBuffer count=new StringBuffer("select count(*) from AlarmsHeader a where 1=1");
if (null == alarmsHeader) {
- //logger.error("AlarmsHeaderServiceImpl getAllCount alarmsHeader is null!");
+ logger.error("AlarmsHeaderServiceImpl getAllCount alarmsHeader is null!");
}else {
if(null!=alarmsHeader.getVersion()) {
String ver=alarmsHeader.getVersion();
@@ -420,10 +340,6 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { String ver =alarmsHeader.getSpecificProblem();
count.append(" and a.specificProblem like '%"+ver+"%'");
}
- /*if(null!=alarmsHeader.getVfStatus()) {
- String ver =alarmsHeader.getVfStatus();
- count.append(" and a.vfStatus = '"+ver+"'");
- }*/
if(null!=alarmsHeader.getAlarmInterfaceA()) {
String ver =alarmsHeader.getAlarmInterfaceA();
count.append(" and a.alarmInterfaceA like '%"+ver+"%'");
@@ -436,7 +352,6 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { count.append(" and a.createTime between :startTime and :endTime");
}
}
- //count.append(" and a.status != 3");
Query query = session.createQuery(count.toString());
if (null != alarmsHeader)
if(null!=alarmsHeader.getCreateTime() || alarmsHeader.getUpdateTime()!= null) {
@@ -452,17 +367,16 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { }
}
- //@SuppressWarnings("unchecked")
@Override
public Page<AlarmsHeader> queryAlarmsHeader(AlarmsHeader alarmsHeader,int currentPage,int pageSize) {
Page<AlarmsHeader> page = new Page<AlarmsHeader>();
int allRow =this.getAllCount(alarmsHeader,currentPage,pageSize);
int offset = page.countOffset(currentPage, pageSize);
-
- try(Session session = sessionFactory.openSession();){
+
+ try(Session session = getSession()){
StringBuffer hql =new StringBuffer("from AlarmsHeader a where 1=1");
if (null == alarmsHeader) {
- //logger.error("AlarmsHeaderServiceImpl queryAlarmsHeader alarmsHeader is null!");
+ logger.error("AlarmsHeaderServiceImpl queryAlarmsHeader alarmsHeader is null!");
}else {
if(null!=alarmsHeader.getVersion()) {
String ver=alarmsHeader.getVersion();
@@ -548,10 +462,6 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { String ver =alarmsHeader.getSpecificProblem();
hql.append(" and a.specificProblem like '%"+ver+"%'");
}
- /*if(null!=alarmsHeader.getVfStatus()) {
- String ver =alarmsHeader.getVfStatus();
- hql.append(" and a.vfStatus = '"+ver+"'");
- }*/
if(null!=alarmsHeader.getAlarmInterfaceA()) {
String ver =alarmsHeader.getAlarmInterfaceA();
hql.append(" and a.alarmInterfaceA like '%"+ver+"%'");
@@ -564,7 +474,6 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { hql.append(" and a.createTime between :startTime and :endTime");
}
}
- //hql.append(" and a.status != 3");
logger.info("AlarmsHeaderServiceImpl queryAlarmsHeader: alarmsHeader={}", alarmsHeader);
Query query = session.createQuery(hql.toString());
if (null != alarmsHeader)
@@ -579,7 +488,6 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { page.setPageSize(pageSize);
page.setTotalRecords(allRow);
page.setList(list);
-
session.flush();
return page;
} catch (Exception e) {
@@ -588,11 +496,10 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { }
}
-
@SuppressWarnings("unchecked")
@Override
public List<AlarmsHeader> queryId(String[] id) {
- try(Session session = sessionFactory.openSession()){
+ try(Session session = getSession()){
if(id.length==0) {
logger.error("AlarmsHeaderServiceImpl queryId is null!");
}
@@ -606,15 +513,12 @@ public class AlarmsHeaderServiceImpl implements AlarmsHeaderService { }
}
-
@Override
public String queryStatusCount(String status) {
- try(Session session = sessionFactory.openSession()){
+ try(Session session = getSession()){
String hql = "select count(status) from AlarmsHeader a";
if (!status.equals("0"))
hql+=" where a.status = :status";
- // else
- // hql+=" where a.status != 3";
Query query = session.createQuery(hql);
if (!status.equals("0"))
query.setString("status",status);
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 3d1c37a2..f7be8c09 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 @@ -55,65 +55,23 @@ public class AlarmsHeaderServiceImplTest { public void after() throws Exception { } + AlarmsHeaderServiceImpl alarmsHeaderServiceImpl = new AlarmsHeaderServiceImpl(); private Session session; private Transaction transaction; private Query query; - /** - * - * Method: saveAlarmsHeader(AlarmsHeader alarmsHeader) - * - */ @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"); - - - - - 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; } - }; - new MockUp<SessionFactory>() { @Mock - public Session openSession() { - return mockedSession.getMockInstance(); + public void save(AlarmsHeader alarmsHeader) { + } + @Mock + public void flush() { } }; new MockUp<Transaction>() { @@ -121,20 +79,20 @@ public class AlarmsHeaderServiceImplTest { public void commit() { } }; - new MockUp<AlarmsHeaderServiceImpl>() { - @Mock - private Session getSession() { - return mockedSession.getMockInstance(); - } - }; - - AlarmsHeaderServiceImpl alarmsHeaderServiceImpl = new AlarmsHeaderServiceImpl(); - alarmsHeaderServiceImpl.saveAlarmsHeader(a); - } - - - + new MockUp<AlarmsHeaderServiceImpl>() { + @Mock + private Session getSession() { + return mockedSession.getMockInstance(); + } + }; + AlarmsHeader alarmsHeader = null; + alarmsHeaderServiceImpl.saveAlarmsHeader(alarmsHeader); + } + @Test(expected = Exception.class) + public void testSaveAlarmsHeaderError(){ + alarmsHeaderServiceImpl.saveAlarmsHeader(new AlarmsHeader()); + } } |