@Override
 public void delete(ToolRequest req) throws Exception {
   for (ToolRequest tr : this.requests) {
     if (tr.getKey().equals(req.getKey())) {
       this.requests.remove(tr);
       break;
     }
   }
 }
  @Override
  public List<ToolRequest> select(ToolRequest req, String selectType) throws Exception {
    List<ToolRequest> requests = new ArrayList<>();

    switch (selectType) {
      case "clientRequest":
        for (ToolRequest tr : this.requests) {
          if (tr.getClientRequest().getKey().equals(req.getClientRequest().getKey())) {
            requests.add(tr);
          }
        }
        break;
      case "key":
        for (ToolRequest tr : this.requests) {
          if (tr.getKey().equals(req.getKey())) {
            requests.add(tr);
          }
        }
        break;
      case "tool":
        for (ToolRequest tr : this.requests) {
          if (tr.getTool().getKey().equals(req.getTool().getKey())) {
            requests.add(tr);
          }
        }
        break;
      default:
        throw new InvalidParameterException("Select type isn't valid");
    }

    if (requests.isEmpty()) {
      throw new InvalidParameterException("No one tool request matched");
    } else {
      return requests;
    }
  }
  @Test
  public void test_FindClientRequest_0() {
    // initializing "test session"
    SessionIdHelper sessionIdHelper = new SessionIdHelper();
    sessionIdHelper.setSessionId("test_FindClientRequest_0");

    // initializing test context
    TestContextHelper context = new TestContextHelper();
    SpringContextHelper.getInstance(context, sessionIdHelper.getSessionId());
    HashMap<String, Object> beans = new HashMap<>();
    context.setBeans(beans);

    // initializing test client request DAO and it's data
    TestClientRequestDAO testClientRequestDao = new TestClientRequestDAO();
    beans.put("clientRequestDao", testClientRequestDao);
    List<ClientRequest> clientRequests = new ArrayList<>();
    ClientRequest firstClientRequest = new ClientRequest();
    firstClientRequest.setKey(1);
    firstClientRequest.setNumber(75);
    clientRequests.add(firstClientRequest);
    ClientRequest secondClientRequest = new ClientRequest();
    secondClientRequest.setKey(2);
    secondClientRequest.setNumber(751);
    clientRequests.add(secondClientRequest);
    testClientRequestDao.setRequests(clientRequests);

    // initializing test tool request DAO and it's data
    TestToolRequestDAO testToolRequestDao = new TestToolRequestDAO();
    beans.put("toolRequestDao", testToolRequestDao);
    List<ToolRequest> toolRequests = new ArrayList<>();
    ToolRequest firstToolRequest = new ToolRequest();
    firstToolRequest.setKey(1);
    firstToolRequest.setInProcess(1);
    firstToolRequest.setClientRequest(firstClientRequest);
    toolRequests.add(firstToolRequest);
    ToolRequest secondToolRequest = new ToolRequest();
    secondToolRequest.setKey(2);
    secondToolRequest.setInProcess(2);
    secondToolRequest.setClientRequest(firstClientRequest);
    toolRequests.add(secondToolRequest);
    ToolRequest thirdToolRequest = new ToolRequest();
    thirdToolRequest.setKey(3);
    thirdToolRequest.setInProcess(1);
    thirdToolRequest.setClientRequest(secondClientRequest);
    toolRequests.add(thirdToolRequest);
    testToolRequestDao.setRequests(toolRequests);

    // initializing services
    ToolRequestServiceFactoryBean trsFactory = new ToolRequestServiceFactoryBean();
    trsFactory.setSessionIdHelper(sessionIdHelper);
    IToolRequestService toolRequestService = null;

    ClientRequestServiceFactoryBean crsFactory = new ClientRequestServiceFactoryBean();
    crsFactory.setSessionIdHelper(sessionIdHelper);
    IClientRequestService clientRequestService = null;

    try {
      toolRequestService = (IToolRequestService) trsFactory.createInstanceStub();
      beans.put("toolRequestService", toolRequestService);

      clientRequestService = (IClientRequestService) crsFactory.createInstanceStub();
      beans.put("clientRequestService", clientRequestService);

      ClientRequest clientRequest = clientRequestService.findClientRequest(firstToolRequest);
      Assert.assertNotNull(clientRequest);
      Assert.assertEquals(1, clientRequest.getKey().intValue());
      Assert.assertEquals(75, clientRequest.getNumber().intValue());
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }
 @Override
 public void create(ToolRequest req) throws Exception {
   req.setKey(++this.requestsCount);
   this.requests.add(req);
 }
  @Test
  public void test_findCriticalClientRequests_0() {
    // initializing "test session"
    SessionIdHelper sessionIdHelper = new SessionIdHelper();
    sessionIdHelper.setSessionId("test_findCriticalClientRequests_0");

    // initializing test context
    TestContextHelper context = new TestContextHelper();
    SpringContextHelper.getInstance(context, sessionIdHelper.getSessionId());
    HashMap<String, Object> beans = new HashMap<>();
    context.setBeans(beans);

    // initializing test client request DAO
    TestClientRequestDAO clientRequestDao = new TestClientRequestDAO();
    beans.put("clientRequestDao", clientRequestDao);
    List<ClientRequest> clientRequests = new ArrayList<>();

    ClientRequest cr0 = new ClientRequest();
    cr0.setKey(1);
    cr0.setNumber(750);
    cr0.setPerformed(true);
    clientRequests.add(cr0);

    ClientRequest cr1 = new ClientRequest();
    cr1.setKey(2);
    cr1.setNumber(751);
    cr1.setPerformed(false);
    clientRequests.add(cr1);

    ClientRequest cr2 = new ClientRequest();
    cr2.setKey(3);
    cr2.setNumber(752);
    cr2.setPerformed(false);
    clientRequests.add(cr2);

    ClientRequest cr3 = new ClientRequest();
    cr3.setKey(4);
    cr3.setNumber(753);
    cr3.setPerformed(true);
    clientRequests.add(cr3);

    ClientRequest cr4 = new ClientRequest();
    cr4.setKey(5);
    cr4.setNumber(754);
    cr4.setPerformed(false);
    clientRequests.add(cr4);
    clientRequestDao.setRequests(clientRequests);

    // initializing test tool request DAO
    TestToolRequestDAO toolRequestDao = new TestToolRequestDAO();
    beans.put("toolRequestDao", toolRequestDao);
    List<ToolRequest> toolRequests = new ArrayList<>();

    ToolRequest tr0 = new ToolRequest();
    tr0.setKey(1);
    tr0.setInProcess(2);
    tr0.setClientRequest(cr1);
    toolRequests.add(tr0);

    ToolRequest tr1 = new ToolRequest();
    tr1.setKey(2);
    tr1.setInProcess(0);
    tr1.setClientRequest(cr0);
    toolRequests.add(tr1);

    ToolRequest tr2 = new ToolRequest();
    tr2.setKey(3);
    tr2.setInProcess(1);
    tr2.setClientRequest(cr1);
    toolRequests.add(tr2);

    ToolRequest tr3 = new ToolRequest();
    tr3.setKey(4);
    tr3.setInProcess(1);
    tr3.setClientRequest(cr1);
    toolRequests.add(tr3);

    ToolRequest tr4 = new ToolRequest();
    tr4.setKey(5);
    tr4.setInProcess(2);
    tr4.setClientRequest(cr2);
    toolRequests.add(tr4);

    ToolRequest tr5 = new ToolRequest();
    tr5.setKey(6);
    tr5.setInProcess(1);
    tr5.setClientRequest(cr4);
    toolRequests.add(tr5);

    ToolRequest tr6 = new ToolRequest();
    tr6.setKey(7);
    tr6.setInProcess(0);
    tr6.setClientRequest(cr3);
    toolRequests.add(tr6);

    ToolRequest tr7 = new ToolRequest();
    tr7.setKey(8);
    tr7.setInProcess(1);
    tr7.setClientRequest(cr4);
    toolRequests.add(tr7);

    ToolRequest tr8 = new ToolRequest();
    tr8.setKey(9);
    tr8.setInProcess(0);
    tr8.setClientRequest(cr3);
    toolRequests.add(tr8);
    toolRequestDao.setRequests(toolRequests);

    // initialization test contract DAO
    TestContractDAO contractDao = new TestContractDAO();
    beans.put("contractDao", contractDao);
    List<Contract> contracts = new ArrayList<>();

    Contract c0 = new Contract();
    c0.setKey(1);
    c0.setNumber("75_1");
    contracts.add(c0);

    Contract c1 = new Contract();
    c1.setKey(2);
    c1.setNumber("75_2");
    contracts.add(c1);

    Contract c2 = new Contract();
    c2.setKey(3);
    c2.setNumber("75_3");
    contracts.add(c2);

    Contract c3 = new Contract();
    c3.setKey(4);
    c3.setNumber("75_4");
    contracts.add(c3);
    contractDao.setContracts(contracts);

    // initialization test tool contract DAO
    TestToolContractDAO toolContractDao = new TestToolContractDAO();
    beans.put("toolContractDao", toolContractDao);
    List<ToolContract> toolContracts = new ArrayList<>();
    Calendar cal = Calendar.getInstance();

    ToolContract tc0 = new ToolContract();
    tc0.setKey(1);
    cal.set(2012, 3, 30);
    tc0.setReturnDate(cal.getTime());
    tc0.setContract(c0);
    tc0.setToolRequest(tr2);
    toolContracts.add(tc0);

    ToolContract tc1 = new ToolContract();
    tc1.setKey(2);
    cal.set(2012, 3, 29);
    tc1.setReturnDate(cal.getTime());
    tc1.setContract(c1);
    tc1.setToolRequest(tr3);
    toolContracts.add(tc1);

    ToolContract tc2 = new ToolContract();
    tc2.setKey(3);
    cal.set(2012, 4, 12);
    tc2.setReturnDate(cal.getTime());
    tc2.setContract(c2);
    tc2.setToolRequest(tr5);
    toolContracts.add(tc2);

    ToolContract tc3 = new ToolContract();
    tc3.setKey(4);
    cal.set(2012, 4, 13);
    tc3.setReturnDate(cal.getTime());
    tc3.setContract(c3);
    tc3.setToolRequest(tr7);
    toolContracts.add(tc3);
    toolContractDao.setToolContracts(toolContracts);

    // initializing services
    ToolRequestServiceFactoryBean trsFactory = new ToolRequestServiceFactoryBean();
    trsFactory.setSessionIdHelper(sessionIdHelper);
    IToolRequestService toolRequestService = null;

    ClientRequestServiceFactoryBean crsFactory = new ClientRequestServiceFactoryBean();
    crsFactory.setSessionIdHelper(sessionIdHelper);
    IClientRequestService clientRequestService = null;

    try {
      toolRequestService = (IToolRequestService) trsFactory.createInstanceStub();
      beans.put("toolRequestService", toolRequestService);

      clientRequestService = (IClientRequestService) crsFactory.createInstanceStub();
      beans.put("clientRequestService", clientRequestService);

      cal.set(2012, 4, 13);
      List<ClientRequest> requests = clientRequestService.findCriticalClientRequests(cal.getTime());
      Assert.assertEquals(2, requests.size());

      ClientRequest cli0 = requests.get(0);
      ClientRequest cli1 = requests.get(1);
      Assert.assertEquals(cr1, cli0);
      Assert.assertEquals(cr4, cli1);
    } catch (Exception e) {
      fail(e.getMessage());
    }
  }