diff options
author | 2018-06-15 15:32:01 +0100 | |
---|---|---|
committer | 2018-06-18 11:33:18 +0100 | |
commit | b348af8ed2c4192f88169b37bf53fa25b8a7a681 (patch) | |
tree | 3ce66feb7909f645536a8d0eb5ad2a7879d3ab6c /src/test/java/org | |
parent | 00832f054dd0c21492af531548e321ea25cdb8b4 (diff) |
ETags on resources
An etag should be generated (by Champ) when creating
a resource (edge or vertex). The Champ microservice should return
this etag in the response header. Gizmo should also return the etag
in it's response header (Gizmo will receive the etag from Champ).
Issue-ID: AAI-1196
Change-Id: Ie16f871eccbceeccde037e73e0de0d96eeba18bd
Signed-off-by: Sotiropoulos, Ioannis (is948x) <Ioannis.Sotiropoulos@amdocs.com>
Diffstat (limited to 'src/test/java/org')
-rw-r--r-- | src/test/java/org/onap/crud/service/CrudRestServiceTest.java | 45 | ||||
-rw-r--r-- | src/test/java/org/onap/crud/service/TestDao.java (renamed from src/test/java/org/onap/crud/dao/TestDao.java) | 101 | ||||
-rw-r--r-- | src/test/java/org/onap/crud/util/etag/EtagGeneratorTest.java | 168 |
3 files changed, 266 insertions, 48 deletions
diff --git a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java index 13cba11..68c876c 100644 --- a/src/test/java/org/onap/crud/service/CrudRestServiceTest.java +++ b/src/test/java/org/onap/crud/service/CrudRestServiceTest.java @@ -26,8 +26,6 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.nio.file.Path; -import java.nio.file.Paths; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; @@ -37,8 +35,8 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.mockito.Mockito; -import org.onap.crud.dao.TestDao; import org.onap.crud.exception.CrudException; +import org.onap.crud.service.TestDao; import org.onap.crud.service.util.TestHeaders; import org.onap.crud.service.util.TestRequest; import org.onap.crud.service.util.TestUriInfo; @@ -80,18 +78,18 @@ public class CrudRestServiceTest { @Before public void init() throws Exception { - Path resourcePath = Paths.get(ClassLoader.getSystemResource("model").toURI()); - Path parentPath = resourcePath.getParent(); + ClassLoader classLoader = getClass().getClassLoader(); + File dir = new File(classLoader.getResource("model").getFile()); + System.setProperty("CONFIG_HOME", dir.getParent()); + EdgeRulesLoader.resetSchemaVersionContext(); - System.setProperty("CONFIG_HOME", parentPath.toString()); - EdgeRulesLoader.resetSchemaVersionContext (); - CrudGraphDataService service = new CrudGraphDataService(new TestDao()); - CrudRestService restService = new CrudRestService(service, null); - mockService = Mockito.spy(restService); - - Mockito.doReturn(true).when(mockService).validateRequest(Mockito.any(HttpServletRequest.class), - Mockito.anyString(), Mockito.anyString(), Mockito.any(CrudRestService.Action.class), Mockito.anyString(), - Mockito.any(HttpHeaders.class)); + CrudGraphDataService service = new CrudGraphDataService(new TestDao()); + CrudRestService restService = new CrudRestService(service, null); + mockService = Mockito.spy(restService); + + Mockito.doReturn(true).when(mockService).validateRequest(Mockito.any(HttpServletRequest.class), + Mockito.anyString(), Mockito.anyString(), Mockito.any(CrudRestService.Action.class), Mockito.anyString(), + Mockito.any(HttpHeaders.class)); } @Test @@ -123,21 +121,25 @@ public class CrudRestServiceTest { new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 400); + Assert.assertNull(response.getEntityTag()); response = mockService.addVertex(postVertexPayload, "v11", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 201); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); response = mockService.addVertex(postMissingPropVertexPayload, "v11", "pserver", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); - assertTrue(response.getStatus() == 400); + assertTrue(response.getStatus() == 400); + Assert.assertNull(response.getEntityTag()); response = mockService.addVertex(postVertexPayload, "v11", "pserver", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); - assertTrue(response.getStatus() == 201); + assertTrue(response.getStatus() == 201); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); } @Test @@ -148,11 +150,13 @@ public class CrudRestServiceTest { new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 201); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); response = mockService.addEdge(postEdgePayload, "v11", "tosca.relationships.HostedOn", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); - assertTrue(response.getStatus() == 201); + assertTrue(response.getStatus() == 201); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); } @Test @@ -170,19 +174,22 @@ public class CrudRestServiceTest { response = mockService.updateVertex(putVertexPayload, "v11", "pserver", "bad-id", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); - assertTrue(response.getStatus() == 400); + assertTrue(response.getStatus() == 400); + Assert.assertNull(response.getEntityTag()); // Success case response = mockService.updateVertex(putVertexPayload, "v11", "pserver", "test-uuid", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 200); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); // Patch response = mockService.patchVertex(putVertexPayload, "v11", "pserver", "test-uuid", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 200); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); } @Test @@ -193,12 +200,14 @@ public class CrudRestServiceTest { "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 200); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); // Patch response = mockService.patchEdge(postEdgePayload, "v11", "tosca.relationships.HostedOn", "my-uuid", "services/inventory/v11", new TestHeaders(), null, new TestRequest()); System.out.println("Response: " + response.getStatus() + "\n" + response.getEntity().toString()); assertTrue(response.getStatus() == 200); + Assert.assertEquals(response.getEntityTag().getValue(), "test123"); } @Test diff --git a/src/test/java/org/onap/crud/dao/TestDao.java b/src/test/java/org/onap/crud/service/TestDao.java index 69ce4a3..d412163 100644 --- a/src/test/java/org/onap/crud/dao/TestDao.java +++ b/src/test/java/org/onap/crud/service/TestDao.java @@ -18,27 +18,36 @@ * limitations under the License. * ============LICENSE_END========================================================= */ -package org.onap.crud.dao; +package org.onap.crud.service; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Map; - +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import org.onap.aai.restclient.client.OperationResult; import org.onap.crud.dao.GraphDao; import org.onap.crud.entity.Edge; import org.onap.crud.entity.Vertex; import org.onap.crud.exception.CrudException; public class TestDao implements GraphDao { - + private final String champVertex = "{" + "\"key\": \"test-uuid\"," + "\"type\": \"pserver\"," + "\"properties\": {" + "\"fqdn\": \"myhost.onap.com\"," + "\"hostname\": \"myhost\" } }"; - + + private final String champVertices = "[ {" + + "\"key\": \"test-uuid\"," + + "\"type\": \"pserver\"," + + "\"properties\": {" + + "\"fqdn\": \"myhost.onap.com\"," + + "\"hostname\": \"myhost\" } } ]"; + private final String champEdge = "{" + "\"key\": \"test-uuid\"," + "\"type\": \"tosca.relationships.HostedOn\"," + @@ -50,15 +59,28 @@ public class TestDao implements GraphDao { "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" + " }"; + private final String champEdges = "[ {" + + "\"key\": \"test-uuid\"," + + "\"type\": \"tosca.relationships.HostedOn\"," + + "\"properties\": {" + + "\"prevent-delete\": \"NONE\" }," + + "\"source\": {" + + "\"key\": \"50bdab41-ad1c-4d00-952c-a0aa5d827811\", \"type\": \"vserver\"}," + + "\"target\": {" + + "\"key\": \"1d326bc7-b985-492b-9604-0d5d1f06f908\", \"type\": \"pserver\"}" + + " } ]"; + @Override public Vertex getVertex(String id, String version) throws CrudException { return Vertex.fromJson(champVertex, "v11"); } @Override - public Vertex getVertex(String id, String type, String version, Map<String, String> queryParams) + public OperationResult getVertex(String id, String type, String version, Map<String, String> queryParams) throws CrudException { - return Vertex.fromJson(champVertex, "v11"); + OperationResult operationResult = new OperationResult(); + operationResult.setResult(champVertex); + return operationResult; } @Override @@ -69,41 +91,49 @@ public class TestDao implements GraphDao { } @Override - public List<Vertex> getVertices(String type, Map<String, Object> filter, String version) throws CrudException { - List<Vertex> list = new ArrayList<Vertex>(); - list.add(Vertex.fromJson(champVertex, "v11")); - return list; + public OperationResult getVertices(String type, Map<String, Object> filter, String version) throws CrudException { + OperationResult operationResult = new OperationResult(); + operationResult.setResult(champVertices); + return operationResult; } @Override - public List<Vertex> getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) + public OperationResult getVertices(String type, Map<String, Object> filter, HashSet<String> properties, String version) throws CrudException { - List<Vertex> list = new ArrayList<Vertex>(); - list.add(Vertex.fromJson(champVertex, "v11")); - return list; + OperationResult operationResult = new OperationResult(); + operationResult.setResult(champVertices); + return operationResult; } @Override - public Edge getEdge(String id, String type, Map<String, String> queryParams) throws CrudException { - return Edge.fromJson(champEdge); + public OperationResult getEdge(String id, String type, Map<String, String> queryParams) throws CrudException { + OperationResult operationResult = new OperationResult(); + operationResult.setResult(champEdge); + return operationResult; } @Override - public List<Edge> getEdges(String type, Map<String, Object> filter) throws CrudException { - List<Edge> list = new ArrayList<Edge>(); - list.add(Edge.fromJson(champEdge)); - return list; + public OperationResult getEdges(String type, Map<String, Object> filter) throws CrudException { + OperationResult operationResult = new OperationResult(); + operationResult.setResult(champEdges); + return operationResult; } @Override - public Vertex addVertex(String type, Map<String, Object> properties, String version) throws CrudException { - return Vertex.fromJson(champVertex, "v11"); + public OperationResult addVertex(String type, Map<String, Object> properties, String version) throws CrudException { + OperationResult operationResult = new OperationResult(); + operationResult.setHeaders(addReponseHeader()); + operationResult.setResult(champVertex); + return operationResult; } @Override - public Vertex updateVertex(String id, String type, Map<String, Object> properties, String version) + public OperationResult updateVertex(String id, String type, Map<String, Object> properties, String version) throws CrudException { - return Vertex.fromJson(champVertex, "v11"); + OperationResult operationResult = new OperationResult(); + operationResult.setHeaders(addReponseHeader()); + operationResult.setResult(champVertex); + return operationResult; } @Override @@ -112,14 +142,20 @@ public class TestDao implements GraphDao { } @Override - public Edge addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) + public OperationResult addEdge(String type, Vertex source, Vertex target, Map<String, Object> properties, String version) throws CrudException { - return Edge.fromJson(champEdge); + OperationResult operationResult = new OperationResult(); + operationResult.setHeaders(addReponseHeader()); + operationResult.setResult(champEdge); + return operationResult; } @Override - public Edge updateEdge(Edge edge) throws CrudException { - return Edge.fromJson(champEdge); + public OperationResult updateEdge(Edge edge) throws CrudException { + OperationResult operationResult = new OperationResult(); + operationResult.setHeaders(addReponseHeader()); + operationResult.setResult(champEdge); + return operationResult; } @Override @@ -190,5 +226,10 @@ public class TestDao implements GraphDao { public Edge getEdge(String id, String type, String txId) throws CrudException { return Edge.fromJson(champEdge); } - -} + + private MultivaluedMap<String, String> addReponseHeader() { + MultivaluedMap<String, String> headers = new MultivaluedHashMap<String, String>(); + headers.add("etag", "test123"); + return headers; + } +}
\ No newline at end of file diff --git a/src/test/java/org/onap/crud/util/etag/EtagGeneratorTest.java b/src/test/java/org/onap/crud/util/etag/EtagGeneratorTest.java new file mode 100644 index 0000000..c4f7c38 --- /dev/null +++ b/src/test/java/org/onap/crud/util/etag/EtagGeneratorTest.java @@ -0,0 +1,168 @@ +/** + * ============LICENSE_START======================================================= + * org.onap.aai + * ================================================================================ + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ +package org.onap.crud.util.etag; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; +import java.io.IOException; +import java.security.NoSuchAlgorithmException; +import org.junit.Before; +import org.junit.Test; +import org.onap.crud.event.GraphEventEdge; +import org.onap.crud.event.GraphEventVertex; +import org.onap.crud.util.etag.EtagGenerator; +import com.google.gson.JsonObject; + +public class EtagGeneratorTest { + + private EtagGenerator etagGenerator; + + @Before + public void init() throws NoSuchAlgorithmException { + etagGenerator = new EtagGenerator(); + } + + private GraphEventVertex createVertex(String propKey, String propValue) { + JsonObject properties = new JsonObject(); + properties.addProperty(propKey, propValue); + GraphEventVertex vertex = new GraphEventVertex("vertex1", "v11", "pserver", properties); + return vertex; + } + + private GraphEventEdge createEdge(String id, GraphEventVertex source, GraphEventVertex target, String propKey, + String propValue) { + JsonObject properties = new JsonObject(); + properties.addProperty(propKey, propValue); + GraphEventEdge edge = new GraphEventEdge(id, "v11", "tosca.relationships.HostedOn", source, target, properties); + return edge; + } + + @Test + public void computeHashForIdenticalVertexObjects() throws IOException { + // everything is same + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop2", "value2"); + + GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1"); + + GraphEventVertex sourceVertex2 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex2 = createVertex("prop2", "value2"); + + GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "prop1", "value1"); + + assertThat(etagGenerator.computeHashForEdge(edge1), is(etagGenerator.computeHashForEdge(edge2))); + } + + @Test + public void computeHashForVertexObjectsWithDifferentKey() throws IOException { + // key is different + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop2", "value2"); + + GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1"); + + GraphEventVertex sourceVertex2 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex2 = createVertex("prop2", "value2"); + + GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1"); + + assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2))); + } + + @Test + public void computeHashForVertexObjectsWithDifferentEdge() throws IOException { + // relationship is different + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop2", "value2"); + + GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1"); + + GraphEventVertex sourceVertex2 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex2 = createVertex("prop2", "value2"); + + GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1"); + edge2.setType("tosca.relationships.RelatedTo"); + + assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2))); + } + + @Test + public void computeHashForEdgeObjectsWithDifferentVertexObjects() throws IOException { + // source/target different + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop2", "value2"); + targetVertex1.setId("vertex2"); + + GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "prop1", "value1"); + + GraphEventVertex sourceVertex2 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex2 = createVertex("prop2", "value2"); + + GraphEventEdge edge2 = createEdge("edge2", sourceVertex2, targetVertex2, "prop1", "value1"); + edge2.setType("tosca.relationships.RelatedTo"); + + assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2))); + } + + @Test + public void computeHashForEdgeObjectsWithDifferentProperties() throws IOException { + // property different + GraphEventVertex sourceVertex1 = createVertex("sourceprop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("targetprop2", "value2"); + + GraphEventEdge edge1 = createEdge("edge1", sourceVertex1, targetVertex1, "edgeprop1", "value1"); + + GraphEventVertex sourceVertex2 = createVertex("sourceprop1", "value1"); + GraphEventVertex targetVertex2 = createVertex("targetprop2", "value2"); + + GraphEventEdge edge2 = createEdge("edge1", sourceVertex2, targetVertex2, "edgeprop2", "value2"); + + assertThat(etagGenerator.computeHashForEdge(edge1), not(etagGenerator.computeHashForEdge(edge2))); + } + + @Test + public void testComputeHashForIdenticalVertexObjects() throws IOException { + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop1", "value1"); + assertThat(etagGenerator.computeHashForVertex(sourceVertex1), + is(etagGenerator.computeHashForVertex(targetVertex1))); + } + + @Test + public void testComputeHashForVertexObjectsWithDifferentProperties() throws IOException { + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop2", "value2"); + assertThat(etagGenerator.computeHashForVertex(sourceVertex1), + not(etagGenerator.computeHashForVertex(targetVertex1))); + } + + @Test + public void testComputeHashForChampObjectsWithDifferentKey() throws IOException { + GraphEventVertex sourceVertex1 = createVertex("prop1", "value1"); + GraphEventVertex targetVertex1 = createVertex("prop1", "value1"); + targetVertex1.setId("vertex2"); + assertThat(etagGenerator.computeHashForVertex(sourceVertex1), + not(etagGenerator.computeHashForVertex(targetVertex1))); + } + + +}
\ No newline at end of file |