Skip to content

Commit

Permalink
feat: switch from json-b to jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
josejulio authored and vkrizan committed Aug 2, 2023
1 parent 2a11c3f commit 8d1147c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
*/
package com.redhat.cloud.policies.app;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.cloud.policies.app.model.Msg;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Priority;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.validation.ValidationException;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.WebApplicationException;
Expand Down Expand Up @@ -55,8 +54,9 @@ public RuntimeException toThrowable(Response response) {
private Msg getBody(Response response) {
String msg = response.readEntity(String.class);
if (msg != null && !msg.isEmpty()) {
try (Jsonb jsonb = JsonbBuilder.create()) {
Map<String, String> errorMap = jsonb.fromJson(msg, HashMap.class);
ObjectMapper objectMapper = new ObjectMapper();
try {
Map<String, String> errorMap = objectMapper.readValue(msg, HashMap.class);
return new Msg(errorMap.get("errorMsg"));
} catch (Exception e) {
return new Msg("Parsing of response failed. Status code was " + response.getStatus());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
*/
package com.redhat.cloud.policies.app.auth;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Base64;
import java.util.Optional;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.json.bind.JsonbException;
import javax.ws.rs.core.HttpHeaders;

public abstract class HeaderHelper {

private static Jsonb jsonb = JsonbBuilder.create();
private static ObjectMapper objectMapper = new ObjectMapper();

public static Optional<XRhIdentity> getRhIdFromHeader(HttpHeaders httpHeaders) {
if (httpHeaders == null) {
Expand All @@ -42,8 +42,8 @@ public static Optional<XRhIdentity> getRhIdFromString(String xRhIdHeader) {
XRhIdentity rhIdentity;
try {
String json_string = new String(Base64.getDecoder().decode(xRhIdHeader));
rhIdentity = jsonb.fromJson(json_string, XRhIdentity.class);
} catch (JsonbException jbe) {
rhIdentity = objectMapper.readValue(json_string, XRhIdentity.class);
} catch (JsonProcessingException jpe) {
return Optional.empty();
}
return Optional.ofNullable(rhIdentity);
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/redhat/cloud/policies/app/auth/XRhIdentity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
*/
package com.redhat.cloud.policies.app.auth;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;
import javax.json.bind.annotation.JsonbProperty;

/**
* Data model of the representation of a x-rh-identity header.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class XRhIdentity {

public Map<String, Object> entitlements;
Expand All @@ -34,31 +37,33 @@ public String getUsername() {
return identity.user.username;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Identity {

@JsonbProperty("account_number")
@JsonProperty("account_number")
public String accountNumber;

@JsonbProperty("org_id")
@JsonProperty("org_id")
public String orgId;

public String type;
public User user;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class User {

public String email;
@JsonbProperty("first_name")
@JsonProperty("first_name")
public String firstName;
@JsonbProperty("last_name")
@JsonProperty("last_name")
public String lastName;
public String username;
@JsonbProperty("is_active")
@JsonProperty("is_active")
public boolean isActive;
@JsonbProperty("is_internal")
@JsonProperty("is_internal")
public boolean isInternal;
@JsonbProperty("is_org_admin")
@JsonProperty("is_org_admin")
public boolean isOrgAdmin;
}
}
16 changes: 9 additions & 7 deletions src/main/java/com/redhat/cloud/policies/app/model/Policy.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package com.redhat.cloud.policies.app.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.redhat.cloud.policies.app.model.pager.Page;
import com.redhat.cloud.policies.app.model.pager.Pager;
import com.redhat.cloud.policies.app.model.filter.Filter;
Expand All @@ -30,7 +33,6 @@
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.json.bind.annotation.JsonbTransient;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
Expand All @@ -44,17 +46,18 @@
import org.eclipse.microprofile.openapi.annotations.media.Schema;

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
public class Policy extends PanacheEntityBase {

// The ID will be created by code.
@Id
public
UUID id;

@JsonbTransient
@JsonIgnore
public String customerid;

@JsonbTransient
@JsonIgnore
@Column(name = "org_id")
public String orgId;

Expand Down Expand Up @@ -86,19 +89,21 @@ public class Policy extends PanacheEntityBase {
readOnly = true,
format = "yyyy-MM-dd hh:mm:ss.ddd",
implementation = String.class)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Timestamp mtime = new Timestamp(System.currentTimeMillis());

@Schema(type = SchemaType.STRING,
description = "Create time in a form like '2020-01-24 12:19:56.718', output only",
readOnly = true,
format = "yyyy-MM-dd hh:mm:ss.ddd",
implementation = String.class)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private Timestamp ctime = new Timestamp(System.currentTimeMillis());

@Column(name = "last_triggered", insertable = false, updatable = false)
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
private long lastTriggered;

@JsonbTransient
public void setMtime(String mtime) {
this.mtime = Timestamp.valueOf(mtime);
}
Expand All @@ -111,8 +116,6 @@ public String getMtime() {
return mtime.toString();
}


@JsonbTransient
public void setLastTriggered(long tTime) {
lastTriggered = tTime;
}
Expand All @@ -121,7 +124,6 @@ public long getLastTriggered() {
return lastTriggered;
}

@JsonbTransient
public void setCtime(String ctime) {
this.ctime = Timestamp.valueOf(ctime);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package com.redhat.cloud.policies.app.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.redhat.cloud.policies.app.lightweight.OrgIdLatestUpdateRepository;
import com.redhat.cloud.policies.app.lightweight.LightweightEngine;
import com.redhat.cloud.policies.app.auth.RhIdPrincipal;
Expand Down Expand Up @@ -46,7 +48,6 @@

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.json.JsonString;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.transaction.SystemException;
Expand Down Expand Up @@ -128,6 +129,9 @@ public class PolicyCrudService {
@Inject
PoliciesHistoryRepository policiesHistoryRepository;

@Inject
ObjectMapper objectMapper;

// workaround for returning generic types: https://github.com/swagger-api/swagger-core/issues/498#issuecomment-74510379
// This class is used only for swagger return type
private static class PagedResponseOfPolicy extends PagingUtils.PagedResponse<Policy> {
Expand Down Expand Up @@ -625,14 +629,21 @@ public Response validateCondition(@Valid @NotNull Policy policy) {
@APIResponse(responseCode = "500", description = "Internal error")
})
@Parameter(name = "id", description = "UUID of the policy")
public Response validateName(@NotNull JsonString policyName, @QueryParam("id") UUID id) {
public Response validateName(@NotNull String jsonPolicyName, @QueryParam("id") UUID id) {
if (!user.canReadPolicies()) {
return Response.status(Response.Status.FORBIDDEN).entity(new Msg(MISSING_PERMISSIONS_TO_VERIFY_POLICY)).build();
}

String policyName;
try {
policyName = objectMapper.readValue(jsonPolicyName, String.class);
} catch (JsonProcessingException jpe) {
return Response.status(400, "Invalid policy name received in body").build();
}

Policy policy = new Policy();
policy.id = id;
policy.name = policyName.getString();
policy.name = policyName;

Set<ConstraintViolation<Policy>> result = validator.validateProperty(policy, "name");

Expand Down
15 changes: 7 additions & 8 deletions src/test/java/com/redhat/cloud/policies/app/RestApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@
import java.util.UUID;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
import javax.validation.constraints.NotNull;

import io.restassured.response.Response;
Expand All @@ -59,6 +57,7 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper;

@QuarkusTest
@QuarkusTestResource(TestLifecycleManager.class)
Expand Down Expand Up @@ -1025,7 +1024,7 @@ void storeNewPolicyNoRbac() {
}

@Test
void storeAndUpdatePolicy() {
void storeAndUpdatePolicy() throws Exception {
TestPolicy tp = new TestPolicy();
tp.actions = "notification";
tp.conditions = "cores = 2";
Expand Down Expand Up @@ -1065,8 +1064,8 @@ void storeAndUpdatePolicy() {
.body()
.asString();

Jsonb jsonb = JsonbBuilder.create();
TestPolicy ret = jsonb.fromJson(resp, TestPolicy.class);
ObjectMapper objectMapper = new ObjectMapper();
TestPolicy ret = objectMapper.readValue(resp, TestPolicy.class);
assertEquals(tp.conditions, ret.conditions);

assertNotNull(ret.ctime);
Expand Down Expand Up @@ -1202,7 +1201,7 @@ void storeAndEnableDisablePolicy() {
// Check that update is protected by RBAC.
// we need to store as user with access first.
@Test
void storeAndUpdatePolicyNoUpdateAccess() {
void storeAndUpdatePolicyNoUpdateAccess() throws Exception {
TestPolicy tp = new TestPolicy();
tp.actions = "notification";
tp.conditions = "cores = 2";
Expand Down Expand Up @@ -1237,8 +1236,8 @@ void storeAndUpdatePolicyNoUpdateAccess() {
.body()
.asString();

Jsonb jsonb = JsonbBuilder.create();
TestPolicy ret = jsonb.fromJson(resp, TestPolicy.class);
ObjectMapper objectMapper = new ObjectMapper();
TestPolicy ret = objectMapper.readValue(resp, TestPolicy.class);
assertEquals(tp.conditions, ret.conditions);

try {
Expand Down

0 comments on commit 8d1147c

Please sign in to comment.