14
14
15
15
import static org .eclipse .ditto .things .model .TestConstants .Thing .THING_V2 ;
16
16
17
+ import java .lang .reflect .Field ;
17
18
import java .util .Collections ;
19
+ import java .util .Optional ;
20
+ import java .util .concurrent .CompletableFuture ;
18
21
19
22
import org .apache .pekko .actor .ActorSystem ;
20
23
import org .eclipse .ditto .base .model .headers .DittoHeaders ;
29
32
import org .eclipse .ditto .things .model .signals .commands .modify .MigrateThingDefinitionResponse ;
30
33
import org .eclipse .ditto .things .model .signals .events .ThingDefinitionMigrated ;
31
34
import org .eclipse .ditto .things .service .persistence .actors .ETagTestUtils ;
35
+ import org .eclipse .ditto .wot .api .generator .WotThingSkeletonGenerator ;
32
36
import org .junit .Before ;
33
37
import org .junit .Test ;
34
38
35
39
import com .typesafe .config .ConfigFactory ;
36
40
41
+ import org .mockito .Mockito ;
42
+
37
43
/**
38
- * Unit test for {@link MigrateThingDefinitionStrategy} with injected mock of WotThingSkeletonGenerator.
44
+ * Unit test for {@link MigrateThingDefinitionStrategy}, verifying correct behavior when using a mocked {@link WotThingSkeletonGenerator} .
39
45
*/
40
46
public final class MigrateThingDefinitionStrategyTest extends AbstractCommandStrategyTest {
41
47
48
+ private static final String TEST_THING_DEFINITION_URL = "https://mock-model-1.0.0.tm.jsonld" ;
49
+
42
50
private MigrateThingDefinitionStrategy underTest ;
51
+ private final WotThingSkeletonGenerator skeletonGeneratorMock = Mockito .mock (WotThingSkeletonGenerator .class );
43
52
44
53
@ Before
45
- public void setUp () {
54
+ public void setUp () throws Exception {
46
55
final ActorSystem actorSystem = ActorSystem .create ("test" , ConfigFactory .load ("test" ));
47
56
underTest = new MigrateThingDefinitionStrategy (actorSystem );
57
+ injectSkeletonGeneratorMock (underTest , skeletonGeneratorMock );
48
58
}
49
59
50
60
@ Test
@@ -53,36 +63,50 @@ public void migrateExistingThing() {
53
63
final ThingId thingId = context .getState ();
54
64
final Thing existingThing = THING_V2 .toBuilder ().setRevision (NEXT_REVISION - 1 ).build ();
55
65
56
- final JsonObject migrationPayload = JsonFactory .newObjectBuilder ()
57
- .set ("attributes" , JsonFactory .newObjectBuilder ().set ("manufacturer" , "New Corp" ).build ())
58
- .build ();
66
+ final Thing skeletonThing = ThingsModelFactory .newThing (getExpectedThingJson ());
59
67
60
- final String thingDefinitionUrl =
61
- "https://eclipse.dev/ditto/wot/example-models/dimmable-colored-lamp-1.0.0.tm.jsonld" ;
68
+ Mockito . when ( skeletonGeneratorMock . provideThingSkeletonForCreation ( Mockito . any (), Mockito . any (), Mockito . any ()))
69
+ . thenReturn ( CompletableFuture . completedFuture ( Optional . of ( skeletonThing ))) ;
62
70
63
71
final MigrateThingDefinition command = MigrateThingDefinition .of (
64
72
thingId ,
65
- thingDefinitionUrl ,
66
- migrationPayload ,
73
+ TEST_THING_DEFINITION_URL ,
74
+ JsonObject . empty () ,
67
75
Collections .emptyMap (),
68
76
true ,
69
77
DittoHeaders .empty ()
70
78
);
71
79
72
- final MigrateThingDefinitionResponse expectedResponse = ETagTestUtils .migrateThingDefinitionResponse (thingId ,
73
- getThingJson (thingDefinitionUrl ),
74
- getMergedThing (thingDefinitionUrl ),
75
- command .getDittoHeaders ());
80
+ final MigrateThingDefinitionResponse expectedResponse = ETagTestUtils .migrateThingDefinitionResponse (
81
+ thingId ,
82
+ getExpectedThingJson (),
83
+ getExpectedMergedThing (),
84
+ command .getDittoHeaders ()
85
+ );
86
+
87
+ assertStagedModificationResult (underTest , existingThing , command , ThingDefinitionMigrated .class , expectedResponse );
88
+ }
76
89
77
- assertStagedModificationResult (underTest , existingThing , command , ThingDefinitionMigrated .class ,
78
- expectedResponse );
90
+ private void injectSkeletonGeneratorMock (final MigrateThingDefinitionStrategy strategy ,
91
+ final WotThingSkeletonGenerator skeletonGeneratorMock ) throws Exception {
92
+ Class <?> clazz = strategy .getClass ();
93
+ while (clazz != null && !clazz .equals (Object .class )) {
94
+ try {
95
+ final Field field = clazz .getDeclaredField ("wotThingSkeletonGenerator" );
96
+ field .setAccessible (true );
97
+ field .set (strategy , skeletonGeneratorMock );
98
+ return ;
99
+ } catch (final NoSuchFieldException e ) {
100
+ clazz = clazz .getSuperclass ();
101
+ }
102
+ }
103
+ throw new NoSuchFieldException ("wotThingSkeletonGenerator not found in class hierarchy" );
79
104
}
80
105
81
- private static JsonObject getThingJson ( String thingDefinitionUrl ) {
106
+ private static JsonObject getExpectedThingJson ( ) {
82
107
return JsonFactory .newObjectBuilder ()
83
- .set ("definition" , thingDefinitionUrl )
108
+ .set ("definition" , TEST_THING_DEFINITION_URL )
84
109
.set ("attributes" , JsonFactory .newObjectBuilder ()
85
- .set ("manufacturer" , "New Corp" )
86
110
.set ("on" , false )
87
111
.set ("color" , JsonFactory .newObjectBuilder ()
88
112
.set ("r" , 0 )
@@ -94,9 +118,8 @@ private static JsonObject getThingJson(String thingDefinitionUrl) {
94
118
.build ();
95
119
}
96
120
97
-
98
- private Thing getMergedThing (final String thingDefinitionUrl ) {
99
- return ThingsModelFactory .newThingBuilder (getThingJson (thingDefinitionUrl ))
121
+ private Thing getExpectedMergedThing () {
122
+ return ThingsModelFactory .newThingBuilder (getExpectedThingJson ())
100
123
.setRevision (ThingRevision .newInstance (NEXT_REVISION ))
101
124
.build ();
102
125
}
0 commit comments