1414*/
1515package com .ericsson .eiffel .remrem .semantics ;
1616
17-
1817import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_CANCELED ;
1918import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_FINISHED ;
2019import static com .ericsson .eiffel .remrem .semantics .EiffelEventType .ACTIVITY_STARTED ;
8079import com .google .gson .JsonParser ;
8180
8281@ Named ("eiffel-semantics" )
83- public class SemanticsService implements MsgService {
82+ public class SemanticsService implements MsgService {
8483 private static final String ERROR = "error" ;
8584 private static final String SUPPORTED_EVENT_TYPES = "SUPPORTED_EVENT_TYPES" ;
8685 private static final String RESULT = "result" ;
@@ -97,16 +96,15 @@ public class SemanticsService implements MsgService{
9796 public static final Logger log = LoggerFactory .getLogger (SemanticsService .class );
9897
9998 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 () {
103102 for (final EiffelEventType msg : EiffelEventType .values ()) {
104103 supportedEventTypes .add (msg .getEventName ());
105104 }
106105 }
107-
108- public static Map <EiffelEventType , Class <? extends Event >> eventType ()
109- {
106+
107+ public static Map <EiffelEventType , Class <? extends Event >> eventType () {
110108 eventTypes = new HashMap <>();
111109 eventTypes .put (ARTIFACT_PUBLISHED , EiffelArtifactPublishedEvent .class );
112110 eventTypes .put (ACTIVITY_FINISHED , EiffelActivityFinishedEvent .class );
@@ -130,91 +128,96 @@ public static Map<EiffelEventType, Class<? extends Event>> eventType()
130128 eventTypes .put (TESTSUITE_STARTED , EiffelTestSuiteStartedEvent .class );
131129 eventTypes .put (ISSUE_VERIFIED , EiffelIssueVerifiedEvent .class );
132130 eventTypes .put (ARTIFACT_REUSED , EiffelArtifactReusedEvent .class );
133- return eventTypes ;
131+ return eventTypes ;
134132 }
135133
136134 @ Override
137- public String generateMsg (String msgType , JsonObject bodyJson ){
135+ public String generateMsg (String msgType , JsonObject bodyJson ) {
138136 EiffelEventType eiffelType = EiffelEventType .fromString (msgType );
139137 if (eiffelType == null ) {
140138 log .error ("Unknown message type requested: " + msgType );
141- return createErrorResponse (msgType ,supportedEventTypes );
139+ return createErrorResponse (msgType , supportedEventTypes );
142140 }
143141 Class <? extends Event > eventType = eventTypes .get (eiffelType );
144142
145143 JsonObject msgNodes = bodyJson .get (MSG_PARAMS ).getAsJsonObject ();
146144 JsonObject eventNodes = bodyJson .get (EVENT_PARAMS ).getAsJsonObject ();
147145
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.
150149 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 ()))) {
154154 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" );
156157 }
157158
158159 Event event = eventCreation (eventType , msgNodes , eventNodes );
159160
160- String result = gson .toJson (event );
161+ String result = gson .toJson (event );
161162 try {
162163 outputValidate (eiffelType , result );
163164 } 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 ());
165166 return createErrorResponse (e .getMessage (), e .getCause ().toString ());
166167 }
167168 return result ;
168169 }
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 ) {
171172 eventNodes .add ("meta" , msgNodes .get ("meta" ));
172173 Event event = createEvent (eventNodes , eventType );
173174 event .setMeta (event .generateMeta (event .getMeta ()));
174175 return event ;
175176 }
176-
177+
177178 private static Event createEvent (JsonObject eventNodes , Class <? extends Event > eventType ) {
178179 return gson .fromJson (eventNodes , eventType );
179180 }
180181
181- private String createErrorResponse (final String message , final String cause ){
182+ private String createErrorResponse (final String message , final String cause ) {
182183 JsonObject errorResponse = new JsonObject ();
183184 errorResponse .addProperty (MESSAGE , message );
184185 errorResponse .addProperty (CAUSE , cause .replace ("\n " , "" ));
185186 return errorResponse .toString ();
186187 }
187- private String createErrorResponse (final String message , final ArrayList <String > supportedEventTypes ){
188+
189+ private String createErrorResponse (final String message , final ArrayList <String > supportedEventTypes ) {
188190 JsonObject errorResponse = new JsonObject ();
189191 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 ());
192194 return errorResponse .toString ();
193195 }
196+
194197 private void outputValidate (EiffelEventType eiffelType , String jsonStringInput ) throws EiffelValidationException {
195198 EiffelValidator validator = EiffelOutputValidatorFactory .getEiffelValidator (eiffelType );
196199 JsonObject jsonObject = new JsonParser ().parse (jsonStringInput ).getAsJsonObject ();
197200 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.
199202 validator .customValidation (jsonObject );
200203 }
201204
202205 @ Override
203206 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 ();
208210 }
209211 return null ;
210212 }
211213
212214 /*
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
215217 */
216218 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 )) {
218221 return json .get (MSG_PARAMS ).getAsJsonObject ().getAsJsonObject (META ).get (TYPE ).getAsString ();
219222 }
220223 return null ;
0 commit comments