c4651961d9bce2ea3ec0fb93aaf838df9f7a0b35
[nonrtric.git] / enrichment-coordinator-service / src / test / java / org / oransc / enrichment / controller / ProducerSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2020 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.controller;
22
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
27
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
32
33 import lombok.Getter;
34
35 import org.oransc.enrichment.clients.ProducerJobInfo;
36 import org.oransc.enrichment.controllers.ErrorResponse;
37 import org.oransc.enrichment.controllers.VoidResponse;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.http.HttpStatus;
41 import org.springframework.http.MediaType;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.web.bind.annotation.GetMapping;
44 import org.springframework.web.bind.annotation.PostMapping;
45 import org.springframework.web.bind.annotation.RequestBody;
46 import org.springframework.web.bind.annotation.RestController;
47
48 @RestController("ProducerSimulatorController")
49 @Api(tags = {"Producer Simulator"})
50 public class ProducerSimulatorController {
51
52     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
53
54     public static final String JOB_CREATED_URL = "/producer_simulator/job_created";
55     public static final String JOB_DELETED_URL = "/producer_simulator/job_deleted";
56     public static final String JOB_CREATED_ERROR_URL = "/producer_simulator/job_created_error";
57     public static final String JOB_DELETED_ERROR_URL = "/producer_simulator/job_deleted_error";
58
59     public static final String SUPERVISION_URL = "/producer_simulator/supervision";
60     public static final String SUPERVISION_ERROR_URL = "/producer_simulator/supervision_error";
61
62     public static class TestResults {
63
64         public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
65         public List<ProducerJobInfo> jobsStopped = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
66         public int noOfRejectedCreate = 0;
67         public int noOfRejectedDelete = 0;
68         public boolean errorFound = false;
69
70         public TestResults() {
71         }
72
73         public void reset() {
74             jobsStarted.clear();
75             jobsStopped.clear();
76             this.errorFound = false;
77             this.noOfRejectedCreate = 0;
78             this.noOfRejectedDelete = 0;
79         }
80     }
81
82     @Getter
83     private TestResults testResults = new TestResults();
84
85     @PostMapping(path = JOB_CREATED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
86     @ApiOperation(value = "Callback for EI job creation", notes = "")
87     @ApiResponses(
88         value = { //
89             @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
90     )
91     public ResponseEntity<Object> jobCreatedCallback( //
92         @RequestBody ProducerJobInfo request) {
93         try {
94             this.testResults.jobsStarted.add(request);
95             logger.info("Job started callback {}", request.id);
96             if (request.id == null) {
97                 throw new NullPointerException("Illegal argument");
98             }
99             return new ResponseEntity<>(HttpStatus.OK);
100         } catch (Exception e) {
101             this.testResults.errorFound = true;
102             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
103         }
104     }
105
106     @PostMapping(path = JOB_DELETED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
107     @ApiOperation(value = "Callback for EI job deletion", notes = "")
108     @ApiResponses(
109         value = { //
110             @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
111     )
112     public ResponseEntity<Object> jobDeletedCallback( //
113         @RequestBody ProducerJobInfo request) {
114         try {
115             logger.info("Job deleted callback {}", request.id);
116             this.testResults.jobsStopped.add(request);
117             return new ResponseEntity<>(HttpStatus.OK);
118         } catch (Exception e) {
119             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
120         }
121     }
122
123     @PostMapping(path = JOB_CREATED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
124     @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
125     @ApiResponses(
126         value = { //
127             @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
128     )
129     public ResponseEntity<Object> jobCreatedCallbackReturnError( //
130         @RequestBody ProducerJobInfo request) {
131         logger.info("Job created (returning error) callback {}", request.id);
132         this.testResults.noOfRejectedCreate += 1;
133         return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
134     }
135
136     @PostMapping(path = JOB_DELETED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
137     @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
138     @ApiResponses(
139         value = { //
140             @ApiResponse(code = 200, message = "OK", response = VoidResponse.class)}//
141     )
142     public ResponseEntity<Object> jobDeletedCallbackReturnError( //
143         @RequestBody ProducerJobInfo request) {
144         logger.info("Job created (returning error) callback {}", request.id);
145         this.testResults.noOfRejectedDelete += 1;
146         return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);
147     }
148
149     @GetMapping(path = SUPERVISION_URL, produces = MediaType.APPLICATION_JSON_VALUE)
150     @ApiOperation(value = "Producer supervision", notes = "")
151     @ApiResponses(
152         value = { //
153             @ApiResponse(code = 200, message = "OK", response = String.class)}//
154     )
155     public ResponseEntity<Object> producerSupervision() {
156         logger.info("Producer supervision");
157         return new ResponseEntity<>(HttpStatus.OK);
158     }
159
160     @GetMapping(path = SUPERVISION_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
161     @ApiOperation(value = "Producer supervision error", notes = "")
162     @ApiResponses(
163         value = { //
164             @ApiResponse(code = 200, message = "OK", response = String.class)}//
165     )
166     public ResponseEntity<Object> producerSupervisionError() {
167         logger.info("Producer supervision error");
168         return new ResponseEntity<>(HttpStatus.NOT_FOUND);
169     }
170
171 }