private ExternalCompany getClientExternalCompanySaved(String name, String nif) {
    ExternalCompany externalCompany = ExternalCompany.create(name, nif);
    externalCompany.setClient(true);

    externalCompanyDAO.save(externalCompany);
    externalCompanyDAO.flush();
    sessionFactory.getCurrentSession().evict(externalCompany);

    externalCompany.dontPoseAsTransientObjectAnymore();

    return externalCompany;
  }
  @Test
  @Transactional
  public void invalidSubcontractedTaskDataWithoutOrderElement() {
    int previous = orderDAO.getOrders().size();

    ExternalCompany externalCompany = getClientExternalCompanySaved("Company", "company-nif");

    SubcontractedTaskDataDTO subcontractedTaskDataDTO = new SubcontractedTaskDataDTO();
    subcontractedTaskDataDTO.externalCompanyNif = externalCompany.getNif();

    List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList =
        subcontractService.subcontract(subcontractedTaskDataDTO).instanceConstraintViolationsList;
    assertThat(instanceConstraintViolationsList.size(), equalTo(1));

    assertThat(orderDAO.getOrders().size(), equalTo(previous));
  }
  @Test
  @Transactional
  public void validSubcontractedTaskData() {
    int previous = orderDAO.getOrders().size();

    String orderLineCode = "order-line-code";

    OrderElementDTO orderElementDTO = givenBasicOrderLineDTO(orderLineCode);
    ExternalCompany externalCompany = getClientExternalCompanySaved("Company", "company-nif");

    SubcontractedTaskDataDTO subcontractedTaskDataDTO = new SubcontractedTaskDataDTO();
    subcontractedTaskDataDTO.orderElementDTO = orderElementDTO;
    subcontractedTaskDataDTO.externalCompanyNif = externalCompany.getNif();

    String orderName = "Work description";
    String orderCustomerReference = "client-reference-code";
    BigDecimal orderBudget = new BigDecimal(1000).setScale(2);
    subcontractedTaskDataDTO.workDescription = orderName;
    subcontractedTaskDataDTO.subcontractedCode = orderCustomerReference;
    subcontractedTaskDataDTO.subcontractPrice = orderBudget;

    List<InstanceConstraintViolationsDTO> instanceConstraintViolationsList =
        subcontractService.subcontract(subcontractedTaskDataDTO).instanceConstraintViolationsList;
    assertThat(instanceConstraintViolationsList.size(), equalTo(0));

    assertThat(orderDAO.getOrders().size(), equalTo(previous + 1));

    Order order = orderDAO.getOrders().get(previous);
    assertNotNull(order.getCode());
    assertTrue(order.isCodeAutogenerated());
    assertNotNull(order.getExternalCode());
    assertThat(order.getExternalCode(), equalTo(orderLineCode));
    assertThat(order.getState(), equalTo(OrderStatusEnum.OUTSOURCED));
    assertThat(order.getWorkHours(), equalTo(0));
    assertThat(order.getCustomer().getId(), equalTo(externalCompany.getId()));
    assertThat(order.getName(), equalTo(orderName));
    assertThat(order.getCustomerReference(), equalTo(orderCustomerReference));
    assertThat(order.getTotalManualBudget(), equalTo(orderBudget));

    List<OrderElement> children = order.getChildren();
    assertThat(children.size(), equalTo(1));
    assertNull(children.get(0).getExternalCode());
  }
 private ExternalCompany createValidExternalCompany() {
   ExternalCompany externalCompany =
       ExternalCompany.create(UUID.randomUUID().toString(), UUID.randomUUID().toString());
   externalCompanyDAO.save(externalCompany);
   return externalCompany;
 }