b6b8bc34d1a5bc19e7f40df979757abea71c38fe
[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
55     public static class TestResults {
56
57         public List<ProducerJobInfo> jobsStarted = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
58         public List<ProducerJobInfo> jobsStopped = Collections.synchronizedList(new ArrayList<ProducerJobInfo>());
59         public boolean errorFound = false;
60
61         public TestResults() {
62         }
63
64         public void reset() {
65             jobsStarted.clear();
66             jobsStopped.clear();
67             this.errorFound = false;
68         }
69     }
70
71     @Getter
72     private TestResults testResults = new TestResults();
73
74     @PostMapping(path = JOB_CREATED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
75     @ApiOperation(value = "Callback for EI job creation", notes = "")
76     @ApiResponses(
77         value = { //
78             @ApiResponse(code = 200, message = "OK", response = void.class)}//
79     )
80     public ResponseEntity<Object> jobCreatedCallback( //
81         @RequestBody ProducerJobInfo request) {
82         try {
83             this.testResults.jobsStarted.add(request);
84             logger.info("Job started callback {}", request.id);
85             if (request.id == null) {
86                 throw new NullPointerException("Illegal argument");
87             }
88             return new ResponseEntity<>(HttpStatus.OK);
89         } catch (Exception e) {
90             this.testResults.errorFound = true;
91             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
92         }
93     }
94
95     @PostMapping(path = JOB_DELETED_URL, produces = MediaType.APPLICATION_JSON_VALUE)
96     @ApiOperation(value = "Callback for EI job deletion", notes = "")
97     @ApiResponses(
98         value = { //
99             @ApiResponse(code = 200, message = "OK", response = void.class)}//
100     )
101     public ResponseEntity<Object> jobDeletedCallback( //
102         @RequestBody ProducerJobInfo request) {
103         try {
104             logger.info("Job deleted callback {}", request.id);
105             this.testResults.jobsStopped.add(request);
106             return new ResponseEntity<>(HttpStatus.OK);
107         } catch (Exception e) {
108             return ErrorResponse.create(e, HttpStatus.NOT_FOUND);
109         }
110     }
111
112 }