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.util.etag;
23 import static org.hamcrest.CoreMatchers.is;
24 import static org.hamcrest.CoreMatchers.not;
25 import static org.junit.Assert.assertThat;
26 import java.io.IOException;
27 import java.security.NoSuchAlgorithmException;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.crud.event.GraphEventEdge;
31 import org.onap.crud.event.GraphEventVertex;
32 import org.onap.crud.util.etag.EtagGenerator;
33 import com.google.gson.JsonObject;
35 public class EtagGeneratorTest {
37 private EtagGenerator etagGenerator;
40 public void init() throws NoSuchAlgorithmException {
41 etagGenerator = new EtagGenerator();
44 private GraphEventVertex createVertex(String propKey, String propValue) {
45 JsonObject properties = new JsonObject();
46 properties.addProperty(propKey, propValue);
47 GraphEventVertex vertex = new GraphEventVertex("vertex1", "v11", "pserver", properties);
51 private GraphEventEdge createEdge(String id, GraphEventVertex source, GraphEventVertex target, String propKey,
53 JsonObject properties = new JsonObject();
54 properties.addProperty(propKey, propValue);
55 GraphEventEdge edge = new GraphEventEdge(id, "v11", "tosca.relationships.HostedOn", source, target, properties);
60 public void computeHashForIdenticalVertexObjects() throws IOException {
62 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
63 GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
65 GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
67 GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
68 GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
70 GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "prop1", "value1");
72 assertThat(etagGenerator.computeHashForEdge(edge1), is(etagGenerator.computeHashForEdge(edge2)));
76 public void computeHashForVertexObjectsWithDifferentKey() throws IOException {
78 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
79 GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
81 GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
83 GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
84 GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
86 GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
88 assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
92 public void computeHashForVertexObjectsWithDifferentEdge() throws IOException {
93 // relationship is different
94 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
95 GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
97 GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
99 GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
100 GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
102 GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
103 edge2.setType("tosca.relationships.RelatedTo");
105 assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
109 public void computeHashForEdgeObjectsWithDifferentVertexObjects() throws IOException {
110 // source/target different
111 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
112 GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
113 targetVertex1.setId("vertex2");
115 GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
117 GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
118 GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
120 GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
121 edge2.setType("tosca.relationships.RelatedTo");
123 assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
127 public void computeHashForEdgeObjectsWithDifferentProperties() throws IOException {
128 // property different
129 GraphEventVertex sourceVertex1 = createVertex("sourceprop1", "value1");
130 GraphEventVertex targetVertex1 = createVertex("targetprop2", "value2");
132 GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "edgeprop1", "value1");
134 GraphEventVertex sourceVertex2 = createVertex("sourceprop1", "value1");
135 GraphEventVertex targetVertex2 = createVertex("targetprop2", "value2");
137 GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "edgeprop2", "value2");
139 assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
143 public void testComputeHashForIdenticalVertexObjects() throws IOException {
144 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
145 GraphEventVertex targetVertex1 = createVertex("prop1", "value1");
146 assertThat(etagGenerator.computeHashForVertex(sourceVertex1),
147 is(etagGenerator.computeHashForVertex(targetVertex1)));
151 public void testComputeHashForVertexObjectsWithDifferentProperties() throws IOException {
152 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
153 GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
154 assertThat(etagGenerator.computeHashForVertex(sourceVertex1),
155 not(etagGenerator.computeHashForVertex(targetVertex1)));
159 public void testComputeHashForChampObjectsWithDifferentKey() throws IOException {
160 GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
161 GraphEventVertex targetVertex1 = createVertex("prop1", "value1");
162 targetVertex1.setId("vertex2");
163 assertThat(etagGenerator.computeHashForVertex(sourceVertex1),
164 not(etagGenerator.computeHashForVertex(targetVertex1)));