@Test
public void testSearchSpaces() throws Exception {

  assertEquals(0, this.spaceRepository.count());
  final Tenant d_tenant = this.createDefaultTenant();
  final Space d_space = this.createSpace(d_tenant, TEST_DEFAULT_SPACE, true);
  assertEquals(1, this.spaceRepository.count());
  final String space_name = "foo";
  org.eclipse.steady.shared.json.model.Space new_shared_space =
      new org.eclipse.steady.shared.json.model.Space();
  new_shared_space.setSpaceName("foo");
  new_shared_space.setExportConfiguration(ExportConfiguration.OFF);
  new_shared_space.setSpaceDescription("bar");
  new_shared_space.setOwnerEmails(
      new HashSet<String>(Arrays.asList(new String[] {"foo@bar.com"})));
  final List<org.eclipse.steady.shared.json.model.Property> props =
      new ArrayList<org.eclipse.steady.shared.json.model.Property>();
  props.add(
      new org.eclipse.steady.shared.json.model.Property(
          PropertySource.USER, "propName", "propValue"));
  new_shared_space.setProperties(props);
  // Post w tenant header (should create a NON default space as it's not provided)
  MockHttpServletRequestBuilder post_builder =
      post("/spaces")
          .content(JacksonUtil.asJsonString(new_shared_space))
          .contentType(MediaType.APPLICATION_JSON)
          .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken())
          .accept(MediaType.APPLICATION_JSON);
  MvcResult result =
      mockMvc
          .perform(post_builder)
          .andExpect(status().isCreated())
          .andExpect(content().contentType(contentType))
          .andExpect(jsonPath("$.default", is(false)))
          .andExpect(jsonPath("$.spaceName", is(space_name)))
          .andReturn();
  // Create another one (private)
  new_shared_space.setPublic(false);
  post_builder =
      post("/spaces")
          .content(JacksonUtil.asJsonString(new_shared_space))
          .contentType(MediaType.APPLICATION_JSON)
          .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken())
          .accept(MediaType.APPLICATION_JSON);
  result =
      mockMvc
          .perform(post_builder)
          .andExpect(status().isCreated())
          .andExpect(content().contentType(contentType))
          .andExpect(jsonPath("$.default", is(false)))
          .andExpect(jsonPath("$.spaceName", is(space_name)))
          .andReturn();
  assertEquals(3, this.spaceRepository.count());
  // Search : 0 hits
  MockHttpServletRequestBuilder search_builder =
      get("/spaces/search")
          .param("propertyName", "propName")
          .param("value", "xyz", "abc")
          .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken())
          .accept(MediaType.APPLICATION_JSON);
  result = mockMvc.perform(search_builder).andExpect(status().isOk()).andReturn();
  org.eclipse.steady.shared.json.model.Space[] search_result =
      (org.eclipse.steady.shared.json.model.Space[])
          JacksonUtil.asObject(
              result.getResponse().getContentAsString(),
              org.eclipse.steady.shared.json.model.Space[].class);
  assertEquals(0, search_result.length);
  // Search : 1 hit
  search_builder =
      get("/spaces/search")
          .param("propertyName", "propName")
          .param("value", "xyz", "abc", "propValue")
          .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken())
          .accept(MediaType.APPLICATION_JSON);
  result = mockMvc.perform(search_builder).andExpect(status().isOk()).andReturn();
  search_result =
      (org.eclipse.steady.shared.json.model.Space[])
          JacksonUtil.asObject(
              result.getResponse().getContentAsString(),
              org.eclipse.steady.shared.json.model.Space[].class);
  assertEquals(
      1,
      search_result.length); // Should be one public space, the private one shall not be returned
  // Bad reqeust due to lack of search term
  search_builder =
      get("/spaces/search")
          .param("propertyName", "propName")
          .header(Constants.HTTP_TENANT_HEADER, d_tenant.getTenantToken())
          .accept(MediaType.APPLICATION_JSON);
  mockMvc.perform(search_builder).andExpect(status().isBadRequest());
}