2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.crud.event;
23 import static org.junit.Assert.*;
25 import java.io.BufferedReader;
27 import java.io.FileReader;
28 import java.io.IOException;
30 import javax.ws.rs.core.Response.Status;
32 import org.junit.Test;
33 import org.onap.crud.entity.Edge;
34 import org.onap.crud.entity.Vertex;
35 import org.onap.crud.event.GraphEvent;
36 import org.onap.crud.event.GraphEvent.GraphEventOperation;
37 import org.onap.crud.event.GraphEvent.GraphEventResult;
38 import org.onap.crud.event.GraphEventVertex;
39 import org.onap.crud.exception.CrudException;
41 public class GraphEventTest {
42 private final String vertexPayload = "{" +
43 "\"key\": \"test-uuid\"," +
44 "\"type\": \"pserver\"," +
46 "\"fqdn\": \"myhost.onap.com\"," +
47 "\"hostname\": \"myhost\" } }";
49 private final String edgePayload = "{" +
50 "\"key\": \"test-uuid\"," +
51 "\"type\": \"tosca.relationships.HostedOn\"," +
53 "\"prevent-delete\": \"NONE\" }," +
55 "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," +
57 "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" +
61 public void validateGraphEvent() throws CrudException, IOException {
62 // Test building event from json
63 File file = new File("src/test/resources/payloads/graphVertexEvent.json");
64 String payloadStr = readFileToString(file);
65 GraphEvent event = GraphEvent.fromJson(payloadStr);
66 assertTrue(event.getOperation() == GraphEventOperation.UPDATE);
67 assertTrue(event.getDbTransactionId().equals("b3e2853e-f643-47a3-a0c3-cb54cc997ad3"));
68 assertTrue(event.getTimestamp() == Long.parseLong("1514927928167"));
69 assertTrue(event.getTransactionId().equals("c0a81fa7-5ef4-49cd-ab39-e42c53c9b9a4"));
70 assertTrue(event.getObjectKey().equals("mykey"));
71 assertTrue(event.getObjectType().equals("vertex->pserver"));
72 assertTrue(event.getVertex().getId().equals("mykey"));
73 assertTrue(event.getVertex().getModelVersion().equals("v11"));
74 assertTrue(event.getVertex().getType().equals("pserver"));
75 assertTrue(event.getVertex().getProperties() != null);
76 assertTrue(event.getVertex().toVertex() != null);
77 assertTrue(event.getVertex().toJson() != null);
79 // Test building event from vertex
80 Vertex vertex = Vertex.fromJson(vertexPayload, "v11");
81 event = GraphEvent.builder(GraphEventOperation.CREATE).vertex(GraphEventVertex.fromVertex(vertex, "v11")).build();
82 assertTrue(event.getOperation() == GraphEventOperation.CREATE);
85 // Test building event from edge
86 Edge edge = Edge.fromJson(edgePayload);
87 event = GraphEvent.builder(GraphEventOperation.UPDATE).edge(GraphEventEdge.fromEdge(edge, "v11")).build();
88 assertTrue(event.getOperation() == GraphEventOperation.UPDATE);
89 assertTrue(event.getObjectKey().equals("test-uuid"));
90 assertTrue(event.getObjectType().equals("edge->tosca.relationships.HostedOn"));
91 assertTrue(event.getEdge().getId().equals("test-uuid"));
92 assertTrue(event.getEdge().getType().equals("tosca.relationships.HostedOn"));
93 assertTrue(event.getEdge().getProperties() != null);
94 assertTrue(event.getEdge().toEdge() != null);
95 assertTrue(event.getEdge().toJson() != null);
97 // Test Getters/Setters
98 event.setDbTransactionId("a");
99 assertTrue(event.getDbTransactionId().equals("a"));
100 event.setErrorMessage("error");
101 assertTrue(event.getErrorMessage().equals("error"));
102 event.setResult(GraphEventResult.FAILURE);
103 assertTrue(event.getResult() == GraphEventResult.FAILURE);
104 event.setHttpErrorStatus(Status.BAD_REQUEST);
105 assertTrue(event.getHttpErrorStatus() == Status.BAD_REQUEST);
106 event.setTimestamp(1234567);
107 assertTrue(event.getTimestamp() == Long.parseLong("1234567"));
110 public static String readFileToString(File aFile) throws IOException {
111 BufferedReader br = new BufferedReader(new FileReader(aFile));
113 StringBuilder sb = new StringBuilder();
114 String line = br.readLine();
116 while (line != null) {
118 line = br.readLine();
121 return sb.toString().replaceAll("\\s+", "");
125 } catch (IOException e) {
126 fail("Unexpected IOException: " + e.getMessage());