ETags on resources
[aai/gizmo.git] / src / test / java / org / onap / crud / util / etag / EtagGeneratorTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.crud.util.etag;
22
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;
34
35 public class EtagGeneratorTest {
36
37     private EtagGenerator etagGenerator;
38
39     @Before
40     public void init() throws NoSuchAlgorithmException {
41         etagGenerator = new EtagGenerator();
42     }
43
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);
48         return vertex;
49     }
50
51     private GraphEventEdge createEdge(String id, GraphEventVertex source, GraphEventVertex target, String propKey,
52             String propValue) {
53         JsonObject properties = new JsonObject();
54         properties.addProperty(propKey, propValue);
55         GraphEventEdge edge = new GraphEventEdge(id, "v11", "tosca.relationships.HostedOn", source, target, properties);
56         return edge;
57     }
58
59     @Test
60     public void computeHashForIdenticalVertexObjects() throws IOException {
61         // everything is same
62         GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
63         GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
64
65         GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
66
67         GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
68         GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
69
70         GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "prop1", "value1");
71
72         assertThat(etagGenerator.computeHashForEdge(edge1), is(etagGenerator.computeHashForEdge(edge2)));
73     }
74
75     @Test
76     public void computeHashForVertexObjectsWithDifferentKey() throws IOException {
77         // key is different
78         GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
79         GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
80
81         GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
82
83         GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
84         GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
85
86         GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
87
88         assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
89     }
90
91     @Test
92     public void computeHashForVertexObjectsWithDifferentEdge() throws IOException {
93         // relationship is different
94         GraphEventVertex sourceVertex1 = createVertex("prop1", "value1");
95         GraphEventVertex targetVertex1 = createVertex("prop2", "value2");
96
97         GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
98
99         GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
100         GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
101
102         GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
103         edge2.setType("tosca.relationships.RelatedTo");
104
105         assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
106     }
107
108     @Test
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");
114
115         GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1");
116
117         GraphEventVertex sourceVertex2 = createVertex("prop1", "value1");
118         GraphEventVertex targetVertex2 = createVertex("prop2", "value2");
119
120         GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1");
121         edge2.setType("tosca.relationships.RelatedTo");
122
123         assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
124     }
125
126     @Test
127     public void computeHashForEdgeObjectsWithDifferentProperties() throws IOException {
128         // property different
129         GraphEventVertex sourceVertex1 = createVertex("sourceprop1", "value1");
130         GraphEventVertex targetVertex1 = createVertex("targetprop2", "value2");
131
132         GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "edgeprop1", "value1");
133
134         GraphEventVertex sourceVertex2 = createVertex("sourceprop1", "value1");
135         GraphEventVertex targetVertex2 = createVertex("targetprop2", "value2");
136
137         GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "edgeprop2", "value2");
138
139         assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2)));
140     }
141
142     @Test
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)));
148     }
149
150     @Test
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)));
156     }
157
158     @Test
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)));
165     }
166
167
168 }