diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java index bc3673b27..dbb0436ba 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/AuthDao.java @@ -138,10 +138,6 @@ public static AuthDao getInstance(DSLContext dsl) { return getInstance(dsl, null); } - @Override - public List getAll(String limitToOffice) { - throw new UnsupportedOperationException("Unimplemented method 'getAll'"); - } /** * Reserved for future use, get user principal by presented unique name and office. diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/BlobDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/BlobDao.java index c3ab3ecd4..c6ae052ae 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/BlobDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/BlobDao.java @@ -110,38 +110,11 @@ private static void handleResultSet(ResultSet resultSet, BlobConsumer consumer) consumer.accept(blob, mediaType); } - - @Override - public List getAll(String officeId) { - String queryStr = "SELECT AT_BLOB.ID, AT_BLOB.DESCRIPTION, CWMS_MEDIA_TYPE.MEDIA_TYPE_ID, CWMS_OFFICE.OFFICE_ID\n" - + " FROM CWMS_20.AT_BLOB \n" - + "join CWMS_20.CWMS_MEDIA_TYPE on AT_BLOB.MEDIA_TYPE_CODE = CWMS_MEDIA_TYPE.MEDIA_TYPE_CODE \n" - + "join CWMS_20.CWMS_OFFICE on AT_BLOB.OFFICE_CODE=CWMS_OFFICE.OFFICE_CODE \n" - ; - - ResultQuery query; - if (officeId != null) { - queryStr = queryStr + " and upper(CWMS_OFFICE.OFFICE_ID) = upper(?)"; - query = dsl.resultQuery(queryStr, officeId); - } else { - query = dsl.resultQuery(queryStr); - } - - return query.fetch(r -> { - String rId = r.get("ID", String.class); - String rOffice = r.get("OFFICE_ID", String.class); - String rDesc = r.get("DESCRIPTION", String.class); - String rMedia = r.get("MEDIA_TYPE_ID", String.class); - - return new Blob(rOffice, rId, rDesc, rMedia, null); - }); - } - public List getAll(String officeId, String like) { String queryStr = "SELECT AT_BLOB.ID, AT_BLOB.DESCRIPTION, CWMS_MEDIA_TYPE.MEDIA_TYPE_ID, CWMS_OFFICE.OFFICE_ID\n" + " FROM CWMS_20.AT_BLOB \n" + "join CWMS_20.CWMS_MEDIA_TYPE on AT_BLOB.MEDIA_TYPE_CODE = CWMS_MEDIA_TYPE.MEDIA_TYPE_CODE \n" - + "join CWMS_20.CWMS_OFFICE on AT_BLOB.OFFICE_CODE=CWMS_OFFICE.OFFICE_CODE \n" + + "join CWMS_20.CWMS_OFFICE on AT_BLOB.OFFICE_CODE = CWMS_OFFICE.OFFICE_CODE \n" + " where REGEXP_LIKE (upper(AT_BLOB.ID), upper(?))" ; diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/ClobDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/ClobDao.java index d76e33b3b..f07dc3854 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/ClobDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/ClobDao.java @@ -42,74 +42,48 @@ public ClobDao(DSLContext dsl) { super(dsl); } - - // Yikes, I hate this method - it retrieves all the clobs? That could be gigabytes of data. - // Not returning Value or Desc fields until a useful way of working with this method is - // figured out. - @Override - public List getAll(String limitToOffice) { - AV_CLOB ac = AV_CLOB.AV_CLOB; - AV_OFFICE ao = AV_OFFICE.AV_OFFICE; - - Condition whereCond = noCondition(); - if (limitToOffice != null && !limitToOffice.isEmpty()) { - whereCond = ao.OFFICE_ID.eq(limitToOffice); - } - - return dsl.select(ac.ID, ao.OFFICE_ID) - .from(ac.join(ao).on(ac.OFFICE_CODE.eq(ao.OFFICE_CODE))) - .where(whereCond) - .fetch(joinRecord -> - new Clob(joinRecord.get(ao.OFFICE_ID), - joinRecord.get(ac.ID), null, null)); - - } - @Override public Optional getByUniqueName(String uniqueName, String office) { - AV_CLOB ac = AV_CLOB.AV_CLOB; - AV_OFFICE ao = AV_OFFICE.AV_OFFICE; + AV_CLOB vClob = AV_CLOB.AV_CLOB; + AV_OFFICE vOffice = AV_OFFICE.AV_OFFICE; - Condition cond = ac.ID.eq(uniqueName); + Condition cond = vClob.ID.eq(uniqueName); if (office != null && !office.isEmpty()) { - cond = cond.and(ao.OFFICE_ID.eq(office)); + cond = cond.and(vOffice.OFFICE_ID.eq(office)); } RecordMapper mapper = joinRecord -> - new Clob(joinRecord.getValue(ao.OFFICE_ID), - joinRecord.getValue(ac.ID), - joinRecord.getValue(ac.DESCRIPTION), - joinRecord.getValue(ac.VALUE) + new Clob(joinRecord.getValue(vOffice.OFFICE_ID), + joinRecord.getValue(vClob.ID), + joinRecord.getValue(vClob.DESCRIPTION), + joinRecord.getValue(vClob.VALUE) ); - return dsl.select(ao.OFFICE_ID, ac.asterisk()) - .from(ac.join(ao).on(ac.OFFICE_CODE.eq(ao.OFFICE_CODE))) + return dsl.select(vOffice.OFFICE_ID, vClob.asterisk()) + .from(vClob.join(vOffice).on(vClob.OFFICE_CODE.eq(vOffice.OFFICE_CODE))) .where(cond) .fetchOptional(mapper); } - public Clobs getClobs(String cursor, int pageSize, String officeLike, - boolean includeValues) { - return getClobs(cursor, pageSize, officeLike, includeValues, ".*"); - } - public Clobs getClobs(String cursor, int pageSize, String officeLike, boolean includeValues, String idRegex) { int total = 0; - String clobCursor = "*"; - AV_CLOB v_clob = AV_CLOB.AV_CLOB; - AV_OFFICE v_office = AV_OFFICE.AV_OFFICE; + String cursorOffice = null; + String cursorClobId = null; + AV_CLOB vClob = AV_CLOB.AV_CLOB; + AV_OFFICE vOffice = AV_OFFICE.AV_OFFICE; + Condition whereClause = JooqDao.caseInsensitiveLikeRegex(vClob.ID, idRegex) + .and(JooqDao.caseInsensitiveLikeRegexNullTrue(vOffice.OFFICE_ID, officeLike)); if (cursor == null || cursor.isEmpty()) { - - SelectConditionStep> count = - dsl.select(count(asterisk())) - .from(v_clob) - .join(v_office).on(v_clob.OFFICE_CODE.eq(v_office.OFFICE_CODE)) - .where(JooqDao.caseInsensitiveLikeRegex(v_clob.ID, idRegex)) - .and(officeLike == null ? noCondition() : DSL.upper(v_office.OFFICE_ID).like(officeLike.toUpperCase())); - - total = count.fetchOne().value1(); + SelectConditionStep> count = dsl.select(count(asterisk())) + .from(vClob) + .join(vOffice).on(vClob.OFFICE_CODE.eq(vOffice.OFFICE_CODE)) + .where(whereClause); + Record1 rec = count.fetchOne(); + if(rec != null) { + total = rec.value1(); + } } else { final String[] parts = CwmsDTOPaginated.decodeCursor(cursor, "||"); @@ -119,35 +93,41 @@ public Clobs getClobs(String cursor, int pageSize, String officeLike, } if (parts.length > 1) { - clobCursor = parts[0].split(";")[0]; - clobCursor = clobCursor.substring(clobCursor.indexOf("/") + 1); // ditch the - // officeId that's embedded in + cursorOffice = Clobs.getOffice(cursor); + cursorClobId = Clobs.getId(cursor); total = Integer.parseInt(parts[1]); pageSize = Integer.parseInt(parts[2]); } } + Condition moreInSameOffice = cursorClobId == null || cursorOffice == null ? noCondition() : + vOffice.OFFICE_ID.eq(cursorOffice.toUpperCase()) + .and(upper(vClob.ID).greaterThan(cursorClobId.toUpperCase())); + Condition nextOffices = cursorOffice == null ? noCondition(): + upper(vOffice.OFFICE_ID).greaterThan(cursorOffice.toUpperCase()); + Condition pagingCondition = moreInSameOffice.or(nextOffices); + SelectLimitPercentStep> query = dsl.select( - v_office.OFFICE_ID, - v_clob.ID, - v_clob.DESCRIPTION, - includeValues ? v_clob.VALUE : DSL.inline("").as(v_clob.VALUE) - ) - .from(v_clob) - //.innerJoin(forLimit).on(forLimit.field(v_clob.ID).eq(v_clob.ID)) - .join(v_office).on(v_clob.OFFICE_CODE.eq(v_office.OFFICE_CODE)) - .where(JooqDao.caseInsensitiveLikeRegex(v_clob.ID,idRegex)) - .and(DSL.upper(v_clob.ID).greaterThan(clobCursor)) - .orderBy(v_clob.ID).limit(pageSize); + vOffice.OFFICE_ID, + vClob.ID, + vClob.DESCRIPTION, + includeValues ? vClob.VALUE : DSL.inline("").as(vClob.VALUE) + ) + .from(vClob) + .join(vOffice).on(vClob.OFFICE_CODE.eq(vOffice.OFFICE_CODE)) + .where(whereClause) + .and(pagingCondition) + .orderBy(vOffice.OFFICE_ID, vClob.ID) + .limit(pageSize); - Clobs.Builder builder = new Clobs.Builder(clobCursor, pageSize, total); + Clobs.Builder builder = new Clobs.Builder(cursor, pageSize, total); logger.atFine().log(query.getSQL(ParamType.INLINED)); query.fetch().forEach(row -> { - usace.cwms.db.jooq.codegen.tables.records.AV_CLOB clob = row.into(v_clob); - usace.cwms.db.jooq.codegen.tables.records.AV_OFFICE clobOffice = row.into(v_office); + usace.cwms.db.jooq.codegen.tables.records.AV_CLOB clob = row.into(vClob); + usace.cwms.db.jooq.codegen.tables.records.AV_OFFICE clobOffice = row.into(vOffice); builder.addClob(new Clob( clobOffice.getOFFICE_ID(), clob.getID(), @@ -162,35 +142,26 @@ public Clobs getClobs(String cursor, int pageSize, String officeLike, public List getClobsLike(String office, String idLike) { - AV_CLOB ac = AV_CLOB.AV_CLOB; - AV_OFFICE ao = AV_OFFICE.AV_OFFICE; + AV_CLOB vClob = AV_CLOB.AV_CLOB; + AV_OFFICE vOffice = AV_OFFICE.AV_OFFICE; - Condition cond = DSL.upper(ac.ID).like(idLike.toUpperCase()); + Condition cond = DSL.upper(vClob.ID).like(idLike.toUpperCase()); if (office != null && !office.isEmpty()) { - cond = cond.and(DSL.upper(ao.OFFICE_ID).eq(office.toUpperCase())); + cond = cond.and(DSL.upper(vOffice.OFFICE_ID).eq(office.toUpperCase())); } RecordMapper mapper = joinRecord -> - new Clob(joinRecord.get(ao.OFFICE_ID), - joinRecord.get(ac.ID), - joinRecord.get(ac.DESCRIPTION), - joinRecord.get(ac.VALUE) + new Clob(joinRecord.get(vOffice.OFFICE_ID), + joinRecord.get(vClob.ID), + joinRecord.get(vClob.DESCRIPTION), + joinRecord.get(vClob.VALUE) ); - return dsl.select(ac.asterisk(), ao.OFFICE_ID).from( - ac.join(ao).on(ac.OFFICE_CODE.eq(ao.OFFICE_CODE))).where(cond).fetch(mapper); - } - - public String getClobValue(String office, String id) { - AV_CLOB ac = AV_CLOB.AV_CLOB; - AV_OFFICE ao = AV_OFFICE.AV_OFFICE; - - Condition cond = ac.ID.eq(id).and(ao.OFFICE_ID.eq(office)); - - Record1 clobRecord = dsl.select(ac.VALUE).from( - ac.join(ao).on(ac.OFFICE_CODE.eq(ao.OFFICE_CODE))).where(cond).fetchOne(); - - return clobRecord.value1(); + return dsl.select(vClob.asterisk(), vOffice.OFFICE_ID) + .from(vClob.join(vOffice).on(vClob.OFFICE_CODE.eq(vOffice.OFFICE_CODE))) + .where(cond) + .orderBy(vOffice.OFFICE_ID, vClob.ID) + .fetch(mapper); } public void create(Clob clob, boolean failIfExists) { @@ -224,13 +195,13 @@ public void delete(String officeId, String id) { public void update(Clob clob, boolean ignoreNulls) { - String p_ignore_nulls = getBoolean(ignoreNulls); + String pIgnoreNulls = getBoolean(ignoreNulls); - // Note: when p_ignore_nulls == 'T' and the value or description is "" (not null) + // Note: when pIgnoreNulls == 'T' and the value or description is "" (not null) // the field is not updated. - // Also note: when p_ignore_nulls == 'F' and the value is null + // Also note: when pIgnoreNulls == 'F' and the value is null // it throws - ORA-20244: NULL_ARGUMENT: Argument P_TEXT is not allowed to be null - // Also note: when p_ignore_nulls == 'F' and the value is "" (empty string) + // Also note: when pIgnoreNulls == 'F' and the value is "" (empty string) // it throws - ORA-20244: NULL_ARGUMENT: Argument P_TEXT is not allowed to be null dsl.connection(c -> CWMS_TEXT_PACKAGE.call_UPDATE_TEXT( @@ -238,7 +209,7 @@ public void update(Clob clob, boolean ignoreNulls) { clob.getValue(), clob.getId(), clob.getDescription(), - p_ignore_nulls, + pIgnoreNulls, clob.getOfficeId() ) ); @@ -269,11 +240,7 @@ public void getClob(String clobId, String officeId, ClobConsumer clobConsumer) { try (ResultSet resultSet = preparedStatement.executeQuery()) { if (resultSet.next()) { java.sql.Clob clob = resultSet.getClob("VALUE"); - if (clob != null) { - clobConsumer.accept(clob); - } else { - clobConsumer.accept(null); - } + clobConsumer.accept(clob); } else { throw new NotFoundException("Unable to find clob with id " + clobId + " in office " + officeId); } diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/Dao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/Dao.java index d5634420f..b1d16797e 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/Dao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/Dao.java @@ -32,7 +32,6 @@ import cwms.cda.data.dto.CwmsDTO; import java.sql.Connection; import java.sql.SQLException; -import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; import org.jooq.DSLContext; @@ -107,9 +106,6 @@ protected void setOffice(Connection c, String office) throws SQLException { CWMS_ENV_PACKAGE.call_SET_SESSION_OFFICE_ID(DSL.using(c).configuration(), office); } - - public abstract List getAll(String office); - public abstract Optional getByUniqueName(String uniqueName, String office); /** diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dao/JooqDao.java b/cwms-data-api/src/main/java/cwms/cda/data/dao/JooqDao.java index 8a702c731..5786855bf 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dao/JooqDao.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dao/JooqDao.java @@ -166,11 +166,6 @@ private static Connection setClientInfo(Context ctx, Connection connection) { return connection; } - @Override - public List getAll(String officeId) { - throw new UnsupportedOperationException("Not supported yet."); - } - @Override public Optional getByUniqueName(String uniqueName, String officeId) { throw new UnsupportedOperationException("Not supported yet."); @@ -319,10 +314,8 @@ public static boolean isNotFound(RuntimeException input) { public static boolean isInvalidOffice(RuntimeException input) { return getSqlException(input) - .map(sqlException -> { - return hasCodeOrMessage(sqlException, Collections.singletonList(20010), - Collections.singletonList("INVALID_OFFICE_ID")); - }) + .map(sqlException -> hasCodeOrMessage(sqlException, Collections.singletonList(20010), + Collections.singletonList("INVALID_OFFICE_ID"))) .orElse(false); } diff --git a/cwms-data-api/src/main/java/cwms/cda/data/dto/Clobs.java b/cwms-data-api/src/main/java/cwms/cda/data/dto/Clobs.java index 14c143473..dd2dc5728 100644 --- a/cwms-data-api/src/main/java/cwms/cda/data/dto/Clobs.java +++ b/cwms-data-api/src/main/java/cwms/cda/data/dto/Clobs.java @@ -44,19 +44,53 @@ private void addClob(Clob clob) { clobs.add(clob); } + /** + * Extract the office from the cursor. + * + * @param cursor the cursor + * @return office + */ + public static String getOffice(String cursor) { + String[] parts = CwmsDTOPaginated.decodeCursor(cursor); + if (parts.length > 1) { + String[] idAndOffice = CwmsDTOPaginated.decodeCursor(parts[0]); + if (idAndOffice.length > 0) { + return idAndOffice[0]; + } + } + return null; + } + + /** + * Extract the id from the cursor. + * + * @param cursor the cursor + * @return id + */ + public static String getId(String cursor) { + String[] parts = CwmsDTOPaginated.decodeCursor(cursor); + if (parts.length > 1) { + String[] idAndOffice = CwmsDTOPaginated.decodeCursor(parts[0]); + if (idAndOffice.length > 1) { + return idAndOffice[1]; + } + } + return null; + } + public static class Builder { - private Clobs workingClobs; + private final Clobs workingClobs; public Builder(String cursor, int pageSize, int total) { workingClobs = new Clobs(cursor, pageSize, total); } public Clobs build() { - if (this.workingClobs.clobs.size() == this.workingClobs.pageSize) { - this.workingClobs.nextPage = encodeCursor( - this.workingClobs.clobs.get(this.workingClobs.clobs.size() - 1).toString().toUpperCase(), - this.workingClobs.pageSize, - this.workingClobs.total); + if (this.workingClobs.clobs.size() == this.workingClobs.pageSize && !this.workingClobs.clobs.isEmpty()) { + Clob lastClob = this.workingClobs.clobs.get(this.workingClobs.clobs.size() - 1); + String cursor = encodeCursor(CwmsDTOPaginated.delimiter, lastClob.getOfficeId(), + lastClob.getId()); + this.workingClobs.nextPage = encodeCursor(cursor, this.workingClobs.pageSize, this.workingClobs.total); } else { this.workingClobs.nextPage = null; } diff --git a/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTestIT.java index c25c81ba5..76aea6e21 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/ClobControllerTestIT.java @@ -1,9 +1,9 @@ package cwms.cda.api; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; -import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import cwms.cda.data.dto.Clob; import cwms.cda.formatters.Formats; @@ -13,7 +13,6 @@ import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; - import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -24,6 +23,7 @@ public class ClobControllerTestIT extends DataApiTestIT { public static final String SPK = "SPK"; + private static final String EXISTING_CLOB_ID = "TEST/TEST_CLOBIT2"; private static final String EXISTING_CLOB_VALUE = "test value"; private static final String EXISTING_CLOB_DESC = "test description"; @@ -31,17 +31,18 @@ public class ClobControllerTestIT extends DataApiTestIT { @BeforeAll static void createExistingClob() throws Exception { - Clob clob = new Clob(SPK, EXISTING_CLOB_ID, EXISTING_CLOB_DESC, EXISTING_CLOB_VALUE); + TestAccounts.KeyUser user = TestAccounts.KeyUser.SPK_NORMAL; ObjectMapper om = JsonV2.buildObjectMapper(); + + Clob clob = new Clob(SPK, EXISTING_CLOB_ID, EXISTING_CLOB_DESC, EXISTING_CLOB_VALUE); String serializedClob = om.writeValueAsString(clob); - TestAccounts.KeyUser user = TestAccounts.KeyUser.SPK_NORMAL; given() .log().ifValidationFails(LogDetail.ALL,true) .accept(Formats.JSONV2) .contentType(Formats.JSONV2) .body(serializedClob) .header("Authorization",user.toHeaderValue()) - .queryParam("office",SPK) + .queryParam("office", clob.getOfficeId()) .queryParam("fail-if-exists",false) .when() .redirects().follow(true) @@ -49,7 +50,28 @@ static void createExistingClob() throws Exception .post("/clobs/") .then() .log().ifValidationFails(LogDetail.ALL,true) - .assertThat() + .assertThat() + .statusCode(is(HttpServletResponse.SC_CREATED)); + + //Need to verify that getAll filters to a specific office + user = TestAccounts.KeyUser.SWT_NORMAL; + clob = new Clob(user.getOperatingOffice(), EXISTING_CLOB_ID, EXISTING_CLOB_DESC, EXISTING_CLOB_VALUE); + serializedClob = om.writeValueAsString(clob); + given() + .log().ifValidationFails(LogDetail.ALL, true) + .accept(Formats.JSONV2) + .contentType(Formats.JSONV2) + .body(serializedClob) + .header("Authorization", user.toHeaderValue()) + .queryParam("office", SPK) + .queryParam("fail-if-exists", false) + .when() + .redirects().follow(true) + .redirects().max(3) + .post("/clobs/") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() .statusCode(is(HttpServletResponse.SC_CREATED)); } @@ -59,13 +81,13 @@ void test_getOne_notFound() throws UnsupportedEncodingException { String urlencoded = URLEncoder.encode(clobId, "UTF-8"); given() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .accept(Formats.JSONV2) .queryParam(Controllers.OFFICE, SPK) .when() .get("/clobs/" + urlencoded) .then() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_NOT_FOUND)); } @@ -80,13 +102,13 @@ of the object name (NOTE: good candidate for actually having a GUID or other "co given() .accept(Formats.JSONV2) - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .queryParam(Controllers.OFFICE, SPK) .queryParam(Controllers.CLOB_ID, EXISTING_CLOB_ID) .when() .get("/clobs/ignored") .then() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) .body("office-id", is(SPK)) @@ -101,14 +123,14 @@ void test_getOne_plainText_withRange() // We can now do Range requests! given() .accept("text/plain") - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .queryParam(Controllers.OFFICE, SPK) .queryParam(Controllers.CLOB_ID, EXISTING_CLOB_ID) .header("Range"," bytes=3-") .when() .get("/clobs/ignored") .then() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_PARTIAL_CONTENT)) .body( is("t value")); @@ -119,18 +141,46 @@ void test_getOne_plain_text() { given() .accept("text/plain") - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .queryParam(Controllers.OFFICE, SPK) .queryParam(Controllers.CLOB_ID, EXISTING_CLOB_ID) .when() .get("/clobs/ignored") .then() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) .body( is(EXISTING_CLOB_VALUE)); } + @Test + void test_getAll_specific_office() + { + given() + .log().ifValidationFails(LogDetail.ALL, true) + .queryParam(Controllers.OFFICE, SPK) + .accept(Formats.JSON) + .when() + .get("/clobs/") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body("clobs.office-id", hasItem(SPK)); + + given() + .log().ifValidationFails(LogDetail.ALL, true) + .queryParam(Controllers.OFFICE, TestAccounts.KeyUser.SWT_NORMAL.getOperatingOffice()) + .accept(Formats.JSON) + .when() + .get("/clobs/") + .then() + .log().ifValidationFails(LogDetail.ALL, true) + .assertThat() + .statusCode(is(HttpServletResponse.SC_OK)) + .body("clobs.office-id", hasItem(TestAccounts.KeyUser.SWT_NORMAL.getOperatingOffice())); + } + @ParameterizedTest @EnumSource(GetAllTest.class) @@ -138,13 +188,13 @@ void test_getAll_aliases(GetAllTest test) { given() .accept("text/plain") - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .queryParam(Controllers.OFFICE, SPK) .accept(test._accept) .when() .get("/clobs/") .then() - .log().ifValidationFails(LogDetail.ALL,true) + .log().ifValidationFails(LogDetail.ALL, true) .assertThat() .statusCode(is(HttpServletResponse.SC_OK)) .contentType(is(test._expectedContentType)); diff --git a/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java b/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java index b8443bd97..350bde633 100644 --- a/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java +++ b/cwms-data-api/src/test/java/cwms/cda/data/dto/ClobTest.java @@ -3,16 +3,13 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.codahale.metrics.MetricRegistry; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import cwms.cda.api.ClobController; import cwms.cda.formatters.Formats; import cwms.cda.formatters.json.JsonV2; import cwms.cda.formatters.xml.XMLv2; -import javax.xml.bind.JAXBException; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -41,26 +38,7 @@ void testRoundtripJSON() throws JsonProcessingException @Test - void testRoundtripXML() throws JAXBException { - Clob clob = new Clob("MYOFFICE", "MYID", "MYDESC", "MYVALUE"); - - XMLv2 xml = new XMLv2(); - String output = xml.format(clob); - - assertNotNull(output); - - Clob clob2 = Formats.parseContent(Formats.parseHeader(Formats.XMLV2, Clob.class), output, Clob.class); - - assertNotNull(clob2); - - assertEquals(clob.getId(), clob2.getId()); - assertEquals(clob.getOfficeId(), clob2.getOfficeId()); - assertEquals(clob.getDescription(), clob2.getDescription()); - assertEquals(clob.getValue(), clob2.getValue()); - } - - @Test - void testRoundtripXML2() throws JsonProcessingException { + void testRoundtripXML2() { Clob clob = new Clob("MYOFFICE", "MYID", "MYDESC", "MYVALUE"); XMLv2 xml = new XMLv2(); @@ -102,7 +80,9 @@ void testRoundtripXML2Clobs(String format) { assertEquals(1, clobs2.getPageSize()); assertEquals(1, clobs2.getTotal()); assertEquals("Y3Vyc29yfHwxfHwx", clobs2.getPage()); - assertEquals("TVlPRkZJQ0UvTVlJRDtERVNDUklQVElPTj1NWURFU0N8fDF8fDE=", clobs2.getNextPage()); + assertEquals("VFZsUFJrWkpRMFY4ZkUxWlNVUT18fDF8fDE=", clobs2.getNextPage()); + assertEquals(clob.getOfficeId(), Clobs.getOffice(clobs2.getNextPage())); + assertEquals(clob.getId(), Clobs.getId(clobs2.getNextPage())); assertEquals(clob.getId(), clobs2.getClobs().get(0).getId()); assertEquals(clob.getOfficeId(), clobs2.getClobs().get(0).getOfficeId()); assertEquals(clob.getDescription(), clobs2.getClobs().get(0).getDescription());