/
DeleteAttributeStrategyTest.java
141 lines (121 loc) · 6.02 KB
/
DeleteAttributeStrategyTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (c) 2017 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.services.things.persistence.actors.strategies.commands;
import static org.eclipse.ditto.model.things.TestConstants.Thing.THING_V2;
import static org.mutabilitydetector.unittesting.MutabilityAssert.assertInstancesOf;
import static org.mutabilitydetector.unittesting.MutabilityMatchers.areImmutable;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonPointer;
import org.eclipse.ditto.json.JsonValue;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingId;
import org.eclipse.ditto.services.utils.persistentactors.commands.CommandStrategy;
import org.eclipse.ditto.signals.commands.things.modify.DeleteAttribute;
import org.eclipse.ditto.signals.commands.things.modify.DeleteAttributeResponse;
import org.eclipse.ditto.signals.events.things.AttributeDeleted;
import org.junit.Before;
import org.junit.Test;
/**
* Unit test for {@link DeleteAttributeStrategy}.
*/
public final class DeleteAttributeStrategyTest extends AbstractCommandStrategyTest {
private DeleteAttributeStrategy underTest;
@Before
public void setUp() {
underTest = new DeleteAttributeStrategy();
}
@Test
public void assertImmutability() {
assertInstancesOf(DeleteAttributeStrategy.class, areImmutable());
}
@Test
public void successfullyDeleteAttribute() {
final JsonPointer attrPointer = JsonFactory.newPointer("/location/longitude");
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteAttribute command = DeleteAttribute.of(context.getState(), attrPointer, DittoHeaders.empty());
assertModificationResult(underTest, THING_V2, command,
AttributeDeleted.class,
DeleteAttributeResponse.of(context.getState(),
attrPointer, command.getDittoHeaders()));
}
@Test
public void deleteAttributeFromThingWithoutAttributes() {
final JsonPointer attrPointer = JsonFactory.newPointer("/location/longitude");
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteAttribute command = DeleteAttribute.of(context.getState(), attrPointer, DittoHeaders.empty());
final DittoRuntimeException expectedException =
ExceptionFactory.attributeNotFound(context.getState(), attrPointer, command.getDittoHeaders());
assertErrorResult(underTest, THING_V2.removeAttributes(), command, expectedException);
}
@Test
public void deleteAttributeFromThingWithoutThatAttribute() {
final JsonPointer attrPointer = JsonFactory.newPointer("/location/longitude");
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteAttribute command = DeleteAttribute.of(context.getState(), attrPointer, DittoHeaders.empty());
final DittoRuntimeException expectedException =
ExceptionFactory.attributeNotFound(context.getState(), attrPointer, command.getDittoHeaders());
assertErrorResult(underTest, THING_V2.removeAttribute(attrPointer), command, expectedException);
}
/**
* Having attributes:
* <code>
* "attributes": {
* "complex": {
* "nested": "foo"
* }
* }
* </code>
* a delete on <code>/complex/nested/non/existent/path</code> should result in an error
*/
@Test
public void deleteFromComplexNestedAttributeWithoutThatPath() {
// the last known object key must be still part of the pointer, or the test will not work. Since "nested"
// is the last known part of the pointer that is part of the attributes, it has to be a primitive value.
// if it was an object and the object did not contain "non", then the test would fail trivially.
final JsonPointer attrPointer = JsonFactory.newPointer("/complex/nested/non/existent/path");
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteAttribute command = DeleteAttribute.of(context.getState(), attrPointer, DittoHeaders.empty());
final DittoRuntimeException expectedException =
ExceptionFactory.attributeNotFound(context.getState(), attrPointer, command.getDittoHeaders());
final Thing thing = THING_V2.toBuilder()
.removeAllAttributes()
.setAttribute(JsonPointer.of("/complex"), JsonObject.of("{\"nested\": \"foo\"}"))
.build();
assertErrorResult(underTest, thing, command, expectedException);
}
/**
* Having attributes:
* <code>
* "attributes": {
* "flat":"foobar"
* }
* </code>
* a delete on <code>/flat/non/existent/path</code> should result in an error
*/
@Test
public void deleteFromFlatAttributeWithoutThatPath() {
final JsonPointer attrPointer = JsonFactory.newPointer("/flat/non/existent/path");
final CommandStrategy.Context<ThingId> context = getDefaultContext();
final DeleteAttribute command = DeleteAttribute.of(context.getState(), attrPointer, DittoHeaders.empty());
final DittoRuntimeException expectedException =
ExceptionFactory.attributeNotFound(context.getState(), attrPointer, command.getDittoHeaders());
final Thing thing = THING_V2.toBuilder()
.removeAllAttributes()
.setAttribute(JsonPointer.of("/flat"), JsonValue.of("foobar"))
.build();
assertErrorResult(underTest, thing, command, expectedException);
}
}