Commit 98629238 by Johannes Edmeier

Use lombok in spring-boot-admin-client

parent 071e36c7
<!--
~ Copyright 2014-2017 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
......@@ -35,6 +51,11 @@
<artifactId>jolokia-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<!-- Make json-simple non-optional.
It is marked optional in boot-dependencies, but required by jolokia-core.
Without this fix it would be missing when used war-packaging. -->
......
......@@ -17,6 +17,7 @@ package de.codecentric.boot.admin.client.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@lombok.Data
@ConfigurationProperties(prefix = "spring.boot.admin.client")
public class ClientProperties {
......@@ -65,22 +66,6 @@ public class ClientProperties {
*/
private boolean enabled = true;
public void setUrl(String[] url) {
this.url = url.clone();
}
public String[] getUrl() {
return url.clone();
}
public void setApiPath(String apiPath) {
this.apiPath = apiPath;
}
public String getApiPath() {
return apiPath;
}
public String[] getAdminUrl() {
String adminUrls[] = url.clone();
for (int i = 0; i < adminUrls.length; i++) {
......@@ -88,60 +73,4 @@ public class ClientProperties {
}
return adminUrls;
}
public long getPeriod() {
return period;
}
public void setPeriod(int period) {
this.period = period;
}
public void setUsername(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public boolean isAutoDeregistration() {
return autoDeregistration;
}
public void setAutoDeregistration(boolean autoDeregistration) {
this.autoDeregistration = autoDeregistration;
}
public boolean isAutoRegistration() {
return autoRegistration;
}
public void setAutoRegistration(boolean autoRegistration) {
this.autoRegistration = autoRegistration;
}
public boolean isRegisterOnce() {
return registerOnce;
}
public void setRegisterOnce(boolean registerOnce) {
this.registerOnce = registerOnce;
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}
......@@ -20,6 +20,7 @@ import java.util.Map;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
@lombok.Data
@ConfigurationProperties(prefix = "spring.boot.admin.client.instance")
public class InstanceProperties {
/**
......@@ -66,67 +67,4 @@ public class InstanceProperties {
*/
private Map<String, String> metadata = new HashMap<>();
public String getManagementUrl() {
return managementUrl;
}
public void setManagementUrl(String managementUrl) {
this.managementUrl = managementUrl;
}
public String getServiceUrl() {
return serviceUrl;
}
public void setServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
}
public String getHealthUrl() {
return healthUrl;
}
public void setHealthUrl(String healthUrl) {
this.healthUrl = healthUrl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isPreferIp() {
return preferIp;
}
public void setPreferIp(boolean preferIp) {
this.preferIp = preferIp;
}
public Map<String, String> getMetadata() {
return metadata;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}
public String getManagementBaseUrl() {
return managementBaseUrl;
}
public void setManagementBaseUrl(String managementBaseUrl) {
this.managementBaseUrl = managementBaseUrl;
}
public String getServiceBaseUrl() {
return serviceBaseUrl;
}
public void setServiceBaseUrl(String serviceBaseUrl) {
this.serviceBaseUrl = serviceBaseUrl;
}
}
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,7 +18,6 @@ package de.codecentric.boot.admin.client.registration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.Assert;
/**
......@@ -26,6 +25,7 @@ import org.springframework.util.Assert;
*
* @author Johannes Edmeier
*/
@lombok.Data
public class Application {
private final String name;
private final String managementUrl;
......@@ -33,144 +33,26 @@ public class Application {
private final String serviceUrl;
private final Map<String, String> metadata;
protected Application(Builder builder) {
Assert.hasText(builder.name, "name must not be empty!");
Assert.hasText(builder.healthUrl, "healthUrl must not be empty!");
this.healthUrl = builder.healthUrl;
this.managementUrl = builder.managementUrl;
this.serviceUrl = builder.serviceUrl;
this.name = builder.name;
this.metadata = Collections.unmodifiableMap(new HashMap<>(builder.metadata));
}
public static Builder create(String name) {
return new Builder(name);
}
public static class Builder {
private String name;
private String managementUrl;
private String healthUrl;
private String serviceUrl;
private Map<String, String> metadata = new HashMap<>();
private Builder(String name) {
@lombok.Builder(builderClassName = "Builder")
protected Application(String name,
String managementUrl,
String healthUrl,
String serviceUrl,
@lombok.Singular("metadata") Map<String, String> metadata) {
Assert.hasText(name, "name must not be empty!");
Assert.hasText(healthUrl, "healthUrl must not be empty!");
this.name = name;
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withServiceUrl(String serviceUrl) {
this.serviceUrl = serviceUrl;
return this;
}
public Builder withHealthUrl(String healthUrl) {
this.healthUrl = healthUrl;
return this;
}
public Builder withManagementUrl(String managementUrl) {
this.managementUrl = managementUrl;
return this;
}
public Builder withMetadata(String key, String value) {
this.metadata.put(key, value);
return this;
}
public Builder withMetadata(Map<String, String> metadata) {
this.metadata.putAll(metadata);
return this;
}
public Application build() {
return new Application(this);
}
}
public String getName() {
return name;
}
public String getManagementUrl() {
return managementUrl;
}
public String getHealthUrl() {
return healthUrl;
this.healthUrl = healthUrl;
this.serviceUrl = serviceUrl;
this.metadata = new HashMap<>(metadata);
}
public String getServiceUrl() {
return serviceUrl;
public static Builder create(String name) {
return Application.builder().name(name);
}
public Map<String, String> getMetadata() {
return metadata;
}
@Override
public String toString() {
return "Application [name=" + name + ", managementUrl=" + managementUrl + ", healthUrl="
+ healthUrl + ", serviceUrl=" + serviceUrl + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((healthUrl == null) ? 0 : healthUrl.hashCode());
result = prime * result + ((managementUrl == null) ? 0 : managementUrl.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((serviceUrl == null) ? 0 : serviceUrl.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Application other = (Application) obj;
if (healthUrl == null) {
if (other.healthUrl != null) {
return false;
}
} else if (!healthUrl.equals(other.healthUrl)) {
return false;
}
if (managementUrl == null) {
if (other.managementUrl != null) {
return false;
}
} else if (!managementUrl.equals(other.managementUrl)) {
return false;
return Collections.unmodifiableMap(metadata);
}
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (serviceUrl == null) {
if (other.serviceUrl != null) {
return false;
}
} else if (!serviceUrl.equals(other.serviceUrl)) {
return false;
}
return true;
}
}
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.client.registration;
import de.codecentric.boot.admin.client.config.InstanceProperties;
......@@ -6,6 +22,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import javax.servlet.ServletContext;
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.event.ApplicationReadyEvent;
......@@ -46,10 +63,10 @@ public class DefaultApplicationFactory implements ApplicationFactory {
@Override
public Application createApplication() {
return Application.create(getName())
.withHealthUrl(getHealthUrl())
.withManagementUrl(getManagementUrl())
.withServiceUrl(getServiceUrl())
.withMetadata(getMetadata())
.healthUrl(getHealthUrl())
.managementUrl(getManagementUrl())
.serviceUrl(getServiceUrl())
.metadata(getMetadata())
.build();
}
......
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -55,9 +55,9 @@ public class ApplicationRegistratorTest {
ApplicationFactory factory = mock(ApplicationFactory.class);
when(factory.createApplication()).thenReturn(Application.create("AppName")
.withManagementUrl("http://localhost:8080/mgmt")
.withHealthUrl("http://localhost:8080/health")
.withServiceUrl("http://localhost:8080")
.managementUrl("http://localhost:8080/mgmt")
.healthUrl("http://localhost:8080/health")
.serviceUrl("http://localhost:8080")
.build());
registrator = new ApplicationRegistrator(restTemplate, client, factory);
......@@ -76,9 +76,9 @@ public class ApplicationRegistratorTest {
assertThat(registrator.register()).isTrue();
Application applicationRef = Application.create("AppName")
.withHealthUrl("http://localhost:8080/health")
.withManagementUrl("http://localhost:8080/mgmt")
.withServiceUrl("http://localhost:8080")
.healthUrl("http://localhost:8080/health")
.managementUrl("http://localhost:8080/mgmt")
.serviceUrl("http://localhost:8080")
.build();
verify(restTemplate).postForEntity("http://sba:8080/api/applications",
new HttpEntity<>(applicationRef, headers), Map.class);
......@@ -127,9 +127,9 @@ public class ApplicationRegistratorTest {
assertThat(registrator.register()).isTrue();
Application applicationRef = Application.create("AppName")
.withHealthUrl("http://localhost:8080/health")
.withManagementUrl("http://localhost:8080/mgmt")
.withServiceUrl("http://localhost:8080")
.healthUrl("http://localhost:8080/health")
.managementUrl("http://localhost:8080/mgmt")
.serviceUrl("http://localhost:8080")
.build();
verify(restTemplate).postForEntity("http://sba:8080/api/applications",
new HttpEntity<>(applicationRef, headers), Map.class);
......@@ -151,9 +151,9 @@ public class ApplicationRegistratorTest {
assertThat(registrator.register()).isTrue();
Application applicationRef = Application.create("AppName")
.withHealthUrl("http://localhost:8080/health")
.withManagementUrl("http://localhost:8080/mgmt")
.withServiceUrl("http://localhost:8080")
.healthUrl("http://localhost:8080/health")
.managementUrl("http://localhost:8080/mgmt")
.serviceUrl("http://localhost:8080")
.build();
verify(restTemplate).postForEntity("http://sba:8080/api/applications",
new HttpEntity<>(applicationRef, headers), Map.class);
......
/*
* Copyright 2014-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.codecentric.boot.admin.registration;
import static org.assertj.core.api.Assertions.assertThat;
import de.codecentric.boot.admin.client.registration.Application;
import java.io.IOException;
import org.junit.Test;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import de.codecentric.boot.admin.client.registration.Application;
import static org.assertj.core.api.Assertions.assertThat;
public class ApplicationTest {
@Test
public void test_json_format() throws JsonProcessingException, IOException {
public void test_json_format() throws IOException {
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().build();
Application app = Application.create("test").withHealthUrl("http://health")
.withServiceUrl("http://service").withManagementUrl("http://management").build();
Application app = Application.create("test")
.healthUrl("http://health")
.serviceUrl("http://service")
.managementUrl("http://management")
.build();
DocumentContext json = JsonPath.parse(objectMapper.writeValueAsString(app));
assertThat((String)json.read("$.name")).isEqualTo("test");
assertThat((String)json.read("$.serviceUrl")).isEqualTo("http://service");
assertThat((String)json.read("$.managementUrl")).isEqualTo("http://management");
assertThat((String)json.read("$.healthUrl")).isEqualTo("http://health");
assertThat((String) json.read("$.name")).isEqualTo("test");
assertThat((String) json.read("$.serviceUrl")).isEqualTo("http://service");
assertThat((String) json.read("$.managementUrl")).isEqualTo("http://management");
assertThat((String) json.read("$.healthUrl")).isEqualTo("http://health");
}
@Test
public void test_equals_hashCode() {
Application a1 = Application.create("foo").withHealthUrl("healthUrl")
.withManagementUrl("mgmt").withServiceUrl("svc").build();
Application a2 = Application.create("foo").withHealthUrl("healthUrl")
.withManagementUrl("mgmt").withServiceUrl("svc").build();
Application a1 = Application.create("foo")
.healthUrl("healthUrl")
.managementUrl("mgmt")
.serviceUrl("svc")
.build();
Application a2 = Application.create("foo")
.healthUrl("healthUrl")
.managementUrl("mgmt")
.serviceUrl("svc")
.build();
assertThat(a1).isEqualTo(a2);
assertThat(a1.hashCode()).isEqualTo(a2.hashCode());
Application a3 = Application.create("foo").withHealthUrl("healthUrl2")
.withManagementUrl("mgmt").withServiceUrl("svc").build();
Application a3 = Application.create("foo")
.healthUrl("healthUrl2")
.managementUrl("mgmt")
.serviceUrl("svc")
.build();
assertThat(a1).isNotEqualTo(a3);
assertThat(a2).isNotEqualTo(a3);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment