Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ShutdownResponse. Background-cleanup now responds to Shutdown by …
…restarting stream. Signed-off-by: Cai Yufei (INST/ECS1) <yufei.cai@bosch-si.com>
- Loading branch information
Showing
3 changed files
with
195 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...ands/common/src/main/java/org/eclipse/ditto/signals/commands/common/ShutdownResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.signals.commands.common; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
import org.eclipse.ditto.json.JsonFactory; | ||
import org.eclipse.ditto.json.JsonField; | ||
import org.eclipse.ditto.json.JsonFieldDefinition; | ||
import org.eclipse.ditto.json.JsonObject; | ||
import org.eclipse.ditto.json.JsonObjectBuilder; | ||
import org.eclipse.ditto.json.JsonValue; | ||
import org.eclipse.ditto.model.base.common.HttpStatusCode; | ||
import org.eclipse.ditto.model.base.headers.DittoHeaders; | ||
import org.eclipse.ditto.model.base.json.JsonParsableCommandResponse; | ||
import org.eclipse.ditto.model.base.json.JsonSchemaVersion; | ||
|
||
/** | ||
* Response to {@code Shutdown} containing the retrieved config. | ||
*/ | ||
@JsonParsableCommandResponse(type = ShutdownResponse.TYPE) | ||
public final class ShutdownResponse extends CommonCommandResponse<ShutdownResponse> { | ||
|
||
/** | ||
* Type of this command response. | ||
*/ | ||
public static final String TYPE = TYPE_PREFIX + Shutdown.NAME; | ||
|
||
private static final JsonFieldDefinition<JsonValue> MESSAGE = JsonFactory.newJsonValueFieldDefinition("message"); | ||
|
||
private final JsonValue message; | ||
|
||
private ShutdownResponse(final JsonValue message, final DittoHeaders dittoHeaders) { | ||
super(TYPE, HttpStatusCode.OK, dittoHeaders); | ||
this.message = message; | ||
} | ||
|
||
/** | ||
* Create a {@code ShutdownResponse}. | ||
* | ||
* @param message what to say in the response. | ||
* @param headers Ditto headers. | ||
* @return the {@code ShutdownResponse}. | ||
*/ | ||
public static ShutdownResponse of(final Object message, final DittoHeaders headers) { | ||
return new ShutdownResponse(JsonValue.of(message), headers); | ||
} | ||
|
||
/** | ||
* Creates a new {@code ShutdownResponse} from the given JSON object. | ||
* | ||
* @param jsonObject the JSON object of which the Shutdown is to be created. | ||
* @param dittoHeaders the headers. | ||
* @return the command. | ||
* @throws NullPointerException if {@code jsonObject} is {@code null}. | ||
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the JSON object does not contain the field "config". | ||
*/ | ||
public static ShutdownResponse fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { | ||
return new ShutdownResponse(jsonObject.getValueOrThrow(MESSAGE), dittoHeaders); | ||
} | ||
|
||
@Override | ||
protected void appendPayload(final JsonObjectBuilder jsonObjectBuilder, final JsonSchemaVersion schemaVersion, | ||
final Predicate<JsonField> predicate) { | ||
|
||
jsonObjectBuilder.set(MESSAGE, message); | ||
} | ||
|
||
@Override | ||
public ShutdownResponse setDittoHeaders(final DittoHeaders dittoHeaders) { | ||
return new ShutdownResponse(message, dittoHeaders); | ||
} | ||
|
||
@Override | ||
public boolean equals(final Object that) { | ||
if (super.equals(that) && that instanceof ShutdownResponse) { | ||
return Objects.equals(message, ((ShutdownResponse) that).message); | ||
} else { | ||
return false; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), message); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "ShutdownResponse[message=" + message + "," + super.toString() + "]"; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../common/src/test/java/org/eclipse/ditto/signals/commands/common/ShutdownResponseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright (c) 2019 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.signals.commands.common; | ||
|
||
import static org.mutabilitydetector.unittesting.AllowedReason.provided; | ||
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf; | ||
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable; | ||
|
||
import org.eclipse.ditto.json.JsonValue; | ||
import org.junit.Test; | ||
|
||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
|
||
/** | ||
* Tests {@link ShutdownResponse}. | ||
*/ | ||
public final class ShutdownResponseTest { | ||
|
||
@Test | ||
public void assertImmutability() { | ||
assertInstancesOf(ShutdownResponse.class, areImmutable(), provided(JsonValue.class).isAlsoImmutable()); | ||
} | ||
|
||
@Test | ||
public void testHashCodeAndEquals() { | ||
EqualsVerifier.forClass(ShutdownResponse.class) | ||
.usingGetClass() | ||
.verify(); | ||
} | ||
|
||
} |