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

Commit ada6529

Browse files
authored
A bit of code clean up (#46)
* A bit of code clean up * A bit of code clean up * A bit of code clean up
1 parent 41a4417 commit ada6529

File tree

12 files changed

+47
-76
lines changed

12 files changed

+47
-76
lines changed

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/ConstraintAndGroupDescriptionResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.slf4j.LoggerFactory.getLogger;
2424

2525
import java.util.ArrayList;
26+
import java.util.Collections;
2627
import java.util.List;
2728
import java.util.MissingResourceException;
2829

@@ -72,9 +73,7 @@ public List<Class> getGroups(Constraint constraint) {
7273
Class[] groups = (Class[]) rawGroups;
7374

7475
List<Class> result = new ArrayList<>();
75-
for (Class group : groups) {
76-
result.add(group);
77-
}
76+
Collections.addAll(result, groups);
7877
return result;
7978
}
8079

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/constraints/HumanReadableConstraintResolver.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static org.springframework.util.StringUtils.arrayToDelimitedString;
2121

2222
import java.util.ArrayList;
23+
import java.util.Collections;
2324
import java.util.HashMap;
2425
import java.util.HashSet;
2526
import java.util.List;
@@ -42,9 +43,7 @@ class HumanReadableConstraintResolver implements ConstraintResolver {
4243
public HumanReadableConstraintResolver(ConstraintResolver delegate) {
4344
this.delegate = delegate;
4445
this.ignoredFields = new HashSet<>();
45-
for (String field : IGNORED_FIELDS) {
46-
this.ignoredFields.add(field);
47-
}
46+
Collections.addAll(this.ignoredFields, IGNORED_FIELDS);
4847
}
4948

5049
@Override

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/jackson/FieldDocumentationObjectVisitor.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616

1717
package capital.scalable.restdocs.jackson;
1818

19-
import static org.apache.commons.collections.IteratorUtils.toList;
20-
21-
import java.lang.annotation.Annotation;
22-
import java.util.List;
23-
2419
import com.fasterxml.jackson.databind.BeanProperty;
2520
import com.fasterxml.jackson.databind.JavaType;
2621
import com.fasterxml.jackson.databind.JsonMappingException;
@@ -60,23 +55,17 @@ public void optionalProperty(BeanProperty prop) throws JsonMappingException {
6055

6156
String fieldPath = path + (path.isEmpty() ? "" : ".") + jsonName;
6257
Class<?> javaBaseClass = prop.getMember().getDeclaringClass();
63-
List<Annotation> annotations = getAnnotations(prop);
6458
boolean shouldExpand = shouldExpand(prop);
6559

6660
InternalFieldInfo fieldInfo =
67-
new InternalFieldInfo(javaBaseClass, fieldName, fieldPath, annotations,
68-
shouldExpand);
61+
new InternalFieldInfo(javaBaseClass, fieldName, fieldPath, shouldExpand);
6962

7063
JsonFormatVisitorWrapper visitor =
7164
new FieldDocumentationVisitorWrapper(getProvider(), context, fieldPath, fieldInfo);
7265

7366
ser.acceptJsonFormatVisitor(visitor, type);
7467
}
7568

76-
private List<Annotation> getAnnotations(BeanProperty prop) {
77-
return toList(prop.getMember().annotations().iterator());
78-
}
79-
8069
protected JsonSerializer<?> getSer(BeanProperty prop) throws JsonMappingException {
8170
JsonSerializer<Object> ser = null;
8271
if (prop instanceof BeanPropertyWriter) {

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/jackson/InternalFieldInfo.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,17 @@
1616

1717
package capital.scalable.restdocs.jackson;
1818

19-
import java.lang.annotation.Annotation;
20-
import java.util.List;
21-
2219
class InternalFieldInfo {
2320
private final Class<?> javaBaseClass;
2421
private final String javaFieldName;
2522
private final String jsonFieldPath;
26-
private List<Annotation> annotations;
2723
private final boolean shouldExpand;
2824

2925
public InternalFieldInfo(Class<?> javaBaseClass, String javaFieldName,
30-
String jsonFieldPath, List<Annotation> annotations, boolean shouldExpand) {
26+
String jsonFieldPath, boolean shouldExpand) {
3127
this.javaBaseClass = javaBaseClass;
3228
this.javaFieldName = javaFieldName;
3329
this.jsonFieldPath = jsonFieldPath;
34-
this.annotations = annotations;
3530
this.shouldExpand = shouldExpand;
3631
}
3732

@@ -47,10 +42,6 @@ public String getJsonFieldPath() {
4742
return jsonFieldPath;
4843
}
4944

50-
public List<Annotation> getAnnotations() {
51-
return annotations;
52-
}
53-
5445
public boolean shouldExpand() {
5546
return shouldExpand;
5647
}

spring-auto-restdocs-core/src/main/java/capital/scalable/restdocs/response/JsonContentModifier.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static org.springframework.http.MediaType.APPLICATION_JSON;
2020

2121
import java.io.IOException;
22-
import java.util.Iterator;
2322

2423
import com.fasterxml.jackson.databind.JsonNode;
2524
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -48,7 +47,7 @@ public byte[] modifyContent(byte[] originalContent, MediaType contentType) {
4847
* Uses UTF-8. According to {@link com.fasterxml.jackson.core.JsonEncoding}
4948
* only UTF-{8,16,32} are a valid JSON encodings.
5049
*
51-
* @param originalContent
50+
* @param originalContent original JSON content
5251
* @return modified JSON content
5352
*/
5453
protected byte[] modifyContent(byte[] originalContent) {
@@ -68,8 +67,8 @@ protected void walk(JsonNode node, JsonNodeFilter filter,
6867
if (filter.apply(node)) {
6968
consumer.accept(node);
7069
}
71-
for (Iterator<JsonNode> children = node.iterator(); children.hasNext();) {
72-
walk(children.next(), filter, consumer);
70+
for (JsonNode child : node) {
71+
walk(child, filter, consumer);
7372
}
7473
}
7574
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/constraints/ConstraintAndGroupDescriptionResolverTest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
import static org.mockito.Mockito.mock;
2727
import static org.mockito.Mockito.when;
2828

29+
import java.util.Collections;
30+
import java.util.HashMap;
2931
import java.util.List;
3032
import java.util.Map;
3133
import java.util.MissingResourceException;
3234

33-
import org.apache.commons.collections.map.HashedMap;
3435
import org.junit.Before;
3536
import org.junit.Test;
3637
import org.mockito.ArgumentCaptor;
@@ -52,7 +53,7 @@ public void setup() {
5253
@Test
5354
public void noGroupDescriptionIsResolved() {
5455
// given
55-
Map<String, Object> configuration = new HashedMap();
56+
Map<String, Object> configuration = new HashMap<>();
5657
configuration.put(GROUPS, new Class<?>[]{});
5758
Constraint constraint = new Constraint("Constraint", configuration);
5859
when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
@@ -65,7 +66,7 @@ public void noGroupDescriptionIsResolved() {
6566
@Test
6667
public void noDescriptionIsNotResolved() {
6768
// given
68-
Map<String, Object> configuration = new HashedMap();
69+
Map<String, Object> configuration = new HashMap<>();
6970
configuration.put(GROUPS, new Class<?>[]{});
7071
Constraint constraint = new Constraint("Constraint", configuration);
7172
when(delegate.resolveDescription(eq(constraint)))
@@ -79,7 +80,7 @@ public void noDescriptionIsNotResolved() {
7980
@Test
8081
public void noDescriptionWithGroupsIsResolved() {
8182
// given
82-
Map<String, Object> configuration = new HashedMap();
83+
Map<String, Object> configuration = new HashMap<>();
8384
configuration.put(GROUPS, new Class<?>[]{Update.class});
8485
Constraint constraint = new Constraint("Constraint", configuration);
8586
when(delegate.resolveDescription(eq(constraint)))
@@ -93,7 +94,7 @@ public void noDescriptionWithGroupsIsResolved() {
9394
@Test
9495
public void singleGroupDescriptionIsResolved() {
9596
// given
96-
Map<String, Object> configuration = new HashedMap();
97+
Map<String, Object> configuration = new HashMap<>();
9798
configuration.put(GROUPS, new Class<?>[]{Update.class});
9899
Constraint constraint = new Constraint("Constraint", configuration);
99100
when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
@@ -107,7 +108,7 @@ public void singleGroupDescriptionIsResolved() {
107108
@Test
108109
public void multipleGroupDescriptionsAreResolved() {
109110
// given
110-
Map<String, Object> configuration = new HashedMap();
111+
Map<String, Object> configuration = new HashMap<>();
111112
configuration.put(GROUPS, new Class<?>[]{Update.class, Create.class});
112113
Constraint constraint = new Constraint("Constraint", configuration);
113114
when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
@@ -122,7 +123,7 @@ public void multipleGroupDescriptionsAreResolved() {
122123
@Test
123124
public void groupDescriptionIsNotResolved() {
124125
// given
125-
Map<String, Object> configuration = new HashedMap();
126+
Map<String, Object> configuration = new HashMap<>();
126127
configuration.put(GROUPS, new Class<?>[]{Update.class});
127128
Constraint constraint = new Constraint("Constraint", configuration);
128129
when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
@@ -137,7 +138,7 @@ public void groupDescriptionIsNotResolved() {
137138
@Test
138139
public void strangeGroupConfigurationIsResolved() {
139140
// given
140-
Map<String, Object> configuration = new HashedMap();
141+
Map<String, Object> configuration = new HashMap<>();
141142
configuration.put(GROUPS, "no group");
142143
Constraint constraint = new Constraint("Constraint", configuration);
143144
when(delegate.resolveDescription(eq(constraint))).thenReturn("Must be it");
@@ -151,7 +152,7 @@ public void strangeGroupConfigurationIsResolved() {
151152
@Test
152153
public void noConstraintDescriptionIsResolved() {
153154
// given
154-
Map<String, Object> configuration = new HashedMap();
155+
Map<String, Object> configuration = new HashMap<>();
155156
configuration.put(GROUPS, new Class<?>[]{Update.class});
156157
Constraint constraint = new Constraint("Constraint", configuration);
157158
when(delegate.resolveDescription(eq(constraint))).thenReturn("");
@@ -190,7 +191,8 @@ public void groupDescriptionNotResolved() {
190191
@Test
191192
public void noGroupsResolved() {
192193
// given
193-
Constraint constraint = new Constraint("Constraint", new HashedMap());
194+
Constraint constraint =
195+
new Constraint("Constraint", Collections.<String, Object>emptyMap());
194196
// when
195197
List<Class> groups = resolver.getGroups(constraint);
196198
// then
@@ -200,14 +202,14 @@ public void noGroupsResolved() {
200202
@Test
201203
public void groupsResolved() {
202204
// given
203-
Map<String, Object> configuration = new HashedMap();
205+
Map<String, Object> configuration = new HashMap<>();
204206
configuration.put(GROUPS, new Class<?>[]{Update.class, Create.class});
205207
Constraint constraint = new Constraint("Constraint", configuration);
206208
// when
207209
List<Class> groups = resolver.getGroups(constraint);
208210
// then
209211
assertThat(groups.size(), is(2));
210-
assertThat((Class) groups.get(0), equalTo((Class) Update.class));
211-
assertThat((Class) groups.get(1), equalTo((Class) Create.class));
212+
assertThat(groups.get(0), equalTo((Class) Update.class));
213+
assertThat(groups.get(1), equalTo((Class) Create.class));
212214
}
213215
}

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/constraints/HumanReadableConstraintResolverTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void testHumanReadable() {
4848
// setup
4949
Map<String, Object> configuration = new HashedMap();
5050
configuration.put("primitive", 1);
51-
configuration.put("wrapper", Integer.valueOf(1));
51+
configuration.put("wrapper", 1);
5252
configuration.put("object", new CustomObj("Peter"));
5353
configuration.put("array", new Object[]{"value1", "value2"});
5454
configuration.put("class", CustomConstraint.class);

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/constraints/SkippableConstraintResolverTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,11 @@ public class SkippableConstraintResolverTest {
4141

4242
private SkippableConstraintResolver resolver;
4343
private ConstraintResolver delegate;
44-
private GroupDescriptionResolver descriptionResolver;
4544

4645
@Before
4746
public void setup() {
4847
delegate = mock(ConstraintResolver.class);
49-
descriptionResolver = mock(GroupDescriptionResolver.class);
48+
GroupDescriptionResolver descriptionResolver = mock(GroupDescriptionResolver.class);
5049
resolver = new SkippableConstraintResolver(delegate, descriptionResolver);
5150
}
5251

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/jackson/ExtendedFieldDescriptor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ public boolean equals(Object o) {
6464
return false;
6565
if (optionals != null ? !optionals.equals(that.optionals) : that.optionals != null)
6666
return false;
67-
if (constraints != null ? !constraints.equals(that.constraints) : that.constraints != null)
68-
return false;
67+
return constraints != null ? constraints.equals(that.constraints) :
68+
that.constraints == null;
6969

70-
return true;
7170
}
7271

7372
@Override

spring-auto-restdocs-core/src/test/java/capital/scalable/restdocs/jackson/FieldDocumentationGeneratorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ public void testGenerateDocumentationForConstraints() throws Exception {
356356
.thenReturn(singletonList("false"));
357357
when(constraintReader.getConstraintMessages(ConstraintField.class, "value"))
358358
.thenReturn(asList(
359-
new String[]{"A constraint1 for value", "A constraint2 for value"}));
359+
"A constraint1 for value", "A constraint2 for value"));
360360
when(constraintReader.getOptionalMessages(ConstraintField.class, "value"))
361361
.thenReturn(singletonList("false"));
362362

0 commit comments

Comments
 (0)