Skip to content

Commit 0c8b143

Browse files
committed
eliminate code warnings, make use of java 17 syntax
1 parent f57934d commit 0c8b143

File tree

7 files changed

+72
-44
lines changed

7 files changed

+72
-44
lines changed

conga-ansible-plugin/src/main/java/io/wcm/devops/conga/plugins/ansible/valueprovider/AnsibleInventoryValueProviderPlugin.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.HashMap;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.stream.Collectors;
2928

3029
import org.apache.commons.lang3.StringUtils;
3130
import org.apache.commons.lang3.Strings;
@@ -129,9 +128,9 @@ private InventoryContent getInventoryContent(ValueProviderContext context) {
129128
private InventoryContent tryReadJsonStyle(String inventoryContent, File file, ValueProviderContext context) {
130129
try {
131130
JsonElement root = JsonParser.parseString(inventoryContent);
132-
if (root instanceof JsonObject) {
131+
if (root instanceof JsonObject jsonObject) {
133132
ReadContext jsonpathReadContext = JsonPath.parse(inventoryContent);
134-
return new InventoryContent(jsonToConfig((JsonObject)root), jsonpathReadContext);
133+
return new InventoryContent(jsonToConfig(jsonObject), jsonpathReadContext);
135134
}
136135
}
137136
catch (JsonSyntaxException ex) {
@@ -147,14 +146,13 @@ private Map<String, List<String>> jsonToConfig(JsonObject root) {
147146
for (Map.Entry<String, JsonElement> entry : root.entrySet()) {
148147
if (!Strings.CS.equals(entry.getKey(), "_meta")) {
149148
JsonArray hostNamesArray = null;
150-
if (entry.getValue() instanceof JsonArray) {
151-
hostNamesArray = (JsonArray)entry.getValue();
149+
if (entry.getValue() instanceof JsonArray entryArray) {
150+
hostNamesArray = entryArray;
152151
}
153-
else if (entry.getValue() instanceof JsonObject) {
154-
JsonObject entryObject = (JsonObject)entry.getValue();
152+
else if (entry.getValue() instanceof JsonObject entryObject) {
155153
JsonElement hostsElement = entryObject.get("hosts");
156-
if (hostsElement instanceof JsonArray) {
157-
hostNamesArray = (JsonArray)hostsElement;
154+
if (hostsElement instanceof JsonArray hostsArray) {
155+
hostNamesArray = hostsArray;
158156
}
159157
}
160158

@@ -185,7 +183,7 @@ private Map<String, List<String>> inventoryToConfig(AnsibleInventory inventory)
185183
for (AnsibleGroup group : inventory.getGroups()) {
186184
config.put(group.getName(), group.getHosts().stream()
187185
.map(AnsibleHost::getName)
188-
.collect(Collectors.toList()));
186+
.toList());
189187
}
190188
return config;
191189
}

conga-ansible-plugin/src/test/java/io/wcm/devops/conga/plugins/ansible/urlfile/AnsibleVaultUrlFilePluginTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void testGetFile() throws Exception {
6666
}
6767

6868
@Test
69-
void testInvalidFile() throws Exception {
69+
void testInvalidFile() {
7070
assertThrows(IOException.class, () -> {
7171
urlFileManager.getFile("ansible-vault:classpath:/vault-sample/nonexisting-file");
7272
});

conga-ansible-plugin/src/test/java/io/wcm/devops/conga/plugins/ansible/util/FileScriptLoaderTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@
2929

3030
class FileScriptLoaderTest {
3131

32-
private static final String EXPECTED_CONTENT = "[test-group]\n"
33-
+ "host-01 conga_node=aem-author\n"
34-
+ "host-02 conga_node=aem-publish\n"
35-
+ "host-03 conga_node=aem-publish\n"
36-
+ "\n"
37-
+ "[aem-author]\n"
38-
+ "host-01\n"
39-
+ "\n"
40-
+ "[aem-publish]\n"
41-
+ "host-02\n"
42-
+ "host-03";
32+
private static final String EXPECTED_CONTENT = """
33+
[test-group]
34+
host-01 conga_node=aem-author
35+
host-02 conga_node=aem-publish
36+
host-03 conga_node=aem-publish
37+
38+
[aem-author]
39+
host-01
40+
41+
[aem-publish]
42+
host-02
43+
host-03""";
4344

4445
@Test
4546
void testFile() throws Exception {

conga-ansible-plugin/src/test/java/it/andreascarpino/ansible/inventory/AnsibleInventoryReaderTest.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,17 @@ void testReadSimple() {
6262

6363
for (AnsibleHost h : group.getHosts()) {
6464
switch (h.getName()) {
65-
case "host1":
65+
case "host1":
6666
assertEquals(3, h.getVariables().size());
67-
break;
68-
case "host2":
67+
break;
68+
case "host2":
6969
assertEquals(0, h.getVariables().size());
70-
break;
71-
case "host3":
70+
break;
71+
case "host3":
7272
assertEquals(1, h.getVariables().size());
73-
break;
73+
break;
74+
default:
75+
// ignore
7476
}
7577
}
7678

@@ -146,10 +148,31 @@ void testReadGroupVars() {
146148

147149
@Test
148150
void testReadAnsibleExample() {
149-
final String inventoryText = "[atlanta]\nhost1\nhost2\n\n[raleigh]\nhost2\nhost3\n\n[southeast:children]\n"
150-
+ "atlanta\nraleigh\n\n[southeast:vars]\nsome_server=foo.southeast.example.com\nhalon_system_timeout=30"
151-
+ "\nself_destruct_countdown=60\nescape_pods=2\n\n[usa:children]\nsoutheast\nnortheast\nsouthwest\n"
152-
+ "northwest\n";
151+
final String inventoryText = """
152+
[atlanta]
153+
host1
154+
host2
155+
156+
[raleigh]
157+
host2
158+
host3
159+
160+
[southeast:children]
161+
atlanta
162+
raleigh
163+
164+
[southeast:vars]
165+
some_server=foo.southeast.example.com
166+
halon_system_timeout=30
167+
nself_destruct_countdown=60
168+
escape_pods=2
169+
170+
[usa:children]
171+
southeast
172+
northeast
173+
southwest
174+
northwest
175+
""";
153176

154177
AnsibleInventory inventory = AnsibleInventoryReader.read(inventoryText);
155178

conga-ansible-plugin/src/test/java/it/andreascarpino/ansible/inventory/AnsibleInventoryWriterTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,16 @@ void testWriteHostAndGroups() {
6363

6464
String inventoryText = AnsibleInventoryWriter.write(inventory);
6565

66-
assertEquals(
67-
"mail.example.com\n[webservers]\nfoo.example.com\nbar.example.com\n[dbservers]\none.example.com\ntwo.example.com\nthree.example.com\n",
66+
assertEquals("""
67+
mail.example.com
68+
[webservers]
69+
foo.example.com
70+
bar.example.com
71+
[dbservers]
72+
one.example.com
73+
two.example.com
74+
three.example.com
75+
""",
6876
inventoryText);
6977
}
7078

conga-ansible-plugin/src/test/java/net/wedjaa/ansible/vault/ProvisioningInfo.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public class ProvisioningInfo {
2222
String apiClientId;
2323
String apiPassword;
2424

25-
public ProvisioningInfo() {
26-
27-
}
28-
2925
public String getApiUser() {
3026
return apiUser;
3127
}

conga-ansible-plugin/src/test/java/net/wedjaa/ansible/vault/crypto/VaultHandlerTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ class VaultHandlerTest {
3535
private static final String TEST_PASSWORD = "password";
3636
private static final String TEST_WRONG_PASSWORD = "not_this_one";
3737
private static final String WRONG_PASS_EX = "HMAC Digest doesn't match - possibly it's the wrong password.";
38-
private static final String DECODED_VAULT = "!net.wedjaa.ansible.vault.ProvisioningInfo\n"
39-
+ "apiClientId: The provisioner ClientId\n"
40-
+ "apiPassword: The secret password\n"
41-
+ "apiUser: Secret User\n";
42-
43-
private final static Logger logger = LoggerFactory.getLogger(VaultHandlerTest.class);
38+
private static final String DECODED_VAULT = """
39+
!net.wedjaa.ansible.vault.ProvisioningInfo
40+
apiClientId: The provisioner ClientId
41+
apiPassword: The secret password
42+
apiUser: Secret User
43+
""";
44+
45+
private static final Logger logger = LoggerFactory.getLogger(VaultHandlerTest.class);
4446

4547
@Test
4648
void testByteArrayValidVault() throws Exception {

0 commit comments

Comments
 (0)