@Test
  public void allowsPostRequestForAdmin() throws Exception {

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.ACCEPT, MediaTypes.HAL_JSON_VALUE);
    headers.set(
        HttpHeaders.AUTHORIZATION,
        "Basic " + new String(Base64.encode(("ollie:gierke").getBytes())));

    mvc.perform(
            get("/employees")
                . //
                headers(headers))
        . //
        andExpect(content().contentTypeCompatibleWith(MediaTypes.HAL_JSON))
        . //
        andExpect(status().isOk())
        . //
        andDo(print());

    headers.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);

    String location =
        mvc.perform(
                post("/employees")
                    . //
                    content(PAYLOAD)
                    . //
                    headers(headers))
            . //
            andExpect(status().isCreated())
            . //
            andDo(print())
            . //
            andReturn()
            .getResponse()
            .getHeader(HttpHeaders.LOCATION);

    ObjectMapper mapper = new ObjectMapper();

    String content =
        mvc.perform(get(location))
            . //
            andReturn()
            .getResponse()
            .getContentAsString();
    Employee employee = mapper.readValue(content, Employee.class);

    assertThat(employee.getFirstName(), is("Saruman"));
    assertThat(employee.getLastName(), is("the White"));
    assertThat(employee.getTitle(), is("Wizard"));
  }
Esempio n. 2
0
  @Test
  public void testSave() throws Exception {

    Employee emp = new Employee();
    emp.setEmpName("黄鹏");
    emp.setWorkDate(new Date());
    emp.setDesc("测试描述");

    Session sessiom = sf.openSession();
    Transaction tx = sessiom.beginTransaction();
    sessiom.save(emp);

    tx.commit();
    sessiom.close();
  }
  @Test
  public void testFindEmployees() {
    Employee emp = getSampleEmployee();

    final String sampleUniqueName = Long.toString(System.currentTimeMillis());
    emp.setName(sampleUniqueName);
    createEmployee(emp);

    List<Employee> empList =
        empDaoBean.findEmployees(
            new EmployeeMatcherCreator() {
              public EmployeeMatcher createEmployeeMatcher() {
                EmployeeMatcher empMatcher = new EmployeeMatcher();
                empMatcher.setNameToFind(sampleUniqueName);
                return empMatcher;
              }
            });
    assertEquals(1, empList.size());
  }
 public void doFunctionalQueryTest(IMap imap) {
   Employee em = new Employee("joe", 33, false, 14.56);
   imap.put("1", new Employee("joe", 33, false, 14.56));
   imap.put("2", new Employee("ali", 23, true, 15.00));
   for (int i = 3; i < 103; i++) {
     imap.put(
         String.valueOf(i), new Employee("name" + i, i % 60, ((i % 2) == 1), Double.valueOf(i)));
   }
   Set<Map.Entry> entries = imap.entrySet();
   assertEquals(102, entries.size());
   int itCount = 0;
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     itCount++;
   }
   assertEquals(102, itCount);
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.is("active").and(e.get("age").equal(23));
   entries = imap.entrySet(predicate);
   assertEquals(3, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
   imap.remove("2");
   entries = imap.entrySet(predicate);
   assertEquals(2, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
 }
 @Test
 public void putAndGetEmployeeObjects() {
   HazelcastClient hClient = getHazelcastClient();
   int counter = 1000;
   Map<String, Employee> clientMap = hClient.getMap("putAndGetEmployeeObjects");
   for (int i = 0; i < counter; i++) {
     Employee employee = new Employee("name" + i, i, true, 5000 + i);
     employee.setMiddleName("middle" + i);
     employee.setFamilyName("familiy" + i);
     clientMap.put("" + i, employee);
   }
   for (int i = 0; i < counter; i++) {
     Employee e = clientMap.get("" + i);
     assertEquals("name" + i, e.getName());
     assertEquals("middle" + i, e.getMiddleName());
     assertEquals("familiy" + i, e.getFamilyName());
     assertEquals(i, e.getAge());
     assertEquals(true, e.isActive());
     assertEquals(5000 + i, e.getSalary(), 0);
   }
   //        }
 }
  public static Company createCompany() {

    // Create company
    Company sampleCompany = new Company();
    sampleCompany.setName("ACME Corporation");

    // Create all employees
    Employee craig = new Employee();
    craig.setName("Craig");
    craig.setAddress("Redmond");
    craig.setSalary(123456);
    Employee erik = new Employee();
    erik.setName("Erik");
    erik.setAddress("Utrecht");
    erik.setSalary(12345);
    Employee ralf = new Employee();
    ralf.setName("Ralf");
    ralf.setAddress("Koblenz");
    ralf.setSalary(1234);
    Employee ray = new Employee();
    ray.setName("Ray");
    ray.setAddress("Redmond");
    ray.setSalary(234567);
    Employee klaus = new Employee();
    klaus.setName("Klaus");
    klaus.setAddress("Boston");
    klaus.setSalary(23456);
    Employee karl = new Employee();
    karl.setName("Karl");
    karl.setAddress("Riga");
    karl.setSalary(2345);
    Employee joe = new Employee();
    joe.setName("Joe");
    joe.setAddress("Wifi City");
    joe.setSalary(2344);

    // Create research department
    Department research = new Department();
    research.setManager(craig);
    research.setName("Research");
    research.getEmployees().add(erik);
    research.getEmployees().add(ralf);
    sampleCompany.getDepts().add(research);

    // Create development department
    Department development = new Department();
    development.setManager(ray);
    development.setName("Development");
    sampleCompany.getDepts().add(development);

    // Create sub-department dev1
    Department dev1 = new Department();
    dev1.setName("Dev1");
    dev1.setManager(klaus);
    development.getSubdepts().add(dev1);

    // Create sub-department dev11
    Department dev11 = new Department();
    dev11.setName("Dev1.1");
    dev11.setManager(karl);
    dev11.getEmployees().add(joe);
    dev1.getSubdepts().add(dev11);

    return sampleCompany;
  }