Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feature/update_jooq_version' int…
Browse files Browse the repository at this point in the history
…o feature/text_time_series
  • Loading branch information
rma-rripken committed Dec 28, 2023
2 parents 8530ecf + f4fcf10 commit 9753b29
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 145 deletions.
6 changes: 3 additions & 3 deletions cwms-data-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ dependencies {
exclude group: "mil.army.usace.hec", module: "cwms-db-jooq-codegen"

}
implementation('mil.army.usace.hec:cwms-db-jooq-codegen_java8:23.03.16-1.0.2') { //was 7.0.0-SNAPSHOT, 23.03.16 or cwms-db-jooq-codegen_java11 23.03.16-1.0.2
implementation('mil.army.usace.hec:cwms-db-jooq-codegen_java8:23.03.16-1.0.2') {
exclude group: "com.oracle", module: "*"
exclude group: "com.oracle.database.jdbc", module: "*"
exclude group: "org.jooq.pro", module: "*"
Expand Down Expand Up @@ -69,7 +69,7 @@ dependencies {
exclude group: "mil.army.usace.hec", module: "cwms-db-jooq-codegen"
}

implementation('org.jooq.pro-java-8:jooq:3.18.7') {
implementation('org.jooq.pro-java-8:jooq:3.18.7-jdk8RC02') {
exclude group: "com.oracle", module: "*"
exclude group: "com.oracle.database.jdbc", module: "*"
exclude group: "javax.xml.bind", module: "*"
Expand Down Expand Up @@ -322,4 +322,4 @@ task generateTimeSeriesSamples(type: JavaExec) {
args url
args user
args password
}
}
16 changes: 8 additions & 8 deletions cwms-data-api/src/main/java/cwms/cda/api/ClobController.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ protected DSLContext getDslContext(Context ctx) {
tags = {TAG}
)
@Override
public void getAll(Context ctx) {
public void getAll(@NotNull Context ctx) {

try (final Timer.Context ignored = markAndTime(GET_ALL);) {
DSLContext dsl = getDslContext(ctx);
try (final Timer.Context ignored = markAndTime(GET_ALL)) {
DSLContext dsl = getDslContext(ctx);
String office = ctx.queryParam(OFFICE);
Optional<String> officeOpt = Optional.ofNullable(office);

Expand Down Expand Up @@ -175,10 +175,10 @@ public void getAll(Context ctx) {
tags = {TAG}
)
@Override
public void getOne(Context ctx, @NotNull String clobId) {
public void getOne(@NotNull Context ctx, @NotNull String clobId) {

try (final Timer.Context ignored = markAndTime(GET_ONE);) {
DSLContext dsl = getDslContext(ctx);
try (final Timer.Context ignored = markAndTime(GET_ONE)) {
DSLContext dsl = getDslContext(ctx);
ClobDao dao = new ClobDao(dsl);
Optional<String> office = Optional.ofNullable(ctx.queryParam(OFFICE));
Optional<Clob> optAc = dao.getByUniqueName(clobId, office);
Expand Down Expand Up @@ -219,8 +219,8 @@ public void getOne(Context ctx, @NotNull String clobId) {
tags = {TAG}
)
@Override
public void create(Context ctx) {
try (final Timer.Context ignored = markAndTime(CREATE)){
public void create(@NotNull Context ctx) {
try (final Timer.Context ignored = markAndTime(CREATE)) {
DSLContext dsl = getDslContext(ctx);

String reqContentType = ctx.req.getContentType();
Expand Down
29 changes: 16 additions & 13 deletions cwms-data-api/src/main/java/cwms/cda/api/CountyController.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@

package cwms.cda.api;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.RESULTS;
import static cwms.cda.api.Controllers.SIZE;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
Expand All @@ -38,18 +44,14 @@
import io.javalin.plugin.openapi.annotations.OpenApi;
import io.javalin.plugin.openapi.annotations.OpenApiContent;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;
import org.jooq.DSLContext;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.*;
import static cwms.cda.data.dao.JooqDao.getDslContext;
import javax.servlet.http.HttpServletResponse;
import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

/**
* Handles all county CRUD methods.
*
*
* @see CountyController
*/
public class CountyController implements CrudHandler {
Expand All @@ -76,13 +78,14 @@ private Timer.Context markAndTime(String subject) {
@OpenApiResponse(status = "" + HttpServletResponse.SC_OK,
description = "A list of counties.",
content = {
@OpenApiContent(from = County.class, isArray = true, type = Formats.JSONV2),
@OpenApiContent(from = County.class, isArray = true,
type = Formats.JSONV2),
}),
},
tags = {"Counties"}
)
@Override
public void getAll(Context ctx) {
public void getAll(@NotNull Context ctx) {
try (Timer.Context timeContext = markAndTime(GET_ALL)) {
DSLContext dsl = getDslContext(ctx);
CountyDao dao = new CountyDao(dsl);
Expand All @@ -98,7 +101,7 @@ public void getAll(Context ctx) {

@OpenApi(ignore = true)
@Override
public void getOne(Context ctx, String county) {
public void getOne(Context ctx, @NotNull String county) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
}

Expand All @@ -110,13 +113,13 @@ public void create(Context ctx) {

@OpenApi(ignore = true)
@Override
public void update(Context ctx, String county) {
public void update(Context ctx, @NotNull String county) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
}

@OpenApi(ignore = true)
@Override
public void delete(Context ctx, String county) {
public void delete(Context ctx, @NotNull String county) {
ctx.status(HttpServletResponse.SC_NOT_IMPLEMENTED).json(CdaError.notImplemented());
}

Expand Down
36 changes: 18 additions & 18 deletions cwms-data-api/src/main/java/cwms/cda/api/auth/ApiKeyController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package cwms.cda.api.auth;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.RESULTS;
import static cwms.cda.api.Controllers.SIZE;
import static cwms.cda.api.Controllers.STATUS_201;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;

import cwms.cda.api.errors.CdaError;
import cwms.cda.api.errors.NotFoundException;
import cwms.cda.data.dao.AuthDao;
import cwms.cda.data.dto.auth.ApiKey;
import cwms.cda.formatters.Formats;
Expand All @@ -18,13 +22,8 @@
import io.javalin.plugin.openapi.annotations.OpenApiParam;
import io.javalin.plugin.openapi.annotations.OpenApiRequestBody;
import io.javalin.plugin.openapi.annotations.OpenApiResponse;

import static com.codahale.metrics.MetricRegistry.name;
import static cwms.cda.api.Controllers.*;
import static cwms.cda.data.dao.JooqDao.getDslContext;

import java.util.List;

import org.jetbrains.annotations.NotNull;
import org.jooq.DSLContext;

public class ApiKeyController implements CrudHandler {
Expand Down Expand Up @@ -61,14 +60,14 @@ public void create(Context ctx) {
AuthDao auth = AuthDao.getInstance(dsl);
ApiKey sourceData = ctx.bodyAsClass(ApiKey.class);
ApiKey key = auth.createApiKey(p, sourceData);
if(key == null) {
if (key == null) {
ctx.status(HttpCode.BAD_REQUEST);
} else {
ctx.json(key).status(HttpCode.CREATED);
}
} catch (CwmsAuthException ex) {
if( ex.getMessage().equals(AuthDao.ONLY_OWN_KEY_MESSAGE)) {
ctx.json(new CdaError(ex.getMessage(),true)).status(ex.getAuthFailCode());
if (ex.getMessage().equals(AuthDao.ONLY_OWN_KEY_MESSAGE)) {
ctx.json(new CdaError(ex.getMessage(), true)).status(ex.getAuthFailCode());
} else {
throw ex;
}
Expand All @@ -91,13 +90,14 @@ public void create(Context ctx) {
tags = {"Authorization"}
)
@Override
public void delete(Context ctx, String keyName) {
public void delete(@NotNull Context ctx, @NotNull String keyName) {
DataApiPrincipal p = ctx.attribute(AuthDao.DATA_API_PRINCIPAL);

DSLContext dsl = getDslContext(ctx);

AuthDao auth = AuthDao.getInstance(dsl);
auth.deleteKeyForUser(p, keyName);
ctx.status(HttpCode.NO_CONTENT);

}

@OpenApi(
Expand All @@ -114,6 +114,7 @@ public void getAll(Context ctx) {
DataApiPrincipal p = ctx.attribute(AuthDao.DATA_API_PRINCIPAL);

DSLContext dsl = getDslContext(ctx);

AuthDao auth = AuthDao.getInstance(dsl);
List<ApiKey> keys = auth.apiKeysForUser(p);
ctx.json(keys).status(HttpCode.OK);
Expand All @@ -122,9 +123,7 @@ public void getAll(Context ctx) {

@OpenApi(
pathParams = {
@OpenApiParam(
name="key-name",
required = true,
@OpenApiParam(name = "key-name", required = true,
description = "Name of the specific key to get more information for. NOTE: Case-sensitive.")
},
responses = @OpenApiResponse(
Expand All @@ -137,7 +136,7 @@ public void getAll(Context ctx) {
tags = {"Authorization"}
)
@Override
public void getOne(Context ctx, String keyName) {
public void getOne(Context ctx, @NotNull String keyName) {
DataApiPrincipal p = ctx.attribute(AuthDao.DATA_API_PRINCIPAL);
DSLContext dsl = getDslContext(ctx);
AuthDao auth = AuthDao.getInstance(dsl);
Expand All @@ -151,13 +150,14 @@ public void getOne(Context ctx, String keyName) {
);
ctx.json(msg).status(HttpCode.NOT_FOUND);
}

}

@OpenApi(
ignore = true // users should delete and recreate keys. There is nothing to update.
)
@Override
public void update(Context ctx, String arg1) {
public void update(@NotNull Context ctx, @NotNull String arg1) {
throw new UnsupportedOperationException("Update is not implemented. Delete and create a new key.");
}

Expand Down
64 changes: 27 additions & 37 deletions cwms-data-api/src/test/java/cwms/cda/api/CatalogControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,8 @@
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import java.io.InputStream;
import java.sql.Connection;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.codahale.metrics.MetricRegistry;

import cwms.cda.formatters.FormattingException;

import org.jooq.tools.jdbc.MockConnection;
import org.jooq.tools.jdbc.MockFileDatabase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import fixtures.TestHttpServletResponse;
import fixtures.TestServletInputStream;
import io.javalin.core.util.Header;
Expand All @@ -35,36 +18,43 @@
import io.javalin.http.util.ContextUtil;
import io.javalin.plugin.json.JavalinJackson;
import io.javalin.plugin.json.JsonMapperKt;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

public class CatalogControllerTest extends ControllerTest{
public class CatalogControllerTest extends ControllerTest {

@Disabled // get all the infrastructure in place first.
@ParameterizedTest
@ValueSource(strings = {"blurge,","appliation/json+fred"})
public void bad_formats_return_501(String format ) throws Exception {
@ValueSource(strings = {"blurge,", "appliation/json+fred"})
public void bad_formats_return_501(String format) throws Exception {
final String testBody = "test";
CatalogController controller = spy(new CatalogController(new MetricRegistry()));

HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = mock(HttpServletResponse.class);
HashMap<String,Object> attributes = new HashMap<>();
attributes.put(ContextUtil.maxRequestSizeKey,Integer.MAX_VALUE);
HashMap<String, Object> attributes = new HashMap<>();
attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);

when(request.getInputStream()).thenReturn(new TestServletInputStream(testBody));
when(request.getAttribute("database")).thenReturn(this.conn);
when(request.getRequestURI()).thenReturn("/catalog/TIMESERIES");

Context context = ContextUtil.init(request,response,"*",new HashMap<String,String>(), HandlerType.GET,attributes);
context.attribute("database",this.conn);
Context context = ContextUtil.init(request, response, "*", new HashMap<>(),
HandlerType.GET, attributes);
context.attribute("database", this.conn);


assertNotNull( context.attribute("database"), "could not get the connection back as an attribute");
assertNotNull(context.attribute("database"), "could not get the connection back as an "
+ "attribute");

when(request.getHeader(Header.ACCEPT)).thenReturn("BAD FORMAT");

assertThrows( FormattingException.class, () -> {
controller.getAll(context);
});
assertThrows(FormattingException.class, () -> controller.getAll(context));
}

@Test
Expand All @@ -73,9 +63,9 @@ public void catalog_returns_only_original_ids_by_default() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
HttpServletResponse response = new TestHttpServletResponse();

HashMap<String,Object> attributes = new HashMap<>();
attributes.put(ContextUtil.maxRequestSizeKey,Integer.MAX_VALUE);
attributes.put(JsonMapperKt.JSON_MAPPER_KEY,new JavalinJackson());
HashMap<String, Object> attributes = new HashMap<>();
attributes.put(ContextUtil.maxRequestSizeKey, Integer.MAX_VALUE);
attributes.put(JsonMapperKt.JSON_MAPPER_KEY, new JavalinJackson());
attributes.put("PolicyFactory", this.sanitizer);

when(request.getInputStream()).thenReturn(new TestServletInputStream(""));
Expand All @@ -84,12 +74,12 @@ public void catalog_returns_only_original_ids_by_default() throws Exception {
when(request.getHeader(Header.ACCEPT)).thenReturn("application/json;version=2");

Context context = ContextUtil.init(request,
response,
"*",
new HashMap<String,String>(),
HandlerType.GET,
attributes);
context.attribute("database",this.conn);
response,
"*",
new HashMap<>(),
HandlerType.GET,
attributes);
context.attribute("database", this.conn);

controller.getOne(context, CatalogableEndpoint.TIMESERIES.getValue());

Expand Down
Loading

0 comments on commit 9753b29

Please sign in to comment.