2 * ========================LICENSE_START=================================
4 * ======================================================================
5 * Copyright (C) 2019-2020 Nordix Foundation. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.enrichment.controller;
23 import io.swagger.annotations.Api;
24 import io.swagger.annotations.ApiOperation;
25 import io.swagger.annotations.ApiResponse;
26 import io.swagger.annotations.ApiResponses;
28 import java.lang.invoke.MethodHandles;
29 import java.util.ArrayList;
30 import java.util.Collections;
31 import java.util.List;
35 import org.oransc.enrichment.clients.ProducerJobInfo;
36 import org.oransc.enrichment.controllers.ErrorResponse;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.http.MediaType;
41 import org.springframework.http.ResponseEntity;
42 import org.springframework.web.bind.annotation.PostMapping;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RestController;
46 @RestController("ProducerSimulatorController")
47 @Api(tags = {"Producer Simulator"})
48 public class ProducerSimulatorController {
50 private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
52 public static final String JOB_CREATED_URL = "/producer_simulator/job_created";
53 public static final String JOB_DELETED_URL = "/producer_simulator/job_deleted";
54 public static final String JOB_CREATED_ERROR_URL = "/producer_simulator/job_created_error";
55 public static final String JOB_DELETED_ERROR_URL = "/producer_simulator/job_deleted_error";
57 public static class TestResults {
59 public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
60 public List<ProducerJobInfo> jobsStopped = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
61 public int noOfRejectedCreate = 0;
62 public int noOfRejectedDelete = 0;
63 public boolean errorFound = false;
65 public TestResults() {
71 this.errorFound = false;
72 this.noOfRejectedCreate = 0;
73 this.noOfRejectedDelete = 0;
78 private TestResults testResults = new TestResults();
80 @PostMapping(path = JOB_CREATED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
81 @ApiOperation(value = "Callback for EI job creation", notes = "")
84 @ApiResponse(code = 200, message = "OK", response = void.class)}//
86 public ResponseEntity<Object> jobCreatedCallback( //
87 @RequestBody ProducerJobInfo request) {
89 this.testResults.jobsStarted.add(request);
90 logger.info("Job started callback {}", request.id);
91 if (request.id == null) {
92 throw new NullPointerException("Illegal argument");
94 return new ResponseEntity<>(HttpStatus.OK);
95 } catch (Exception e) {
96 this.testResults.errorFound = true;
97 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
101 @PostMapping(path = JOB_DELETED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
102 @ApiOperation(value = "Callback for EI job deletion", notes = "")
105 @ApiResponse(code = 200, message = "OK", response = void.class)}//
107 public ResponseEntity<Object> jobDeletedCallback( //
108 @RequestBody ProducerJobInfo request) {
110 logger.info("Job deleted callback {}", request.id);
111 this.testResults.jobsStopped.add(request);
112 return new ResponseEntity<>(HttpStatus.OK);
113 } catch (Exception e) {
114 return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
118 @PostMapping(path = JOB_CREATED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
119 @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
122 @ApiResponse(code = 200, message = "OK", response = void.class)}//
124 public ResponseEntity<Object> jobCreatedCallbackReturnError( //
125 @RequestBody ProducerJobInfo request) {
126 logger.info("Job created (returning error) callback {}", request.id);
127 this.testResults.noOfRejectedCreate += 1;
128 return ErrorResponse.create("Producer returns error on create job", HttpStatus.NOT_FOUND);
131 @PostMapping(path = JOB_DELETED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
132 @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
135 @ApiResponse(code = 200, message = "OK", response = void.class)}//
137 public ResponseEntity<Object> jobDeletedCallbackReturnError( //
138 @RequestBody ProducerJobInfo request) {
139 logger.info("Job created (returning error) callback {}", request.id);
140 this.testResults.noOfRejectedDelete += 1;
141 return ErrorResponse.create("Producer returns error on delete job", HttpStatus.NOT_FOUND);