diff options
author | Steve Marion <steve.marion@sonarsource.com> | 2023-09-12 15:57:26 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-09-27 20:02:58 +0000 |
commit | 319abd794487e6e17eb2b08ca9ac266cbb16c66a (patch) | |
tree | efa1806effd241fd7cb10ea75463db13d5cb5040 /sonar-core/src | |
parent | b53eb410076e916f554f0ffe71ff0d350cde5362 (diff) | |
download | sonarqube-319abd794487e6e17eb2b08ca9ac266cbb16c66a.tar.gz sonarqube-319abd794487e6e17eb2b08ca9ac266cbb16c66a.zip |
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.
Diffstat (limited to 'sonar-core/src')
3 files changed, 116 insertions, 0 deletions
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<IOUtils> 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 |