14
14
*/
15
15
package com .ericsson .eiffel .remrem .semantics ;
16
16
17
-
18
17
import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_CANCELED ;
19
18
import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_FINISHED ;
20
19
import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_STARTED ;
80
79
import com .google .gson .JsonParser ;
81
80
82
81
@ Named ("eiffel-semantics" )
83
- public class SemanticsService implements MsgService {
82
+ public class SemanticsService implements MsgService {
84
83
private static final String ERROR = "error" ;
85
84
private static final String SUPPORTED_EVENT_TYPES = "SUPPORTED_EVENT_TYPES" ;
86
85
private static final String RESULT = "result" ;
@@ -97,16 +96,15 @@ public class SemanticsService implements MsgService{
97
96
public static final Logger log = LoggerFactory .getLogger (SemanticsService .class );
98
97
99
98
private static Gson gson = new Gson ();
100
- private static Map <EiffelEventType , Class <? extends Event >> eventTypes = SemanticsService .eventType ();
101
-
102
- public SemanticsService (){
99
+ private static Map <EiffelEventType , Class <? extends Event >> eventTypes = SemanticsService .eventType ();
100
+
101
+ public SemanticsService () {
103
102
for (final EiffelEventType msg : EiffelEventType .values ()) {
104
103
supportedEventTypes .add (msg .getEventName ());
105
104
}
106
105
}
107
-
108
- public static Map <EiffelEventType , Class <? extends Event >> eventType ()
109
- {
106
+
107
+ public static Map <EiffelEventType , Class <? extends Event >> eventType () {
110
108
eventTypes = new HashMap <>();
111
109
eventTypes .put (ARTIFACT_PUBLISHED , EiffelArtifactPublishedEvent .class );
112
110
eventTypes .put (ACTIVITY_FINISHED , EiffelActivityFinishedEvent .class );
@@ -130,91 +128,96 @@ public static Map<EiffelEventType, Class<? extends Event>> eventType()
130
128
eventTypes .put (TESTSUITE_STARTED , EiffelTestSuiteStartedEvent .class );
131
129
eventTypes .put (ISSUE_VERIFIED , EiffelIssueVerifiedEvent .class );
132
130
eventTypes .put (ARTIFACT_REUSED , EiffelArtifactReusedEvent .class );
133
- return eventTypes ;
131
+ return eventTypes ;
134
132
}
135
133
136
134
@ Override
137
- public String generateMsg (String msgType , JsonObject bodyJson ){
135
+ public String generateMsg (String msgType , JsonObject bodyJson ) {
138
136
EiffelEventType eiffelType = EiffelEventType .fromString (msgType );
139
137
if (eiffelType == null ) {
140
138
log .error ("Unknown message type requested: " + msgType );
141
- return createErrorResponse (msgType ,supportedEventTypes );
139
+ return createErrorResponse (msgType , supportedEventTypes );
142
140
}
143
141
Class <? extends Event > eventType = eventTypes .get (eiffelType );
144
142
145
143
JsonObject msgNodes = bodyJson .get (MSG_PARAMS ).getAsJsonObject ();
146
144
JsonObject eventNodes = bodyJson .get (EVENT_PARAMS ).getAsJsonObject ();
147
145
148
- //Compare the input JSON EventType with query parameter(-t) and also check type exist or not,
149
- //if input JSON EventType is missing adding query parameter as Type.
146
+ // Compare the input JSON EventType with query parameter(-t) and also
147
+ // check type exist or not,
148
+ // if input JSON EventType is missing adding query parameter as Type.
150
149
String inputEventType = getInputEventType (bodyJson );
151
- if (inputEventType == null || inputEventType .isEmpty ()){
152
- bodyJson .get (MSG_PARAMS ).getAsJsonObject ().get (META ).getAsJsonObject ().addProperty (TYPE , eiffelType .getEventName ());
153
- }else if (!(inputEventType .equals (eiffelType .getEventName ()))){
150
+ if (inputEventType == null || inputEventType .isEmpty ()) {
151
+ bodyJson .get (MSG_PARAMS ).getAsJsonObject ().get (META ).getAsJsonObject ().addProperty (TYPE ,
152
+ eiffelType .getEventName ());
153
+ } else if (!(inputEventType .equals (eiffelType .getEventName ()))) {
154
154
log .error ("check the input json message type : " + inputEventType );
155
- return createErrorResponse (eiffelType .getEventName (),"Mismatch of eventype in request query parameter with property 'type' in the input json message" );
155
+ return createErrorResponse (eiffelType .getEventName (),
156
+ "Mismatch of eventype in request query parameter with property 'type' in the input json message" );
156
157
}
157
158
158
159
Event event = eventCreation (eventType , msgNodes , eventNodes );
159
160
160
- String result = gson .toJson (event );
161
+ String result = gson .toJson (event );
161
162
try {
162
163
outputValidate (eiffelType , result );
163
164
} catch (EiffelValidationException e ) {
164
- log .error ("Could not validate message. Reason:" + e .getMessage () +"\n Cause: " + e .getCause ().toString ());
165
+ log .error ("Could not validate message. Reason:" + e .getMessage () + "\n Cause: " + e .getCause ().toString ());
165
166
return createErrorResponse (e .getMessage (), e .getCause ().toString ());
166
167
}
167
168
return result ;
168
169
}
169
- private static Event eventCreation ( Class <? extends Event > eventType , JsonObject msgNodes ,
170
- JsonObject eventNodes ) {
170
+
171
+ private static Event eventCreation ( Class <? extends Event > eventType , JsonObject msgNodes , JsonObject eventNodes ) {
171
172
eventNodes .add ("meta" , msgNodes .get ("meta" ));
172
173
Event event = createEvent (eventNodes , eventType );
173
174
event .setMeta (event .generateMeta (event .getMeta ()));
174
175
return event ;
175
176
}
176
-
177
+
177
178
private static Event createEvent (JsonObject eventNodes , Class <? extends Event > eventType ) {
178
179
return gson .fromJson (eventNodes , eventType );
179
180
}
180
181
181
- private String createErrorResponse (final String message , final String cause ){
182
+ private String createErrorResponse (final String message , final String cause ) {
182
183
JsonObject errorResponse = new JsonObject ();
183
184
errorResponse .addProperty (MESSAGE , message );
184
185
errorResponse .addProperty (CAUSE , cause .replace ("\n " , "" ));
185
186
return errorResponse .toString ();
186
187
}
187
- private String createErrorResponse (final String message , final ArrayList <String > supportedEventTypes ){
188
+
189
+ private String createErrorResponse (final String message , final ArrayList <String > supportedEventTypes ) {
188
190
JsonObject errorResponse = new JsonObject ();
189
191
errorResponse .addProperty (RESULT , ERROR );
190
- errorResponse .addProperty (MESSAGE , UNKNOWN_EVENT_TYPE_REQUESTED + " - " + message );
191
- errorResponse .addProperty (SUPPORTED_EVENT_TYPES ,supportedEventTypes .toString ());
192
+ errorResponse .addProperty (MESSAGE , UNKNOWN_EVENT_TYPE_REQUESTED + " - " + message );
193
+ errorResponse .addProperty (SUPPORTED_EVENT_TYPES , supportedEventTypes .toString ());
192
194
return errorResponse .toString ();
193
195
}
196
+
194
197
private void outputValidate (EiffelEventType eiffelType , String jsonStringInput ) throws EiffelValidationException {
195
198
EiffelValidator validator = EiffelOutputValidatorFactory .getEiffelValidator (eiffelType );
196
199
JsonObject jsonObject = new JsonParser ().parse (jsonStringInput ).getAsJsonObject ();
197
200
validator .validate (jsonObject );
198
- //custom validations on an event which is not covered in schema.
201
+ // custom validations on an event which is not covered in schema.
199
202
validator .customValidation (jsonObject );
200
203
}
201
204
202
205
@ Override
203
206
public String getEventId (JsonObject json ) {
204
- if (json .isJsonObject () && json .getAsJsonObject ().has (META ) && json .getAsJsonObject ()
205
- .getAsJsonObject (META ).has (ID )) {
206
- return json .getAsJsonObject ().getAsJsonObject (META )
207
- .get (ID ).getAsString ();
207
+ if (json .isJsonObject () && json .getAsJsonObject ().has (META )
208
+ && json .getAsJsonObject ().getAsJsonObject (META ).has (ID )) {
209
+ return json .getAsJsonObject ().getAsJsonObject (META ).get (ID ).getAsString ();
208
210
}
209
211
return null ;
210
212
}
211
213
212
214
/*
213
- * json Input json which we have passed from CLI or Service
214
- * return eventType will return from inputJson file
215
+ * json Input json which we have passed from CLI or Service return eventType
216
+ * will return from inputJson file
215
217
*/
216
218
public String getInputEventType (JsonObject json ) {
217
- if (json .isJsonObject () && json .get (MSG_PARAMS ).getAsJsonObject ().has (META ) && json .get (MSG_PARAMS ).getAsJsonObject ().getAsJsonObject (META ).has (TYPE )){
219
+ if (json .isJsonObject () && json .get (MSG_PARAMS ).getAsJsonObject ().has (META )
220
+ && json .get (MSG_PARAMS ).getAsJsonObject ().getAsJsonObject (META ).has (TYPE )) {
218
221
return json .get (MSG_PARAMS ).getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ();
219
222
}
220
223
return null ;
0 commit comments