2
2
3
3
import java .io .IOException ;
4
4
5
+ import com .ericsson .ei .controller .model .RuleCheckBody ;
6
+ import com .ericsson .ei .jmespath .JmesPathInterface ;
7
+ import com .ericsson .ei .services .IRuleCheckService ;
8
+ import io .swagger .annotations .Api ;
9
+ import io .swagger .annotations .ApiOperation ;
10
+ import io .swagger .annotations .ApiParam ;
11
+ import org .json .JSONArray ;
5
12
import org .json .JSONException ;
6
13
import org .json .JSONObject ;
7
14
import org .slf4j .Logger ;
12
19
import org .springframework .http .ResponseEntity ;
13
20
import org .springframework .stereotype .Component ;
14
21
import org .springframework .web .bind .annotation .CrossOrigin ;
15
-
16
- import com .ericsson .ei .jmespath .JmesPathInterface ;
17
- import com .ericsson .ei .services .IRuleCheckService ;
18
-
19
- import io .swagger .annotations .Api ;
20
- import io .swagger .annotations .ApiOperation ;
22
+ import org .springframework .web .bind .annotation .RequestParam ;
23
+ import org .springframework .web .bind .annotation .RequestBody ;
21
24
22
25
/**
23
26
* This class implements the Interface for JMESPath API, generated by RAML 0.8
39
42
40
43
@ Component
41
44
@ CrossOrigin
42
- @ Api (value = "Check Rules" , description = "This rest call for the execute the rule or rules(Rule object ) on the Json, for checking output of rule " )
45
+ @ Api (value = "Check Rules" , description = "REST endpoints for executing rule(s ) on the JSON " )
43
46
public class RuleCheckControllerImpl implements RuleCheckController {
44
47
45
- private static final Logger LOG = LoggerFactory .getLogger (SubscriptionControllerImpl .class );
48
+ private static final Logger LOGGER = LoggerFactory .getLogger (SubscriptionControllerImpl .class );
49
+ private static final String BAD_REQUEST = "{\" message\" : \" Bad request\" }" ;
50
+ private static final String INVALID_JSON = "{\" message\" : \" Invalid JSON content\" }" ;
51
+ private static final String ENVIRONMENT_DISABLED = "{\" message\" : \" Test environment is not enabled. "
52
+ + "Please use the test environment for this execution\" }" ;
46
53
47
54
@ Autowired
48
- JmesPathInterface jmesPathInterface ;
55
+ private JmesPathInterface jmesPathInterface ;
49
56
50
57
@ Autowired
51
- IRuleCheckService ruleCheckService ;
58
+ private IRuleCheckService ruleCheckService ;
52
59
53
60
@ Value ("${testaggregated.enabled:false}" )
54
61
private Boolean testEnable ;
@@ -62,50 +69,45 @@ public class RuleCheckControllerImpl implements RuleCheckController {
62
69
* content
63
70
* @param jsonContent-
64
71
* takes JSON object as a String
65
- * @return return a String object
72
+ * @return a String object
66
73
*
67
74
*/
68
75
@ Override
69
76
@ CrossOrigin
70
- @ ApiOperation (value = "run rule on event" )
71
- public ResponseEntity <?> updateRulesRuleCheck (String rule , String jsonContent ) {
72
- String res = new String ("[]" );
73
-
77
+ @ ApiOperation (value = "To execute rule on JSON" , response = String .class )
78
+ public ResponseEntity <?> updateRulesRuleCheck (@ ApiParam (value = "JMESPath rule" , required = true ) @ RequestParam String rule ,
79
+ @ ApiParam (value = "JSON object" , required = true ) @ RequestBody String jsonContent ) {
74
80
try {
75
81
JSONObject jsonObj = new JSONObject (jsonContent );
76
-
77
- String jsonString = jsonObj .toString ();
78
- res = jmesPathInterface .runRuleOnEvent (rule , jsonString ).toString ();
79
- LOG .debug ("Query :" + rule + " executed Successfully" );
80
- return new ResponseEntity <String >(res , HttpStatus .OK );
81
-
82
+ String res = jmesPathInterface .runRuleOnEvent (rule , jsonObj .toString ()).toString ();
83
+ LOGGER .debug ("Query: " + rule + " executed successfully" );
84
+ return new ResponseEntity <>(res , HttpStatus .OK );
82
85
} catch (Exception e ) {
83
- LOG .error (e .getMessage (), e );
84
- return new ResponseEntity <String >( res , HttpStatus .BAD_REQUEST );
86
+ LOGGER .error (e .getMessage (), e );
87
+ return new ResponseEntity <>( BAD_REQUEST , HttpStatus .BAD_REQUEST );
85
88
}
86
89
}
87
90
88
91
@ Override
89
92
@ CrossOrigin
90
- @ ApiOperation (value = "Run the list of rules on list of events and prepare the aggregation object. This endpoint for executing the rules on list of objects and return the aggregated objects." )
91
- public ResponseEntity <?> updateAggregation (String listRulesJson , String listEventsJson ) {
92
-
93
+ @ ApiOperation (value = "To execute the list of rules on list of Eiffel events. Return the aggregated object(s)" , response = String . class )
94
+ public ResponseEntity <?> updateAggregation (@ ApiParam ( value = "Object that include list of rules and list of Eiffel events" , required = true )
95
+ @ RequestBody RuleCheckBody body ) {
93
96
if (testEnable ) {
94
97
try {
95
- String aggeObject = ruleCheckService .prepareAggregatedObject (listRulesJson , listEventsJson );
96
- if (aggeObject != null ) {
97
- return new ResponseEntity <String >( aggeObject , HttpStatus .OK );
98
+ String aggregatedObject = ruleCheckService .prepareAggregatedObject (new JSONArray ( body . getListRulesJson ()), new JSONArray ( body . getListEventsJson ()) );
99
+ if (aggregatedObject != null ) {
100
+ return new ResponseEntity <>( aggregatedObject , HttpStatus .OK );
98
101
} else {
99
- return new ResponseEntity <String >( "invalid json content" , HttpStatus .BAD_REQUEST );
102
+ return new ResponseEntity <>( INVALID_JSON , HttpStatus .BAD_REQUEST );
100
103
}
101
-
102
104
} catch (JSONException | IOException e ) {
103
- LOG .error (e .getMessage (), e );
104
- return new ResponseEntity <String >( "invalid json content" , HttpStatus .BAD_REQUEST );
105
+ LOGGER .error (e .getMessage (), e );
106
+ return new ResponseEntity <>( INVALID_JSON , HttpStatus .BAD_REQUEST );
105
107
}
106
108
} else {
107
- LOG .debug ("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events" );
108
- return new ResponseEntity <String >( "Please use the test environment for this execution" , HttpStatus .BAD_REQUEST );
109
+ LOGGER .debug ("testaggregated.controller.enabled is not enabled in application.properties file, Unable to test the rules on list of events" );
110
+ return new ResponseEntity <>( ENVIRONMENT_DISABLED , HttpStatus .SERVICE_UNAVAILABLE );
109
111
}
110
112
}
111
113
0 commit comments