/
CommandAndEventJsonExamplesProducer.java
executable file
·156 lines (131 loc) · 8.48 KB
/
CommandAndEventJsonExamplesProducer.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.services.models.things;
import static org.eclipse.ditto.model.base.auth.AuthorizationModelFactory.newAuthSubject;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.util.Arrays;
import org.eclipse.ditto.json.JsonFactory;
import org.eclipse.ditto.json.JsonField;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.json.JsonParseOptions;
import org.eclipse.ditto.model.base.auth.AuthorizationSubject;
import org.eclipse.ditto.model.base.json.FieldType;
import org.eclipse.ditto.model.base.json.Jsonifiable;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.model.things.ThingLifecycle;
import org.eclipse.ditto.model.things.ThingsModelFactory;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoRetrieveModifiedThingTagsResponse;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoRetrieveThing;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoRetrieveThingResponse;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoRetrieveThings;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoRetrieveThingsResponse;
import org.eclipse.ditto.services.models.things.commands.sudo.SudoStreamModifiedEntities;
/* */
public final class CommandAndEventJsonExamplesProducer {
private static final JsonParseOptions JSON_PARSE_OPTIONS =
JsonFactory.newParseOptionsBuilder().withoutUrlDecoding().build();
public static void main(final String... args) throws IOException {
if (args.length != 1) {
System.err.println("Exactly 1 argument required: the target folder in which to generate the JSON files");
System.exit(-1);
}
produce(Paths.get(args[0]));
}
private static void produce(final Path rootPath) throws IOException {
produceSudoCommands(rootPath.resolve("commands"));
}
private static void produceSudoCommands(final Path rootPath) throws IOException {
final Path sudoCommandsDir = rootPath.resolve(Paths.get("sudo"));
Files.createDirectories(sudoCommandsDir);
final JsonFieldSelector sudoFieldSelector =
JsonFactory.newFieldSelector("thingId,_revision,__lifecycle", JSON_PARSE_OPTIONS);
final SudoRetrieveThing sudoRetrieveThing =
SudoRetrieveThing.of("org.eclipse.ditto:the_thingId_1", TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThing.json")), sudoRetrieveThing);
final SudoRetrieveThing sudoRetrieveThingWithFieldSelector =
SudoRetrieveThing.of("org.eclipse.ditto:the_thingId_1", sudoFieldSelector, TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThing-withFieldSelector.json")),
sudoRetrieveThingWithFieldSelector);
final SudoRetrieveThings sudoRetrieveThings = SudoRetrieveThings
.of(Arrays.asList("org.eclipse.ditto:the_thingId_1", "org.eclipse.ditto:the_thingId_2",
"org.eclipse.ditto:the_thingId_3"),
TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThings.json")), sudoRetrieveThings);
final SudoRetrieveThings sudoRetrieveThingsWithFieldSelector = SudoRetrieveThings
.of(Arrays.asList("org.eclipse.ditto:the_thingId_1", "org.eclipse.ditto:the_thingId_2",
"org.eclipse.ditto:the_thingId_3"),
sudoFieldSelector, TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThings-withFieldSelector.json")),
sudoRetrieveThingsWithFieldSelector);
final Thing exampleThingJson = createExampleThing("org.eclipse.ditto:the_thingId_1");
final Thing exampleThing2Json = createExampleThing("org.eclipse.ditto:the_thingId_2");
final SudoRetrieveThingResponse sudoRetrieveThingResponse =
SudoRetrieveThingResponse.of(exampleThingJson, FieldType.notHidden(), TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThingResponse.json")), sudoRetrieveThingResponse);
final SudoRetrieveThingResponse sudoRetrieveThingResponseWithFieldSelector =
SudoRetrieveThingResponse.of(exampleThingJson, //
sudoFieldSelector, FieldType.regularOrSpecial(), TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThingResponse-withFieldSelector.json")),
sudoRetrieveThingResponseWithFieldSelector);
final SudoRetrieveThingsResponse sudoRetrieveThingsResponse = SudoRetrieveThingsResponse
.of(Arrays.asList(exampleThingJson, exampleThing2Json), FieldType.notHidden(),
TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThingsResponse.json")), sudoRetrieveThingsResponse);
final SudoRetrieveThingsResponse sudoRetrieveThingsResponseWithFieldSelector =
SudoRetrieveThingsResponse.of(Arrays.asList(exampleThingJson, exampleThing2Json), //
sudoFieldSelector, FieldType.regularOrSpecial(), TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveThingsResponse-withFieldSelector.json")),
sudoRetrieveThingsResponseWithFieldSelector);
final Duration timespan = Duration.ofMinutes(5);
final Duration offset = Duration.ofMinutes(1);
final int elementsPerSecond = 100;
final String elementRecipient = "akka.tcp://actorSystem@hostname/user/elementRecipientActor";
final String statusRecipient = "akka.tcp://actorSystem@hostname/user/statusRecipientActor";
final SudoStreamModifiedEntities sudoRetrieveModifiedThingTags =
SudoStreamModifiedEntities.of(timespan, offset, elementsPerSecond, elementRecipient, statusRecipient,
TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoStreamModifiedEntities.json")),
sudoRetrieveModifiedThingTags);
final SudoRetrieveModifiedThingTagsResponse sudoRetrieveModifiedThingTagsResponse =
SudoRetrieveModifiedThingTagsResponse.of(Arrays
.asList(ThingTag.of("org.eclipse.ditto:the_thingId_1", 3),
ThingTag.of("org.eclipse.ditto:the_thingId_2", 6),
ThingTag.of("org.eclipse.ditto:the_thingId_4", 1)), TestConstants.EMPTY_HEADERS);
writeJson(sudoCommandsDir.resolve(Paths.get("sudoRetrieveModifiedThingTagsResponse.json")),
sudoRetrieveModifiedThingTagsResponse);
}
private static Thing createExampleThing(final String thingId) {
final AuthorizationSubject authorizationSubject = newAuthSubject("the_acl_subject");
final AuthorizationSubject anotherAuthorizationSubject = newAuthSubject("another_acl_subject");
return ThingsModelFactory.newThingBuilder() //
.setLifecycle(ThingLifecycle.ACTIVE) //
.setRevision(1) //
.setPermissions(authorizationSubject, org.eclipse.ditto.model.things.Permission.READ,
org.eclipse.ditto.model.things.Permission.WRITE,
org.eclipse.ditto.model.things.Permission.ADMINISTRATE) //
.setPermissions(anotherAuthorizationSubject, org.eclipse.ditto.model.things.Permission.READ,
org.eclipse.ditto.model.things.Permission.WRITE) //
.setId("org.eclipse.ditto.example:" + thingId) //
.build();
}
private static void writeJson(final Path path, final Jsonifiable.WithPredicate<JsonObject, JsonField> jsonifiable)
throws IOException {
final String jsonString = jsonifiable.toJsonString(FieldType.regularOrSpecial());
System.out.println("Writing file: " + path.toAbsolutePath());
Files.write(path, jsonString.getBytes());
}
}