From 319abd794487e6e17eb2b08ca9ac266cbb16c66a Mon Sep 17 00:00:00 2001 From: Steve Marion Date: Tue, 12 Sep 2023 15:57:26 +0200 Subject: [PATCH] NO-JIRA Refactor graphql lib to externalize query so that each implementation can store it independently. Move existing query to their own modules. Add gitlab graphql config to the gitlab module. --- .../org/sonar/core/util/ResourceUtils.java | 55 +++++++++++++++++ .../sonar/core/util/ResourceUtilsTest.java | 60 +++++++++++++++++++ .../sonar/core/util/classpath_resource.txt | 1 + 3 files changed, 116 insertions(+) create mode 100644 sonar-core/src/main/java/org/sonar/core/util/ResourceUtils.java create mode 100644 sonar-core/src/test/java/org/sonar/core/util/ResourceUtilsTest.java create mode 100644 sonar-core/src/test/resources/org/sonar/core/util/classpath_resource.txt diff --git a/sonar-core/src/main/java/org/sonar/core/util/ResourceUtils.java b/sonar-core/src/main/java/org/sonar/core/util/ResourceUtils.java new file mode 100644 index 00000000000..cc9488ee0e2 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/util/ResourceUtils.java @@ -0,0 +1,55 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.core.util; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import org.apache.commons.io.IOUtils; + +public final class ResourceUtils { + private ResourceUtils() { + } + + /** + * Read a text resource from classpath. + * The resolution of the resource is equivalent to {@link Class#getResource(String)}. + * + * @param relativeTo class to base the resource path on + * @param resourcePath classpath name to the resource + * @return content of the resource + * @throws IllegalStateException if the resource cannot be read + */ + public static String readClasspathResource(Class relativeTo, String resourcePath) { + try { + InputStream resourceStream = relativeTo.getResourceAsStream(resourcePath); + if (resourceStream == null) { + throw new IllegalStateException(getErrorMessage(relativeTo, resourcePath)); + } + return IOUtils.toString(resourceStream, StandardCharsets.UTF_8); + } catch (IOException e) { + throw new IllegalStateException(getErrorMessage(relativeTo, resourcePath), e); + } + } + + private static String getErrorMessage(Class relativeTo, String resourcePath) { + return String.format("Fail to read classpath resource: %s of class: %s", resourcePath, relativeTo.getPackageName()); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/util/ResourceUtilsTest.java b/sonar-core/src/test/java/org/sonar/core/util/ResourceUtilsTest.java new file mode 100644 index 00000000000..8634e5b98f4 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/util/ResourceUtilsTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.core.util; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; +import org.apache.commons.io.IOUtils; +import org.junit.Test; +import org.mockito.MockedStatic; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mockStatic; + +public class ResourceUtilsTest { + + @Test + public void whenReadValidClasspathResource_thenReadIt() { + String result = ResourceUtils.readClasspathResource(ResourceUtilsTest.class, "classpath_resource.txt"); + assertThat(result) + .isEqualTo("OK\n"); + } + + @Test + public void whenReadInexistantClasspathResource_thenThrow() { + assertThatThrownBy( + () -> ResourceUtils.readClasspathResource(ResourceUtilsTest.class, "inexistant_resource.txt")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Fail to read classpath resource: inexistant_resource.txt of class: org.sonar.core.util"); + } + + @Test + public void whenReadClasspathResourceFails_thenThrow() { + try (MockedStatic scopedMock = mockStatic(IOUtils.class)) { + scopedMock.when(() -> IOUtils.toString(any(InputStream.class), any(Charset.class))).thenThrow(new IOException("error")); + assertThatThrownBy(() -> ResourceUtils.readClasspathResource(ResourceUtilsTest.class, "classpath_resource.txt")) + .isInstanceOf(IllegalStateException.class) + .hasMessage("Fail to read classpath resource: classpath_resource.txt of class: org.sonar.core.util"); + } + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/util/classpath_resource.txt b/sonar-core/src/test/resources/org/sonar/core/util/classpath_resource.txt new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/util/classpath_resource.txt @@ -0,0 +1 @@ +OK -- 2.39.5