From 7629de1b49341ed3096b9e0323672fa4d2cf8de7 Mon Sep 17 00:00:00 2001 From: Karthik Kalletla Date: Mon, 19 Aug 2019 14:27:56 -0400 Subject: [PATCH 1/2] Update spring boot to 2.3.3.RELEASE This is part of an effort to synchronize the spring-boot and spring versions across our repositories. Co-authored-by: Niklas Reimer @nr23730 Co-authored-by: Karthik Kalletla Co-authored-by: Angelica Ochoa <15623749+ao508@users.noreply.github.com> Signed-off-by: Angelica Ochoa <15623749+ao508@users.noreply.github.com> --- pom.xml | 249 +++++++++--------- .../session_service/SessionService.java | 2 +- .../session_service/domain/Session.java | 20 +- .../internal/SessionRepositoryCustom.java | 2 +- .../internal/SessionRepositoryImpl.java | 15 +- .../service/internal/SessionServiceImpl.java | 47 ++-- .../session_service/SessionServiceTest.java | 50 ++-- 7 files changed, 184 insertions(+), 201 deletions(-) diff --git a/pom.xml b/pom.xml index 0d3d29f..7bb4c9e 100644 --- a/pom.xml +++ b/pom.xml @@ -1,128 +1,125 @@ - - 4.0.0 - Session Service - RESTful API to cBioPortal/cbioportal sessions in MongoDB. - https://github.com/cBioPortal/session-service/ - org.cbioportal.session_service - session_service - 0.3.0 - ${packaging.type} - - - + + 4.0.0 + Session Service + RESTful API to cBioPortal/cbioportal sessions in MongoDB. + https://github.com/cBioPortal/session-service/ + org.cbioportal.session_service + session_service + 0.2.0 + ${packaging.type} + + + org.springframework.boot + spring-boot-starter-parent + 2.3.3.RELEASE + + + + war + + war + + + + jar + + + true + + + jar + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + org.springframework.boot + spring-boot-starter-security + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + test + + + io.springfox + springfox-swagger2 + 2.8.0 + + + io.springfox + springfox-swagger-ui + 2.8.0 + + + io.sentry + sentry + 1.7.5 + + + javax.validation + validation-api + 2.0.1.Final + + + org.hibernate + hibernate-validator + 6.0.13.Final + + + + 1.8 + + + + org.springframework.boot - spring-boot-starter-parent - 1.3.5.RELEASE - - - - - war - - war - - - - jar - - - true - - - jar - - - - - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-tomcat - provided - - - org.springframework.boot - spring-boot-starter-data-mongodb - - - org.springframework.boot - spring-boot-starter-security - - - de.flapdoodle.embed - de.flapdoodle.embed.mongo - test - - - com.fasterxml.jackson.core - jackson-annotations - 2.7.0 - - - io.springfox - springfox-swagger2 - 2.8.0 - - - io.springfox - springfox-swagger-ui - 2.8.0 - - - io.sentry - sentry - 1.7.5 - - - - - 1.8 - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - repackage - - - - - - org.apache.maven.plugins - maven-jar-plugin - - - package - - jar - - - model - - org/cbioportal/session_service/domain/** - - - - - - - - - + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + package + + jar + + + model + + org/cbioportal/session_service/domain/** + + + + + + + + \ No newline at end of file diff --git a/src/main/java/org/cbioportal/session_service/SessionService.java b/src/main/java/org/cbioportal/session_service/SessionService.java index fee7a0a..69e9187 100644 --- a/src/main/java/org/cbioportal/session_service/SessionService.java +++ b/src/main/java/org/cbioportal/session_service/SessionService.java @@ -34,7 +34,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.context.web.SpringBootServletInitializer; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.data.mongodb.core.mapping.event.ValidatingMongoEventListener; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.context.annotation.Bean; diff --git a/src/main/java/org/cbioportal/session_service/domain/Session.java b/src/main/java/org/cbioportal/session_service/domain/Session.java index c26e406..accb3c3 100644 --- a/src/main/java/org/cbioportal/session_service/domain/Session.java +++ b/src/main/java/org/cbioportal/session_service/domain/Session.java @@ -32,19 +32,17 @@ package org.cbioportal.session_service.domain; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonInclude.Include; +import com.fasterxml.jackson.annotation.JsonView; +import com.mongodb.BasicDBObject; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; - import org.springframework.data.annotation.Id; import org.springframework.util.DigestUtils; -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.annotation.JsonInclude.Include; -import com.fasterxml.jackson.annotation.JsonView; -import com.mongodb.util.JSON; // save as JSON, not String of JSON - /** - * @author Manda Wilson + * @author Manda Wilson */ @JsonInclude(Include.NON_NULL) public class Session { @@ -59,7 +57,7 @@ public class Session { private String source; @NotNull private SessionType type; - + @JsonView(Session.Views.IdOnly.class) public String getId() { @@ -72,12 +70,12 @@ public String getChecksum() { public void setData(Object data) { if(data instanceof String) { - this.data = JSON.parse((String)data); + this.data = BasicDBObject.parse((String)data); } else { - this.data = data; + this.data = data; } // JSON.serialize it so that formatting is the same if we test later - this.checksum = DigestUtils.md5DigestAsHex(JSON.serialize(this.data).getBytes()); + this.checksum = DigestUtils.md5DigestAsHex(((BasicDBObject)this.data).toString().getBytes()); } @JsonView(Session.Views.Full.class) diff --git a/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryCustom.java b/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryCustom.java index bd1d620..c207844 100644 --- a/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryCustom.java +++ b/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryCustom.java @@ -52,7 +52,7 @@ public interface SessionRepositoryCustom { List findBySourceAndType(String source, SessionType type); - int deleteBySourceAndTypeAndId(String source, SessionType type, String id); + long deleteBySourceAndTypeAndId(String source, SessionType type, String id); List findBySourceAndTypeAndQuery(String source, SessionType type, String query); diff --git a/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryImpl.java b/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryImpl.java index 7f19bd4..d7005cc 100644 --- a/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryImpl.java +++ b/src/main/java/org/cbioportal/session_service/domain/internal/SessionRepositoryImpl.java @@ -32,13 +32,12 @@ package org.cbioportal.session_service.domain.internal; +import org.bson.Document; import org.cbioportal.session_service.domain.Session; import org.cbioportal.session_service.domain.SessionType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; -import com.mongodb.DBObject; -import com.mongodb.BasicDBObject; import org.springframework.data.mongodb.core.index.CompoundIndexDefinition; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.BasicQuery; @@ -61,10 +60,10 @@ public class SessionRepositoryImpl implements SessionRepositoryCustom { public void saveSession(Session session) { if (!this.mongoTemplate.collectionExists(session.getType().toString())) { this.mongoTemplate.createCollection(session.getType().toString()); - DBObject indexKeys = new BasicDBObject(); - indexKeys.put("source", 1); - indexKeys.put("type", 1); - indexKeys.put("checksum", 1); + Document indexKeys = new Document(); + indexKeys.append("source", 1); + indexKeys.append("type", 1); + indexKeys.append("checksum", 1); this.mongoTemplate.indexOps(session.getType().toString()).ensureIndex( new CompoundIndexDefinition(indexKeys).unique()); } @@ -93,10 +92,10 @@ public List findBySourceAndType(String source, SessionType type) { Session.class, type.toString()); } - public int deleteBySourceAndTypeAndId(String source, SessionType type, String id) { + public long deleteBySourceAndTypeAndId(String source, SessionType type, String id) { return this.mongoTemplate.remove( new Query(Criteria.where("source").is(source).and("type").is(type).and("id").is(id)), - Session.class, type.toString()).getN(); + Session.class, type.toString()).getDeletedCount(); } public List findBySourceAndTypeAndQuery(String source, SessionType type, String query) { diff --git a/src/main/java/org/cbioportal/session_service/service/internal/SessionServiceImpl.java b/src/main/java/org/cbioportal/session_service/service/internal/SessionServiceImpl.java index 95bde7d..29695e8 100644 --- a/src/main/java/org/cbioportal/session_service/service/internal/SessionServiceImpl.java +++ b/src/main/java/org/cbioportal/session_service/service/internal/SessionServiceImpl.java @@ -32,27 +32,24 @@ package org.cbioportal.session_service.service.internal; -import org.cbioportal.session_service.service.SessionService; -import org.cbioportal.session_service.service.exception.*; +import java.util.List; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import org.bson.BSONException; +import org.bson.json.JsonParseException; import org.cbioportal.session_service.domain.Session; import org.cbioportal.session_service.domain.SessionRepository; import org.cbioportal.session_service.domain.SessionType; - -import com.mongodb.util.JSONParseException; -import java.lang.IllegalArgumentException; -import org.springframework.data.mongodb.UncategorizedMongoDbException; - -import javax.validation.ConstraintViolationException; -import javax.validation.ConstraintViolation; - -import org.springframework.dao.DuplicateKeyException; +import org.cbioportal.session_service.service.SessionService; +import org.cbioportal.session_service.service.exception.*; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DuplicateKeyException; +import org.springframework.data.mongodb.UncategorizedMongoDbException; +import org.springframework.http.converter.HttpMessageNotReadableException; import org.springframework.stereotype.Service; -import java.util.List; - /** - * @author Manda Wilson + * @author Manda Wilson */ @Service public class SessionServiceImpl implements SessionService { @@ -62,13 +59,13 @@ public class SessionServiceImpl implements SessionService { @Override public Session addSession(String source, SessionType type, String data) throws SessionInvalidException { - Session session = null; + Session session = null; try { session = new Session(); session.setSource(source); session.setType(type); session.setData(data); - + sessionRepository.saveSession(session); } catch (DuplicateKeyException e) { session = sessionRepository.findOneBySourceAndTypeAndChecksum(source, @@ -76,7 +73,9 @@ public Session addSession(String source, SessionType type, String data) throws S session.getChecksum()); } catch (ConstraintViolationException e) { throw new SessionInvalidException(buildConstraintViolationExceptionMessage(e)); - } catch (JSONParseException e) { + } catch (JsonParseException e) { + throw new SessionInvalidException(e.getMessage()); + } catch (HttpMessageNotReadableException e) { throw new SessionInvalidException(e.getMessage()); } return session; @@ -88,11 +87,11 @@ public List getSessions(String source, SessionType type) { } @Override - public List getSessionsByQuery(String source, SessionType type, String query) + public List getSessionsByQuery(String source, SessionType type, String query) throws SessionQueryInvalidException { try { return sessionRepository.findBySourceAndTypeAndQuery(source, type, query); - } catch (IllegalArgumentException e) { + } catch (IllegalArgumentException | JsonParseException | BSONException e) { throw new SessionQueryInvalidException(e.getMessage()); } catch (UncategorizedMongoDbException e) { throw new SessionQueryInvalidException(e.getMessage()); @@ -109,7 +108,7 @@ public Session getSession(String source, SessionType type, String id) throws Ses } @Override - public void updateSession(String source, SessionType type, String id, String data) throws SessionInvalidException, + public void updateSession(String source, SessionType type, String id, String data) throws SessionInvalidException, SessionNotFoundException { Session savedSession = sessionRepository.findOneBySourceAndTypeAndId(source, type, id); if (savedSession != null) { @@ -118,9 +117,9 @@ public void updateSession(String source, SessionType type, String id, String dat sessionRepository.saveSession(savedSession); } catch (ConstraintViolationException e) { throw new SessionInvalidException(buildConstraintViolationExceptionMessage(e)); - } catch (JSONParseException e) { + } catch (JsonParseException e) { throw new SessionInvalidException(e.getMessage()); - } + } return; } throw new SessionNotFoundException(id); @@ -128,8 +127,8 @@ public void updateSession(String source, SessionType type, String id, String dat @Override public void deleteSession(String source, SessionType type, String id) throws SessionNotFoundException { - int numberDeleted = sessionRepository.deleteBySourceAndTypeAndId(source, type, id); - if (numberDeleted != 1) { // using unique id so never more than 1 + long numberDeleted = sessionRepository.deleteBySourceAndTypeAndId(source, type, id); + if (numberDeleted != 1) { // using unique id so never more than 1 throw new SessionNotFoundException(id); } } diff --git a/src/test/java/org/cbioportal/session_service/SessionServiceTest.java b/src/test/java/org/cbioportal/session_service/SessionServiceTest.java index 21874a6..619a756 100644 --- a/src/test/java/org/cbioportal/session_service/SessionServiceTest.java +++ b/src/test/java/org/cbioportal/session_service/SessionServiceTest.java @@ -32,46 +32,34 @@ package org.cbioportal.session_service; -import static org.hamcrest.Matchers.*; -import static org.junit.Assert.assertThat; - import java.net.URL; - +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import static org.hamcrest.Matchers.*; import org.junit.*; +import static org.junit.Assert.assertThat; import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.test.IntegrationTest; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.boot.test.TestRestTemplate; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.server.LocalServerPort; import org.springframework.http.*; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.web.client.RestTemplate; - -import java.util.regex.Pattern; -import java.util.List; -import java.util.ArrayList; -import java.util.regex.Matcher; /** * @author Manda Wilson */ @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = SessionService.class) -@WebAppConfiguration -// pick random port for testing -@IntegrationTest({"server.port=0"}) -// use application-test.properties config file -@ActiveProfiles("test") +@SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SessionService.class, value= {"server.error.include-exception=true"}) public class SessionServiceTest { // get randomly assigned port - @Value("${local.server.port}") + @LocalServerPort private int port; private URL base; - private RestTemplate template; + private TestRestTemplate template; @Before public void setUp() throws Exception { @@ -138,13 +126,15 @@ public void addSessionNoData() throws Exception { assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); response = addData("msk_portal", "main_session", null); - assertThat(response.getBody(), containsString("Required request body is missing")); + assertThat(response.getBody(), containsString("org.springframework.http.converter.HttpMessageNotReadableException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @Test public void addSessionInvalidData() throws Exception { ResponseEntity response = addData("msk_portal", "main_session", "\"portal-session\":blah blah blah"); + System.out.println("&&&&&&&&&&"); + System.out.println(response); assertThat(response.getBody(), containsString("org.cbioportal.session_service.service.exception.SessionInvalidException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @@ -253,7 +243,7 @@ public void getSessionWithQueryNullCharacterInField() throws Exception { // now query response = template.getForEntity(base.toString() + "msk_portal/main_session/" + "query?field=data.p\0ortal-session.title&value=my portal session", String.class); - assertThat(response.getBody(), containsString("Document field names can't have a NULL character")); + assertThat(response.getBody(), containsString("org.cbioportal.session_service.service.exception.SessionQueryInvalidException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @@ -265,7 +255,7 @@ public void getSessionWithQueryFieldStartsWithDollarSign() throws Exception { // now query response = template.getForEntity(base.toString() + "msk_portal/main_session/" + "query?field=$data.portal-session.title&value=my portal session", String.class); - assertThat(response.getBody(), containsString("Can't canonicalize query")); + assertThat(response.getBody(), containsString("org.cbioportal.session_service.service.exception.SessionQueryInvalidException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @@ -293,7 +283,7 @@ public void fetchSessionWithQueryNullCharacterInField() throws Exception { // now query response = template.exchange(base.toString() + "msk_portal/main_session/query/fetch", HttpMethod.POST, entity, String.class); - assertThat(response.getBody(), containsString("Document field names can't have a NULL character")); + assertThat(response.getBody(), containsString("org.cbioportal.session_service.service.exception.SessionQueryInvalidException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @@ -307,7 +297,7 @@ public void fetchSessionWithQueryFieldStartsWithDollarSign() throws Exception { // now query response = template.exchange(base.toString() + "msk_portal/main_session/query/fetch", HttpMethod.POST, entity, String.class); - assertThat(response.getBody(), containsString("Can't canonicalize query")); + assertThat(response.getBody(), containsString("org.cbioportal.session_service.service.exception.SessionQueryInvalidException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } @@ -375,7 +365,7 @@ public void updateSessionNoData() throws Exception { HttpEntity entity = prepareData(null); response = template.exchange(base.toString() + "msk_portal/main_session/" + id, HttpMethod.PUT, entity, String.class); - assertThat(response.getBody(), containsString("Required request body is missing")); + assertThat(response.getBody(), containsString("org.springframework.http.converter.HttpMessageNotReadableException")); assertThat(response.getStatusCode(), equalTo(HttpStatus.BAD_REQUEST)); } From 6e7301631c2d306ccd22f9640eea3eb24f8f3aba Mon Sep 17 00:00:00 2001 From: Ino de Bruijn Date: Tue, 16 Feb 2021 10:12:36 -0500 Subject: [PATCH 2/2] fix version --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7bb4c9e..d43a00f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ https://github.com/cBioPortal/session-service/ org.cbioportal.session_service session_service - 0.2.0 + 0.4.0 ${packaging.type} @@ -122,4 +122,4 @@ - \ No newline at end of file +