Search in sources :

Example 16 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryDaoImpl method queryList.

/**
 * Get user query list and extract tags
 *
 * @param sql the select from user_queries sql query
 * @param source
 * @return
 */
private List<UserQuery> queryList(String sql, SqlParameterSource source) {
    List<UserQuery> userQueryList = new NamedParameterJdbcTemplate(dsLocator.getUserDataSource()).query(sql, source, new UserQueryRowMapper());
    if (!userQueryList.isEmpty()) {
        List<Long> queryIds = Lists.transform(userQueryList, UserQueryUtils.EXTRACT_QUERY_ID);
        Map<Long, Set<String>> tags = getQueryTags(queryIds);
        for (UserQuery query : userQueryList) {
            Set<String> tagSet = tags.get(query.getUserQueryId());
            // use hashset because google implementation is not serializable
            query.setTags(new HashSet<String>(tagSet));
        }
    }
    return userQueryList;
}
Also used : ResultSet(java.sql.ResultSet) NamedParameterJdbcTemplate(org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate) UserQuery(org.nextprot.api.user.domain.UserQuery)

Example 17 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryController method deleteUserQuery.

// DELETE
@ApiMethod(verb = ApiVerb.DELETE, description = "Deletes an advanced query for the current logged user", produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE })
@RequestMapping(value = "/user/me/queries/{id}", method = { RequestMethod.DELETE })
public void deleteUserQuery(@PathVariable("id") String id, Model model) {
    // Never trust what the users sends to you! Send the query with the correct username, so it will be verified by the service,
    // TODO Is this done on the aspect
    UserQuery q = userQueryService.getUserQueryById(Long.parseLong(id));
    userQueryService.deleteUserQuery(q);
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) ApiMethod(org.jsondoc.core.annotation.ApiMethod)

Example 18 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryControllerIntegrationTest method leonardShouldBeAbleToLookAtHisQueries.

@Test
public void leonardShouldBeAbleToLookAtHisQueries() throws Exception {
    String leonardToken = generateTokenWithExpirationDate("leonard", 1, TimeUnit.DAYS, Arrays.asList("ROLE_USER"));
    // List<UserQuery> getUserQueries()
    String responseString = this.mockMvc.perform(get("/user/me/queries").header("Authorization", "Bearer " + leonardToken).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    List<UserQuery> uqs = new ObjectMapper().readValue(responseString, new TypeReference<List<UserQuery>>() {
    });
    assertTrue(!uqs.isEmpty());
    assertEquals(uqs.size(), 1);
    assertEquals(123456789, uqs.get(0).getUserQueryId());
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) List(java.util.List) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) MVCBaseSecurityTest(org.nextprot.api.web.dbunit.base.mvc.MVCBaseSecurityTest)

Example 19 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryControllerIntegrationTest method leonardShouldBeAbleToUpdateHisQuery.

/* @Test not applicable anymore
	public void othersShouldNotBeAbleToLookAtLeonardsQueries() throws Exception {

		// List<UserQuery> getUserQueries()
		this.mockMvc.perform(get("/user/me/queries").accept(MediaType.APPLICATION_JSON)).
				andExpect(status().isForbidden());
	}*/
// --------------------------------- PUT --------------------------------------------------------------
@Test
public void leonardShouldBeAbleToUpdateHisQuery() throws Exception {
    String leonardToken = generateTokenWithExpirationDate("leonard", 1, TimeUnit.DAYS, Arrays.asList("ROLE_USER"));
    String content = "{\"userQueryId\":123456789,\"title\":\"Awesomely Genious Query\",\"description\":null,\"sparql\":\"some sparql\",\"published\":false,\"owner\":\"test@nextprot.org\",\"ownerId\":0,\"tags\":null,\"ownerName\":\"test@nextprot.org\"}";
    // UserQuery updateAdvancedQuery()
    String responseString = this.mockMvc.perform(put("/user/me/queries/123456789").header("Authorization", "Bearer " + leonardToken).accept(MediaType.APPLICATION_JSON).contentType(MediaType.APPLICATION_JSON).content(content)).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    UserQuery uq = new ObjectMapper().readValue(responseString, UserQuery.class);
    assertEquals("Awesomely Genious Query", uq.getTitle());
    assertTrue(uq.getOwner().equals("leonard"));
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) MVCBaseSecurityTest(org.nextprot.api.web.dbunit.base.mvc.MVCBaseSecurityTest)

Example 20 with UserQuery

use of org.nextprot.api.user.domain.UserQuery in project nextprot-api by calipho-sib.

the class UserQueryControllerIntegrationTest method leonardShouldBeAbleToLookAtHisQuery.

@Test
public void leonardShouldBeAbleToLookAtHisQuery() throws Exception {
    String leonardToken = generateTokenWithExpirationDate("leonard", 1, TimeUnit.DAYS, Arrays.asList("ROLE_USER"));
    // UserQuery getUserQuery()
    String responseString = this.mockMvc.perform(get("/user/me/queries/123456789").header("Authorization", "Bearer " + leonardToken).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn().getResponse().getContentAsString();
    UserQuery uq = new ObjectMapper().readValue(responseString, UserQuery.class);
    // assert that an id was given
    assertTrue(uq.getUserQueryId() == 123456789);
    // asserts that the owner of the query is bob
    assertTrue(uq.getOwner().equals("leonard"));
}
Also used : UserQuery(org.nextprot.api.user.domain.UserQuery) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test) MVCBaseSecurityTest(org.nextprot.api.web.dbunit.base.mvc.MVCBaseSecurityTest)

Aggregations

UserQuery (org.nextprot.api.user.domain.UserQuery)33 Test (org.junit.Test)20 UserResourceBaseTest (org.nextprot.api.user.dao.test.base.UserResourceBaseTest)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 MVCBaseSecurityTest (org.nextprot.api.web.dbunit.base.mvc.MVCBaseSecurityTest)4 ResultSet (java.sql.ResultSet)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 NextProtException (org.nextprot.api.commons.exception.NextProtException)3 NamedParameterJdbcTemplate (org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ApiMethod (org.jsondoc.core.annotation.ApiMethod)2 AbstractUnitBaseTest (org.nextprot.api.commons.dbunit.AbstractUnitBaseTest)2 MapSqlParameterSource (org.springframework.jdbc.core.namedparam.MapSqlParameterSource)2 PrintWriter (java.io.PrintWriter)1 URL (java.net.URL)1 List (java.util.List)1 SearchQueryException (org.nextprot.api.commons.exception.SearchQueryException)1 SparqlEndpoint (org.nextprot.api.rdf.service.SparqlEndpoint)1