如何提高此代码的覆盖范围?

时间:2018-10-19 13:48:16

标签: java spring

如何改善此代码功能或在其上添加一些额外的功能。关于访问保险申请。我希望你们中的一些人有与此部分有关的其他想法。 ..............

大多数情况下,这是一个具有gradle,spring boot和数据库的IntellJDE项目。

 package fi.mandatumlife.epeli.ms.workqueueservice.core.service;

 import com.mandatum.epeli.model.domain.Company;
 import com.mandatum.epeli.model.domain.Insurance;
 import com.mandatum.epeli.model.domain.Notification;
 import com.mandatum.epeli.model.domain.Person;
  import 
  fi.mandatumlife.epeli.ms.workqueueservice.api.dto.WorkqueueRequestDTO;
  import 
  fi.mandatumlife.epeli.ms.workqueueservice.api.dto.EntryParametersDTO;
  import 
  fi.mandatumlife.epeli.ms.workqueueservice.core.
  filler.WorkqueueRequestFiller;
  import 

  fi.mandatumlife.epeli.ms.
  workqueueservice.core.repository.CompanyRepository;
  import 

 import fi.mandatumlife.epeli.ms.workqueueservice.core.util.Util;
 import org.slf4j.Logger;
  import org.slf4j.LoggerFactory;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
 import org.springframework.stereotype.Service;
  import org.springframework.web.client.HttpServerErrorException;

  import javax.annotation.Resource;
  import java.util.Map;


 @Service
  public class CoreService {

   static final Logger log = LoggerFactory.getLogger(CoreService.class);

  private static final String POLICY_HOLDER = "Vakuutuksenottaja";

  private static final String MSG_UI_0047 = "MSG_UI_0047";
   private static final String MSG_UI_0048 = "MSG_UI_0048";
  private static final String SOURCE_EPELI = "Epeli";
  private static final String SOURCE_TYOJONOT = "Työjonot";

private static final String K0091 = "K0091";

@Autowired
private PersonRepository personRepository;

@Autowired
private InsuranceRepository insuranceRepository;

@Autowired
private CompanyRepository companyRepository;

@Autowired
private WorkqueueRequestFiller addTaskToWorkqueueRequestFiller;

@Autowired
private NotificationService notificationService;

@Autowired
private TyojonoRequestService tyojonoRequestService;

@Autowired
private Util util;

@Resource(name = "ui_codes")
private Map <String, String> uiCodes;

    public HttpStatus addTaskToWorkqueueRequest(EntryParametersDTO dto) {
    Insurance insurance = fetchInsurance(dto.getInsuranceNumber());
    Company company = fetchCompany(dto.getInsuranceNumber());
    Person person = util.checkNullOrEmpty(dto.getPersonNumber()) ?null : 
    fetchPerson(dto.getPersonNumber());

  WorkqueueRequestDTO taskDto =
            addTaskToWorkqueueRequestFiller.createAndFill(insurance, 
    company, dto);
    HttpStatus httpStatus = tyojonoRequestService.sendRequest(taskDto);

    if (httpStatus != HttpStatus.CREATED) {
        Notification notification = createSecondCaseNotification(insurance, 
     person, documentType, httpStatus);
        log.error("Http code different from 201.");
        throw new HttpServerErrorException(httpStatus, "Task was not raised. 
      " +
                "See the Notification (id = " +    
     notification.getNotificationId() + ").");
      }

    return httpStatus;
     }

       private boolean checkRequiredParameters(String status, String 
      documentType, String product) {
    return util.checkNullOrEmpty(product) || 
    util.checkNullOrEmpty(documentType)
            || status == null || !status.equalsIgnoreCase("Saapunut");
}

private Notification createFirstCaseNotification(Insurance insurance, Person 
  person, String status, String documentType, String product) {
    String text = "Seuraavat pakolliset tiedot puuttuvat: "
            + (util.checkNullOrEmpty(product) ? "vakuutuslaji, " : "")
            + (util.checkNullOrEmpty(documentType) ? 
   "teht\u00E4v\u00E4tyyppi, " : "")
            + ((status == null || !status.equalsIgnoreCase("Saapunut")) ? 
   "tila " : "");
    return notificationService.createAndSave(MSG_UI_0048, SOURCE_EPELI, 
    text, person, insurance);
     }

       private Notification createSecondCaseNotification(Insurance 
    insurance, Person person, String documentType, HttpStatus httpStatus) {
    String text = uiCodes.get(MSG_UI_0047)
            + "- Teht\u00E4v\u00E4 jota yritettiin nostaa: Task attempted to 
     raise "
            + documentType + " " + 
   util.getCodesMap(K0091).get(documentType);
    return notificationService.createAndSave(MSG_UI_0047, SOURCE_TYOJONOT, text, person, insurance);
}

private Person fetchPerson(String personNumber) {
    Person person = personRepository.findByPersonNumber(personNumber);

    if (person == null) {
        log.warn("Person not found");
        throw new HttpServerErrorException(HttpStatus.NOT_FOUND, "Person not 
      found");
      } else {
        return person;
      }
      } 

      private Insurance fetchInsurance(String insuranceNumber) {
       Insurance insurance = 
       insuranceRepository.findByInsuranceNumber(insuranceNumber);

     if (insurance == null) {
        log.warn("Insurance not found");
        throw new HttpServerErrorException(HttpStatus.NOT_FOUND, "Insurance 
     not found");
       }  else {
        return insurance;
        }
        }

        private Company fetchCompany(String insuranceNumber) {
           Company company = companyRepository
            .findCompanyByTypeAccordingInsuranceNumber(POLICY_HOLDER, 
         insuranceNumber);

        if (company == null) {
        log.warn("Company not found");
        throw new HttpServerErrorException(HttpStatus.NOT_FOUND, "Company 
        not found");
        } else {
        return company;
       }
       }
       }

1 个答案:

答案 0 :(得分:0)

首先,我将您课堂上的各种问题分开

快速浏览后,我看到了

  1. 处理http代码。
  2. 处理数据库调用
  3. 将数据从视图映射到数据模型

您应该正确地对应用程序进行分层,(例如,请参见洋葱架构,这篇文章(http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-architecture.html

所以我将在控制器中拆分代码。唯一了解http的类。 您的服务可能还有其他客户端,而不是http客户端。 (通常用于测试)

然后,我将使用汇编器或将其称为Mapper,Transformer。将控制器语言转换为您的域语言。即dto到域对象。

每个汇编器都可以轻松测试。

在存储库直接在存储库中调用之后,我还将进行空检查。如果您使用的是SpringData,则可以编写一个默认方法,该方法将通过抛出不是http异常的异常来处理null值。 您的数据库不应该知道http层。

我希望这是您想要的阅读内容