Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit 1adce3d

Browse files
committed
Fix sonar issues
1 parent b8749bc commit 1adce3d

File tree

11 files changed

+61
-47
lines changed

11 files changed

+61
-47
lines changed

altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropertyGroupReader.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
class PropertyGroupReader {
1616

17-
private Environment environment;
18-
private String prefix;
17+
private final Environment environment;
18+
private final String prefix;
1919
private Properties props;
2020

2121
PropertyGroupReader(Environment environment, String prefix) {
@@ -38,14 +38,15 @@ private void loadProps() {
3838
.forEach(key -> add(propertySource, key)));
3939
}
4040

41-
private Stream<EnumerablePropertySource> streamOfPropertySources() {
41+
@SuppressWarnings("unchecked")
42+
private Stream<EnumerablePropertySource<Object>> streamOfPropertySources() {
4243
if (environment instanceof ConfigurableEnvironment) {
4344
Iterator<PropertySource<?>> iterator = ((ConfigurableEnvironment) environment)
4445
.getPropertySources().iterator();
4546
Iterable<PropertySource<?>> iterable = () -> iterator;
4647
return StreamSupport.stream(iterable.spliterator(), false)
4748
.filter(EnumerablePropertySource.class::isInstance)
48-
.map(EnumerablePropertySource.class::cast);
49+
.map(it -> (EnumerablePropertySource<Object>) it);
4950
}
5051
return Stream.empty();
5152
}
@@ -58,10 +59,9 @@ private boolean isWanted(String key) {
5859
return key.startsWith(prefix);
5960
}
6061

61-
private Object add(EnumerablePropertySource propertySource, String key) {
62-
return props.put(withoutPrefix(key), propertySource.getProperty(key));
62+
private void add(EnumerablePropertySource<Object> propertySource, String key) {
63+
props.put(withoutPrefix(key), propertySource.getProperty(key));
6364
}
6465

65-
6666
}
6767

altair-spring-boot-autoconfigure/src/main/java/graphql/kickstart/altair/boot/PropsLoader.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.nio.charset.StandardCharsets;
77
import java.util.Optional;
88
import java.util.Properties;
9+
import lombok.SneakyThrows;
910
import org.springframework.core.env.Environment;
1011
import org.springframework.core.io.ClassPathResource;
1112
import org.springframework.core.io.Resource;
@@ -17,7 +18,7 @@ class PropsLoader {
1718
private static final String ALTAIR_PROPS_RESOURCES_PREFIX = ALTAIR_PROPS_PREFIX + "resources.";
1819
private static final String ALTAIR_PROPS_VALUES_PREFIX = ALTAIR_PROPS_PREFIX + "values.";
1920

20-
private Environment environment;
21+
private final Environment environment;
2122

2223
PropsLoader(Environment environment) {
2324
this.environment = environment;
@@ -34,17 +35,15 @@ String load() throws IOException {
3435
return objectMapper.writeValueAsString(props);
3536
}
3637

37-
private Optional<String> loadPropFromResource(String prop) throws IOException {
38+
private Optional<String> loadPropFromResource(String prop) {
3839
String property = ALTAIR_PROPS_RESOURCES_PREFIX + prop;
39-
if (environment.containsProperty(property)) {
40-
String location = environment.getProperty(property);
41-
Resource resource = new ClassPathResource(location);
42-
return Optional.of(loadResource(resource));
43-
}
44-
return Optional.empty();
40+
return Optional.ofNullable(environment.getProperty(property))
41+
.map(ClassPathResource::new)
42+
.map(this::loadResource);
4543
}
4644

47-
private String loadResource(Resource resource) throws IOException {
45+
@SneakyThrows
46+
private String loadResource(Resource resource) {
4847
try (InputStream inputStream = resource.getInputStream()) {
4948
return StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8);
5049
}

altair-spring-boot-autoconfigure/src/test/java/graphql/kickstart/altair/boot/test/AbstractAutoConfigurationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.kickstart.altair.boot.test;
22

3+
import lombok.NonNull;
34
import org.junit.jupiter.api.AfterEach;
45
import org.springframework.boot.test.util.TestPropertyValues;
56
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

altair-spring-boot-autoconfigure/src/test/java/graphql/kickstart/altair/boot/test/AltairControllerTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@
1010
import org.springframework.context.annotation.Bean;
1111
import org.springframework.context.annotation.Configuration;
1212
import org.springframework.context.annotation.PropertySource;
13+
import org.springframework.context.support.AbstractApplicationContext;
1314
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
1415
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
1516

1617
/**
1718
* @author Andrew Potter
1819
*/
19-
public class AltairControllerTest extends AbstractAutoConfigurationTest {
20+
class AltairControllerTest extends AbstractAutoConfigurationTest {
2021

2122
public AltairControllerTest() {
2223
super(AnnotationConfigWebApplicationContext.class, AltairAutoConfiguration.class);
2324
}
2425

2526
@Test
26-
public void altairLoads() {
27+
void altairLoads() {
2728
load(EnabledConfiguration.class);
2829

2930
assertThat(this.getContext().getBean(AltairController.class)).isNotNull();
3031
}
3132

3233
@Test
33-
public void altairDoesNotLoad() {
34+
void altairDoesNotLoad() {
3435
load(DisabledConfiguration.class);
3536

37+
AbstractApplicationContext context = getContext();
3638
assertThatExceptionOfType(NoSuchBeanDefinitionException.class)
37-
.isThrownBy(() -> this.getContext().getBean(AltairController.class));
39+
.isThrownBy(() -> context.getBean(AltairController.class));
3840
}
3941

4042
@Configuration

example-graphql-subscription/src/main/java/graphql/kickstart/spring/web/boot/resolvers/StockPriceUpdate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ public BigDecimal getStockPrice() {
3232
}
3333

3434
public BigDecimal getStockPriceChange() {
35-
return null; //stockPriceChange;
35+
return stockPriceChange;
3636
}
3737
}

example-graphql-tools/src/main/java/com/graphql/sample/boot/Comment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.graphql.sample.boot;
22

3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
6+
@Data
7+
@AllArgsConstructor
38
class Comment {
49

510
private Long id;
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
11
package com.graphql.sample.boot;
22

3+
import static java.util.Collections.singletonList;
4+
35
import graphql.kickstart.tools.GraphQLResolver;
46
import java.util.Collections;
7+
import java.util.HashMap;
58
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Optional;
611
import org.springframework.stereotype.Component;
712

813
@Component
914
class PostResolver implements GraphQLResolver<Post> {
1015

16+
private final Map<Long, List<Comment>> comments = new HashMap<>();
17+
18+
PostResolver() {
19+
comments.put(1L, singletonList(new Comment(1L, "Some comment")));
20+
}
21+
1122
public List<Comment> getComments(Post post) {
12-
return Collections.emptyList();
23+
return Optional.ofNullable(comments.get(post.getId())).orElseGet(Collections::emptyList);
1324
}
1425

1526
}

example-graphql-tools/src/test/java/com/graphql/sample/boot/GraphQLToolsSampleApplicationTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@
1111
import java.io.IOException;
1212
import java.util.ArrayList;
1313
import java.util.List;
14-
import org.junit.jupiter.api.Disabled;
1514
import org.junit.jupiter.api.Test;
1615
import org.junit.jupiter.api.extension.ExtendWith;
1716
import org.springframework.beans.factory.annotation.Autowired;
1817
import org.springframework.test.context.junit.jupiter.SpringExtension;
1918

2019
@ExtendWith(SpringExtension.class)
2120
@GraphQLTest
22-
public class GraphQLToolsSampleApplicationTest {
21+
class GraphQLToolsSampleApplicationTest {
2322

2423
@Autowired
2524
private GraphQLTestTemplate graphQLTestTemplate;
2625

2726
@Test
28-
@Disabled
29-
public void get_comments() throws IOException {
27+
void get_comments() throws IOException {
3028
GraphQLResponse response = graphQLTestTemplate
3129
.postForResource("graphql/post-get-comments.graphql");
3230
assertNotNull(response);
@@ -35,7 +33,7 @@ public void get_comments() throws IOException {
3533
}
3634

3735
@Test
38-
public void get_comments_withFragments() throws IOException {
36+
void get_comments_withFragments() throws IOException {
3937
List<String> fragments = new ArrayList<>();
4038
fragments.add("graphql/all-comment-fields-fragment.graphql");
4139
GraphQLResponse response = graphQLTestTemplate
@@ -46,7 +44,7 @@ public void get_comments_withFragments() throws IOException {
4644
}
4745

4846
@Test
49-
public void create_post() throws IOException {
47+
void create_post() throws IOException {
5048
ObjectNode variables = new ObjectMapper().createObjectNode();
5149
variables.put("text", "lorem ipsum dolor sit amet");
5250
GraphQLResponse response = graphQLTestTemplate

example-request-scoped-dataloader/src/main/java/graphql/servlet/examples/dataloader/requestscope/CustomerRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ private CustomerRepository() {
2121
data.put(106, "Customer Name 6");
2222
}
2323

24-
public List getUserNamesForIds(List<Integer> customerIds) {
24+
public List<String> getUserNamesForIds(List<Integer> customerIds) {
2525
return customerIds.parallelStream().map(data::get).collect(Collectors.toList());
2626
}
2727

example-spring-common/src/main/java/graphql/kickstart/spring/web/boot/sample/SimpleListConnection.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import graphql.schema.DataFetchingEnvironment;
99
import java.nio.charset.StandardCharsets;
1010
import java.util.ArrayList;
11+
import java.util.Base64;
1112
import java.util.List;
1213

1314
/**
@@ -16,18 +17,18 @@
1617
public class SimpleListConnection {
1718

1819
private static final String DUMMY_CURSOR_PREFIX = "simple-cursor";
19-
private List<?> data = new ArrayList<Object>();
20+
private final List<?> data;
2021

2122
public SimpleListConnection(List<?> data) {
2223
this.data = data;
2324
}
2425

25-
public <T extends EdgeObjectType> T createEdgeObject() {
26-
return (T) new EdgeObjectType();
26+
public <E extends EdgeObjectType> E createEdgeObject() {
27+
return (E) new EdgeObjectType();
2728
}
2829

29-
public <T extends ConnectionObjectType> T createConnectionObject() {
30-
return (T) new ConnectionObjectType();
30+
public <E extends ConnectionObjectType> E createConnectionObject() {
31+
return (E) new ConnectionObjectType();
3132
}
3233

3334
private List<EdgeObjectType> buildEdges() {
@@ -42,7 +43,7 @@ private List<EdgeObjectType> buildEdges() {
4243
return edges;
4344
}
4445

45-
public <T extends ConnectionObjectType> T get(DataFetchingEnvironment environment) {
46+
public <C extends ConnectionObjectType> C get(DataFetchingEnvironment environment) {
4647

4748
List<EdgeObjectType> edges = buildEdges();
4849

@@ -52,7 +53,7 @@ public <T extends ConnectionObjectType> T get(DataFetchingEnvironment environmen
5253
int end = Math.min(beforeOffset, edges.size());
5354

5455
edges = edges.subList(begin, end);
55-
if (edges.size() == 0) {
56+
if (edges.isEmpty()) {
5657
return emptyConnection();
5758
}
5859

@@ -69,7 +70,7 @@ public <T extends ConnectionObjectType> T get(DataFetchingEnvironment environmen
6970
edges = edges.subList(edges.size() - last, edges.size());
7071
}
7172

72-
if (edges.size() == 0) {
73+
if (edges.isEmpty()) {
7374
return emptyConnection();
7475
}
7576

@@ -86,13 +87,13 @@ public <T extends ConnectionObjectType> T get(DataFetchingEnvironment environmen
8687
connection.setEdges(edges);
8788
connection.setPageInfo(pageInfo);
8889

89-
return (T) connection;
90+
return (C) connection;
9091
}
9192

92-
private <T extends ConnectionObjectType> T emptyConnection() {
93+
private <E extends ConnectionObjectType> E emptyConnection() {
9394
ConnectionObjectType connection = createConnectionObject();
9495
connection.setPageInfo(new PageInfoObjectType());
95-
return (T) connection;
96+
return (E) connection;
9697
}
9798

9899
public ConnectionCursor cursorForObjectInConnection(Object object) {
@@ -111,9 +112,8 @@ private int getOffsetFromCursor(String cursor, int defaultValue) {
111112
}
112113

113114
private String createCursor(int offset) {
114-
byte[] lala = (DUMMY_CURSOR_PREFIX + Integer.toString(offset)).getBytes(StandardCharsets.UTF_8);
115-
String string = java.util.Base64.getEncoder().encodeToString(lala);
116-
return string;
115+
byte[] lala = (DUMMY_CURSOR_PREFIX + offset).getBytes(StandardCharsets.UTF_8);
116+
return Base64.getEncoder().encodeToString(lala);
117117
}
118118

119119
}

0 commit comments

Comments
 (0)