c44a9ee7423e2d4d1b6aa513922c371aeb90cfb7
[nonrtric.git] / enrichment-coordinator-service / src / test / java / org / oransc / enrichment / controller / ProducerSimulatorController.java
1 /*-
2  * ========================LICENSE_START=================================
3  * ONAP : ccsdk oran
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
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.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;
45
46 @RestController("ProducerSimulatorController")
47 @Api(tags = {"Producer Simulator"})
48 public class ProducerSimulatorController {
49
50     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
51
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";
56
57     public static class TestResults {
58
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;
64
65         public TestResults() {
66         }
67
68         public void reset() {
69             jobsStarted.clear();
70             jobsStopped.clear();
71             this.errorFound = false;
72             this.noOfRejectedCreate = 0;
73             this.noOfRejectedDelete = 0;
74         }
75     }
76
77     @Getter
78     private TestResults testResults = new TestResults();
79
80     @PostMapping(path = JOB_CREATED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
81     @ApiOperation(value = "Callback for EI job creation", notes = "")
82     @ApiResponses(
83         value = { //
84             @ApiResponse(code = 200, message = "OK", response = void.class)}//
85     )
86     public ResponseEntity<Object> jobCreatedCallback( //
87         @RequestBody ProducerJobInfo request) {
88         try {
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");
93             }
94             return new ResponseEntity<>(HttpStatus.OK);
95         } catch (Exception e) {
96             this.testResults.errorFound = true;
97             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
98         }
99     }
100
101     @PostMapping(path = JOB_DELETED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
102     @ApiOperation(value = "Callback for EI job deletion", notes = "")
103     @ApiResponses(
104         value = { //
105             @ApiResponse(code = 200, message = "OK", response = void.class)}//
106     )
107     public ResponseEntity<Object> jobDeletedCallback( //
108         @RequestBody ProducerJobInfo request) {
109         try {
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);
115         }
116     }
117
118     @PostMapping(path = JOB_CREATED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
119     @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
120     @ApiResponses(
121         value = { //
122             @ApiResponse(code = 200, message = "OK", response = void.class)}//
123     )
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);
129     }
130
131     @PostMapping(path = JOB_DELETED_ERROR_URL, produces = MediaType.APPLICATION_JSON_VALUE)
132     @ApiOperation(value = "Callback for EI job creation, returns error", notes = "")
133     @ApiResponses(
134         value = { //
135             @ApiResponse(code = 200, message = "OK", response = void.class)}//
136     )
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);
142     }
143
144 }