691592a4109bbe4e07de19e86ddd1282f1823c3b
[externalapi/nbi.git] / src / test / java / org / onap / nbi / api / listener / ListenerResourceTest.java
1 /**
2  * Copyright (c) 2019 Orange
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package org.onap.nbi.api.listener;
15
16 import java.net.URI;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Map;
20 import java.util.concurrent.ConcurrentHashMap;
21 import org.apache.commons.lang3.StringUtils;
22 import org.onap.nbi.commons.ResourceManagement;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.springframework.http.MediaType;
26 import org.springframework.http.ResponseEntity;
27 import org.springframework.util.MultiValueMap;
28 import org.springframework.web.bind.annotation.DeleteMapping;
29 import org.springframework.web.bind.annotation.GetMapping;
30 import org.springframework.web.bind.annotation.PathVariable;
31 import org.springframework.web.bind.annotation.PostMapping;
32 import org.springframework.web.bind.annotation.RequestBody;
33 import org.springframework.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RequestParam;
35 import org.springframework.web.bind.annotation.RestController;
36 import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
37 import com.fasterxml.jackson.databind.JsonNode;
38
39 @RestController
40 @RequestMapping("/test/listener")
41 public class ListenerResourceTest extends ResourceManagement {
42
43   Logger logger = LoggerFactory.getLogger(ListenerResourceTest.class);
44
45   static Map<String, JsonNode> events = new ConcurrentHashMap<>();
46
47   /*
48    * listener resource test for hub resource
49    */
50   @PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
51   public ResponseEntity<JsonNode> postListener(@RequestBody JsonNode event) {
52     if (logger.isDebugEnabled()) {
53       logger.debug("POST event from nbi : {}", event.toString());
54     }
55     String eventId = event.get("eventId").asText();
56     logger.info("putting eventId {} in the events map", eventId);
57     events.put(eventId, event);
58
59     URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
60         .buildAndExpand(eventId).toUri();
61
62     return ResponseEntity.created(location).body(event);
63   }
64
65
66   @GetMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
67   public ResponseEntity<Collection<JsonNode>> findEvents(
68       @RequestParam MultiValueMap<String, String> params) {
69     logger.info("called listener get with params {} : " + params.toString());
70     Collection<JsonNode> values = new ArrayList<>();
71     String serviceOrderId = params.getFirst("serviceOrderId");
72     String serviceInstanceId = params.getFirst("serviceInstanceId");
73     if (StringUtils.isNotEmpty(serviceOrderId)) {
74       for (JsonNode jsonNode : events.values()) {
75         String id = jsonNode.get("event").get("id").asText();
76         logger.info("found event with service order id : " + id);
77         if (id.equals(serviceOrderId)) {
78           values.add(jsonNode);
79         }
80       }
81       if (!values.isEmpty()) {
82         return ResponseEntity.ok(values);
83       } else {
84         logger.error("cannot found events with service order id : " + serviceOrderId);
85         return ResponseEntity.notFound().build();
86       }
87     } else if (StringUtils.isNotEmpty(serviceInstanceId)) {
88       for (JsonNode jsonNode : events.values()) {
89         String id = jsonNode.get("event").get("id").asText();
90         logger.info("found event with service Instance id : " + id);
91         if (id.equals(serviceInstanceId)) {
92           values.add(jsonNode);
93         }
94       }
95       if (!values.isEmpty()) {
96         return ResponseEntity.ok(values);
97       } else {
98         logger.error("cannot found events with service instance id : " + serviceInstanceId);
99         return ResponseEntity.notFound().build();
100       }
101     } else {
102       values = events.values();
103     }
104     return ResponseEntity.ok(values);
105   }
106
107   @GetMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
108   public ResponseEntity<Object> getEvent(@PathVariable String eventId) {
109
110     return ResponseEntity.ok(events.get(eventId));
111
112   }
113
114   @DeleteMapping(value = "/{eventId}", produces = MediaType.APPLICATION_JSON_VALUE)
115   public ResponseEntity<Object> deleteEvent(@PathVariable String eventId) {
116
117     events.remove(eventId);
118     return ResponseEntity.noContent().build();
119
120   }
121
122   @DeleteMapping(produces = MediaType.APPLICATION_JSON_VALUE)
123   public ResponseEntity<Object> deleteEvents() {
124
125     events.clear();
126     return ResponseEntity.noContent().build();
127
128   }
129
130 }