Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14 fix invalid attribute and multiple with data for mpHealth #20

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,91 +33,57 @@
import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;

/**
* Health
*/
public class HealthCheckResponseBuilderImpl extends HealthCheckResponseBuilder {

private static final TraceComponent tc = Tr.register(HealthCheckResponseBuilderImpl.class);

private String name;
private Optional<Map<String, Object>> data;

private final Optional<Map<String, Object>> data = Optional.of(new HashMap<String, Object>());
private State state;

public HealthCheckResponseBuilderImpl() {}

@Override
public HealthCheckResponseBuilder name(String sName) {
synchronized (this) {
if (sName == null || sName.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Name is null"));
}
this.name = sName;
}
public HealthCheckResponseBuilder name(String name) {
validateName(name);
this.name = name;
return this;
}

@Override
public HealthCheckResponseBuilder withData(String key, String value) {
synchronized (this) {
Map<String, Object> attribute = new HashMap<String, Object>();
if (key == null || key.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Key is null"));
}
attribute.put(key, value);
this.data = Optional.of(attribute);
}
return this;
return withData(key, (Object) value);
}

@Override
public HealthCheckResponseBuilder withData(String key, long value) {
synchronized (this) {
Map<String, Object> attribute = new HashMap<String, Object>();
if (key == null || key.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Key is null"));
}
attribute.put(key, value);
this.data = Optional.of(attribute);
}
return this;
return withData(key, Long.valueOf(value));
}

@Override
public HealthCheckResponseBuilder withData(String key, boolean value) {
synchronized (this) {
Map<String, Object> attribute = new HashMap<String, Object>();
if (key == null || key.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Key is null"));
}
attribute.put(key, value);
this.data = Optional.of(attribute);
}
return this;
return withData(key, Boolean.valueOf(value));
}

@Override
public HealthCheckResponseBuilder up() {
synchronized (this) {
this.state = State.UP;
}
this.state = State.UP;
return this;
}

@Override
public HealthCheckResponseBuilder down() {
synchronized (this) {
this.state = State.DOWN;
}
this.state = State.DOWN;
return this;
}

@Override
public HealthCheckResponseBuilder state(boolean up) {
synchronized (this) {
if (up)
this.state = State.UP;
}
if (up)
this.state = State.UP;
else
this.state = State.DOWN;
return this;
}

Expand Down Expand Up @@ -146,4 +112,25 @@ public String toString() {
return builder.toString();
}

}
private HealthCheckResponseBuilder withData(String key, Object value) {
validateKey(key);

if (data.isPresent())
data.get().put(key, value);

return this;
}

private void validateName(String name) {
if (name == null || name.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Name is null"));
}
}

private void validateKey(String key) {
if (key == null || key.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Key is null"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

package com.ibm.ws.microprofile.health.impl;

import java.util.Iterator;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import org.eclipse.microprofile.health.HealthCheckResponse;
import org.eclipse.microprofile.health.HealthCheckResponseBuilder;
Expand All @@ -50,21 +52,33 @@ public HealthCheckResponseImpl(String name, State state, Optional<Map<String, Ob

@Override
public String getName() {
if (this.name != null)
return this.name;
return null;
if (this.name == null || this.name.length() == 0)
throw new IllegalArgumentException(Tr.formatMessage(tc, "Name is null"));
else
return name;
}

@Override
public State getState() {
if (state != null)
return state;
if (state == null || (state != State.UP && state != State.DOWN))
throw new IllegalArgumentException(Tr.formatMessage(tc, "State is null"));
else
return State.UP; //default
return state;
}

@Override
public Optional<Map<String, Object>> getData() {
if (data.isPresent()) {
Set<String> keys = data.get().keySet();
Iterator<String> keysIter = keys.iterator();
while (keysIter.hasNext()) {
String key = keysIter.next();
if (key == null || key.length() == 0) {
throw new IllegalArgumentException(Tr.formatMessage(tc, "Key is null"));
}
}
}

return data;
}

Expand All @@ -74,9 +88,9 @@ public static HealthCheckResponseBuilder named(String sName) {
if (sName != null && sName.length() > 0) {
builder = HealthCheckResponse.named(sName);
builder.name(sName);
}
} else
throw new IllegalArgumentException(Tr.formatMessage(tc, "Name is null"));
return builder;

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class HealthCheckHttpResponseBuilder {

JSON json = null;

void addResponse(Set<HealthCheckResponse> hcResponseSet) {
void addResponses(Set<HealthCheckResponse> hcResponseSet) {
Iterator<HealthCheckResponse> hcResponseIt = hcResponseSet.iterator();
while (hcResponseIt.hasNext()) {
HealthCheckResponse hcResponse = hcResponseIt.next();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public void performHealthCheck(HttpServletRequest request, HttpServletResponse h
Tr.debug(tc, "In performHealthCheck(): hcResponses = " + hcResponses);

if (!hcResponses.isEmpty())
hcHttpResponseBuilder.addResponse(hcResponses);
hcHttpResponseBuilder.addResponses(hcResponses);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private synchronized void findHealthService(final HttpServletRequest request) th

ServiceReference<HealthCheckService> ref = ctxt.getServiceReference(HealthCheckService.class);
if (ref == null) {
logger.log(Level.SEVERE, "healthcheck.bean.call.exception.CWMH0000E", "HealthCheckService");
logger.log(Level.SEVERE, "healthcheck.CWMH0000E", "HealthCheckService");
throw new ServletException(Tr.formatMessage(tc, "OSGI_SERVICE_ERROR", "HealthCheckService"));
} else {
healthService = ctxt.getService(ref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ public void testWithDataWithString() throws Exception {

Map<String, Object> attribute = new HashMap<String, Object>();
attribute.put("first-key", "first-value");
attribute.put("second-key", "second-value");
final Optional<Map<String, Object>> data = Optional.of(attribute);

HealthCheckResponseBuilder builder = new HealthCheckResponseBuilderImpl();
builder = builder.withData("first-key", "first-value");
builder = builder.withData("second-key", "second-value");
HealthCheckResponse response = builder.build();
Optional<Map<String, Object>> testData = response.getData();

Expand Down Expand Up @@ -163,10 +165,12 @@ public void testWithDataWithLong() throws Exception {

Map<String, Object> attribute = new HashMap<String, Object>();
attribute.put("first-key", (long) 15000);
attribute.put("first-key", (long) 5000);
final Optional<Map<String, Object>> data = Optional.of(attribute);

HealthCheckResponseBuilder builder = new HealthCheckResponseBuilderImpl();
builder = builder.withData("first-key", 15000);
builder = builder.withData("first-key", 5000);
HealthCheckResponse response = builder.build();
Optional<Map<String, Object>> testData = response.getData();

Expand Down