Add HubRessource Test
[externalapi/nbi.git] / src / main / java / org / onap / nbi / apis / hub / service / EventFactory.java
1 /**
2  *     Copyright (c) 2018 Orange
3  *
4  *     Licensed under the Apache License, Version 2.0 (the "License");
5  *     you may not use this file except in compliance with the License.
6  *     You may obtain a copy of the License at
7  *
8  *         http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *     Unless required by applicable law or agreed to in writing, software
11  *     distributed under the License is distributed on an "AS IS" BASIS,
12  *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *     See the License for the specific language governing permissions and
14  *     limitations under the License.
15  */
16 package org.onap.nbi.apis.hub.service;
17
18 import com.fasterxml.jackson.databind.JsonNode;
19 import com.fasterxml.jackson.databind.MappingJsonFactory;
20 import com.fasterxml.jackson.databind.ObjectMapper;
21 import com.fasterxml.jackson.databind.node.ObjectNode;
22 import org.onap.nbi.apis.hub.model.Event;
23 import org.onap.nbi.apis.hub.model.EventType;
24 import org.onap.nbi.apis.serviceorder.model.ServiceOrder;
25 import org.onap.nbi.apis.serviceorder.model.ServiceOrderItem;
26 import org.onap.nbi.commons.JacksonFilter;
27 import org.onap.nbi.commons.JsonRepresentation;
28
29 import java.util.Date;
30 import java.util.UUID;
31
32 public class EventFactory {
33
34     private static final ObjectMapper mapper = new ObjectMapper(new MappingJsonFactory());
35
36     public static Event getEvent(EventType eventType, ServiceOrder serviceOrder, ServiceOrderItem serviceOrderItem) {
37         Event event = new Event();
38         event.setEventId(UUID.randomUUID().toString());
39         event.setEventDate(new Date());
40         event.setEventType(eventType.value());
41
42         JsonNode serviceOrderJson = mapper.valueToTree(filterServiceOrder(serviceOrder));
43
44         if (EventType.SERVICE_ORDER_ITEM_STATE_CHANGE.equals(eventType)) {
45             JsonNode serviceOrderItemJson = mapper.valueToTree(serviceOrderItem);
46             ((ObjectNode)serviceOrderJson).putArray("orderItem").add(serviceOrderItemJson);
47         }
48
49         event.setEvent(serviceOrderJson);
50
51         return event;
52     }
53
54
55     /**
56      * Filter ServiceOrderObject to produce a lightweight object that fit the eventBody specification
57      * @param serviceOrder
58      * @return
59      */
60     private static Object filterServiceOrder(final ServiceOrder serviceOrder) {
61
62         Object filteredServiceOrder = null;
63
64         if (serviceOrder != null) {
65             JsonRepresentation jsonRepresentation = new JsonRepresentation();
66             jsonRepresentation.add("id").add("href").add("externalId").add("state").add("orderDate").add
67                 ("completionDateTime").add("orderItem");
68
69             filteredServiceOrder = JacksonFilter.createNode(serviceOrder, jsonRepresentation);
70         }
71
72         return filteredServiceOrder;
73     }
74 }