geyanggang commented on code in PR #14532:
URL: https://github.com/apache/gravitino/pull/14532#discussion_r4450930360


##########
authorizations/authorization-ranger/src/test/java/org/apache/gravitino/authorization/ranger/reference/TestRangerReferenceJson.java:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.gravitino.authorization.ranger.reference;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.util.Date;
+import java.util.TimeZone;
+import org.apache.ranger.authorization.utils.JsonUtils;
+import org.apache.ranger.plugin.util.JsonUtilsV2;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Pins the JSON exchanged with Ranger Admin for the user and group APIs. 
Since Ranger 2.8 the
+ * client serializes requests with {@link JsonUtils} (Jackson 2) and parses 
responses with {@link
+ * JsonUtilsV2} (Jackson 2), whereas Ranger 2.4 used Gson for requests and 
Jackson 1 for responses.
+ */
+public class TestRangerReferenceJson {
+
+  // The Gson configuration of RangerRESTClient in Ranger 2.4, used to 
serialize request bodies.
+  private static final Gson RANGER_24_REQUEST_GSON =
+      new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").create();
+
+  private static final ObjectMapper MAPPER = new ObjectMapper();
+
+  @Test
+  public void testUserRequestMatchesRanger24Format() throws Exception {
+    VXUser user = 
VXUser.builder().withName("user1").withDescription("desc").build();
+
+    JsonNode actual = MAPPER.readTree(JsonUtils.objectToJson(user));
+    JsonNode expected = MAPPER.readTree(RANGER_24_REQUEST_GSON.toJson(user));
+
+    Assertions.assertEquals(expected, actual);
+    Assertions.assertEquals("user1", actual.get("name").asText());
+    Assertions.assertEquals("rangerR0cks!", actual.get("password").asText());
+    Assertions.assertEquals(RangerCommonEnums.STATUS_ENABLED, 
actual.get("status").asInt());
+    Assertions.assertEquals(RangerCommonEnums.IS_VISIBLE, 
actual.get("isVisible").asInt());
+    Assertions.assertFalse(actual.has("id"));
+    Assertions.assertFalse(actual.has("createDate"));
+  }
+
+  @Test
+  public void testGroupRequestMatchesRanger24Format() throws Exception {
+    VXGroup group = 
VXGroup.builder().withName("group1").withDescription("desc").build();
+
+    JsonNode actual = MAPPER.readTree(JsonUtils.objectToJson(group));
+    JsonNode expected = MAPPER.readTree(RANGER_24_REQUEST_GSON.toJson(group));
+
+    Assertions.assertEquals(expected, actual);
+    Assertions.assertEquals("group1", actual.get("name").asText());
+    Assertions.assertEquals(RangerCommonEnums.GROUP_INTERNAL, 
actual.get("groupSource").asInt());
+    Assertions.assertFalse(actual.has("id"));
+  }
+
+  @Test
+  public void testDateUsesRangerAdminFormat() throws Exception {
+    VXUser user = VXUser.builder().withName("user1").build();
+    Date epoch = Date.from(Instant.EPOCH);
+    user.createDate = epoch;
+
+    JsonNode actual = MAPPER.readTree(JsonUtils.objectToJson(user));
+
+    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");

Review Comment:
   I found the JsonDateSerializer (copied from Ranger Admin) formats in the JVM 
default time zone, so the expected value intentionally uses it too; pinning 
only the test to UTC would make it fail outside UTC. It passes under 
Asia/Shanghai; I added a comment explaining this.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to