From 233a760898a0f33891381e4a0e80553c91c17b45 Mon Sep 17 00:00:00 2001 From: Lukasz Jarocki Date: Thu, 14 Oct 2021 14:19:06 +0200 Subject: [PATCH] SONAR-15517 fixed an issue when sometimes audit logs could not be downloaded --- .../sonar/db/audit/model/UserNewValue.java | 2 +- .../db/audit/model/UserNewValueTest.java | 69 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/UserNewValueTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java index f78f046bd97..e2a42ffde27 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/audit/model/UserNewValue.java @@ -164,7 +164,7 @@ public class UserNewValue extends NewValue { addField(sb, "\"onboarded\": ", ObjectUtils.toString(this.onboarded), false); addField(sb, "\"root\": ", ObjectUtils.toString(this.root), false); addField(sb, "\"lastConnectionDate\": ", this.lastConnectionDate == null ? - "" : DateUtils.formatDateTime(this.lastConnectionDate), false); + "" : DateUtils.formatDateTime(this.lastConnectionDate), true); endString(sb); return sb.toString(); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/UserNewValueTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/UserNewValueTest.java new file mode 100644 index 00000000000..6c89310123b --- /dev/null +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/audit/model/UserNewValueTest.java @@ -0,0 +1,69 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db.audit.model; + +import org.json.simple.parser.JSONParser; +import org.junit.Test; +import org.sonar.db.user.UserDto; + +import static org.junit.Assert.fail; + +public class UserNewValueTest { + + private static final JSONParser jsonParser = new JSONParser(); + + @Test + public void toString_givenAllFieldsWithValue_returnValidJSON() { + UserDto userDto = new UserDto(); + userDto.setName("name"); + userDto.setEmail("name@email.com"); + userDto.setActive(true); + userDto.setScmAccounts("github-account"); + userDto.setExternalId("name"); + userDto.setExternalLogin("name"); + userDto.setExternalIdentityProvider("github"); + userDto.setLocal(false); + userDto.setOnboarded(true); + userDto.setLastConnectionDate(System.currentTimeMillis()); + UserNewValue userNewValue = new UserNewValue(userDto); + + String jsonString = userNewValue.toString(); + + assertValidJSON(jsonString); + } + + @Test + public void toString_givenUserUuidAndUserLogin_returnValidJSON() { + UserNewValue userNewValue = new UserNewValue("userUuid", "userLogin"); + + String jsonString = userNewValue.toString(); + + assertValidJSON(jsonString); + } + + private void assertValidJSON(String jsonString) { + try { + jsonParser.parse(jsonString); + } catch (Exception e) { + //if the json is invalid the test will fail + fail("UserNewValue.toString() generated invalid JSON. More details: " + e.getMessage()); + } + } +} -- 2.39.5