diff options
Diffstat (limited to 'datarouter-prov/src/test/java')
4 files changed, 307 insertions, 51 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryRecordTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryRecordTest.java new file mode 100644 index 00000000..3329b732 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/DeliveryRecordTest.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * 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==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; + +import java.text.ParseException; + +public class DeliveryRecordTest { + + private DeliveryRecord deliveryRecord; + + @Test + public void Validate_Constructor_Creates_Object_With_Get_Methods() throws ParseException { + String[] args = {"2018-08-29-10-10-10-543.", "del", "238465493.fileName", "1","285", "123/file.txt","GET","application/json","2000","example","100"}; + deliveryRecord = new DeliveryRecord(args); + Assert.assertEquals("238465493.fileName", deliveryRecord.getPublishId()); + Assert.assertEquals(1, deliveryRecord.getFeedid()); + Assert.assertEquals(285, deliveryRecord.getSubid()); + Assert.assertEquals("123/file.txt", deliveryRecord.getRequestUri()); + Assert.assertEquals("GET", deliveryRecord.getMethod()); + Assert.assertEquals("file.txt", deliveryRecord.getFileid()); + Assert.assertEquals("application/json", deliveryRecord.getContentType()); + Assert.assertEquals(2000, deliveryRecord.getContentLength()); + Assert.assertEquals("example", deliveryRecord.getUser()); + Assert.assertEquals(100, deliveryRecord.getResult()); + } + + @Test + public void Validate_AsJsonObject_Correct_Json_Object_After_Set_Methods() throws ParseException { + String[] args = {"2018-08-29-10-10-10-543.", "del", "238465493.fileName", "1","285", "123/file.txt","GET","application/json","2000","example","100"}; + deliveryRecord = new DeliveryRecord(args); + deliveryRecord.setContentLength(265); + deliveryRecord.setEventTime(1535533810543L); + deliveryRecord.setPublishId("2345657324.fileName"); + deliveryRecord.setSubid(287); + deliveryRecord.setFeedid(2); + deliveryRecord.setRequestUri("/delete/2"); + deliveryRecord.setMethod("PUT"); + deliveryRecord.setContentType("application/json"); + deliveryRecord.setFileid("file2.txt"); + deliveryRecord.setUser("example2"); + deliveryRecord.setResult(300); + LOGJSONObject deliveryRecordJson = createBaseLogRecordJson(); + String deliveryRecordString = stripBracketFromJson(deliveryRecordJson); + String deliveryRecordStringObject = stripBracketFromJson(deliveryRecord.asJSONObject()); + Assert.assertTrue(deliveryRecordStringObject.matches(deliveryRecordString)); + } + + private LOGJSONObject createBaseLogRecordJson() { + LOGJSONObject deliveryRecordJson = new LOGJSONObject(); + deliveryRecordJson.put("statusCode", 300); + deliveryRecordJson.put("deliveryId", "example2"); + deliveryRecordJson.put("publishId", "2345657324.fileName"); + deliveryRecordJson.put("requestURI", "/delete/2"); + deliveryRecordJson.put("method", "PUT"); + deliveryRecordJson.put("contentType", "application/json"); + deliveryRecordJson.put("type", "del"); + deliveryRecordJson.put("date", "2018-08-29T[0-1][0-9]:10:10.543Z"); + deliveryRecordJson.put("contentLength", 265); + + return deliveryRecordJson; + } + + private String stripBracketFromJson(LOGJSONObject deliveryRecordJson) { + String deliveryRecordString = deliveryRecordJson.toString(); + deliveryRecordString = deliveryRecordString.substring(1, deliveryRecordString.length() - 1); + return deliveryRecordString; + } +} diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/ExpiryRecordTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/ExpiryRecordTest.java new file mode 100644 index 00000000..34a6d979 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/ExpiryRecordTest.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * 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==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import org.junit.Assert; +import org.junit.Test; +import org.onap.dmaap.datarouter.provisioning.utils.LOGJSONObject; + +import java.text.ParseException; + +public class ExpiryRecordTest { + + + private ExpiryRecord expiryRecord; + + @Test + public void Validate_Constructor_Creates_Object_With_Get_Methods() throws ParseException { + String[] args = {"2018-08-29-10-10-10-543.", "EXP", "238465493.fileName", "1","285", "123/file.txt","GET","","2000","example","100"}; + expiryRecord = new ExpiryRecord(args); + Assert.assertEquals("238465493.fileName", expiryRecord.getPublishId()); + Assert.assertEquals(1, expiryRecord.getFeedid()); + Assert.assertEquals("123/file.txt", expiryRecord.getRequestUri()); + Assert.assertEquals("GET", expiryRecord.getMethod()); + Assert.assertEquals("", expiryRecord.getContentType()); + Assert.assertEquals(2000, expiryRecord.getContentLength()); + Assert.assertEquals(285, expiryRecord.getSubid()); + Assert.assertEquals("file.txt", expiryRecord.getFileid()); + Assert.assertEquals(100, expiryRecord.getAttempts()); + Assert.assertEquals("other", expiryRecord.getReason()); + } + + @Test + public void Validate_AsJsonObject_Correct_Json_Object_After_Set_Methods() throws ParseException { + String[] args = {"2018-08-29-10-10-10-543.", "EXP", "238465493.fileName", "1","285", "123/file.txt","GET","","2000","example","100"}; + expiryRecord = new ExpiryRecord(args); + expiryRecord.setContentLength(265); + expiryRecord.setEventTime(1535533810543L); + expiryRecord.setPublishId("2345657324.fileName"); + expiryRecord.setFeedid(2); + expiryRecord.setRequestUri("/delete/2"); + expiryRecord.setContentType("application/json"); + expiryRecord.setMethod("PUT"); + expiryRecord.setSubid(322); + expiryRecord.setFileid("file.txt"); + expiryRecord.setAttempts(125); + expiryRecord.setReason("Out of memory"); + + LOGJSONObject expiryRecordJson = createBaseLogRecordJson(); + String expiryRecordString = stripBracketFromJson(expiryRecordJson); + String expiryRecordObject = stripBracketFromJson(expiryRecord.asJSONObject()); + Assert.assertTrue(expiryRecordObject.matches(expiryRecordString)); + } + + private LOGJSONObject createBaseLogRecordJson() { + LOGJSONObject expiryRecordJson = new LOGJSONObject(); + expiryRecordJson.put("expiryReason", "Out of memory"); + expiryRecordJson.put("publishId", "2345657324.fileName"); + expiryRecordJson.put("attempts", 125); + expiryRecordJson.put("requestURI", "/delete/2"); + expiryRecordJson.put("method", "PUT"); + expiryRecordJson.put("contentType", "application/json"); + expiryRecordJson.put("type", "exp"); + expiryRecordJson.put("date", "2018-08-29T[0-1][0-9]:10:10.543Z"); + expiryRecordJson.put("contentLength", 265); + return expiryRecordJson; + } + + private String stripBracketFromJson(LOGJSONObject expiryRecordJson) { + String expiryRecordString = expiryRecordJson.toString(); + expiryRecordString = expiryRecordString.substring(1, expiryRecordString.length() - 1); + return expiryRecordString; + } +} + diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java index 098765cf..91d72af7 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/GroupTest.java @@ -22,62 +22,80 @@ ******************************************************************************/ package org.onap.dmaap.datarouter.provisioning.beans; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; -import org.powermock.modules.junit4.PowerMockRunner; +import org.junit.*; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import java.util.Collection; import java.util.Date; +import java.util.List; - -@RunWith(PowerMockRunner.class) -@SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.provisioning.beans.Group"}) public class GroupTest { - private Group group; + private static EntityManagerFactory emf; + private static EntityManager em; + private Group group; + private DB db; + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("dr-unit-tests"); + em = emf.createEntityManager(); + System.setProperty( + "org.onap.dmaap.datarouter.provserver.properties", + "src/test/resources/h2Database.properties"); + } + + @AfterClass + public static void tearDownClass() { + em.clear(); + em.close(); + emf.close(); + } + + @Before + public void setUp() throws Exception { + db = new DB(); + group = new Group("GroupTest", "", ""); + group.doInsert(db.getConnection()); + } + + @Test + public void Given_Group_Exists_In_Db_GetAllGroups_Returns_Correct_Group() { + Collection<Group> groups = Group.getAllgroups(); + Assert.assertEquals("Group1", ((List<Group>) groups).get(0).getName()); + } + + @Test + public void Given_Group_Inserted_Into_Db_GetGroupMatching_Returns_Created_Group() { + Assert.assertEquals(group, Group.getGroupMatching(group)); + } + + @Test + public void Given_Group_Inserted_With_Same_Name_GetGroupMatching_With_Id_Returns_Correct_Group() + throws Exception { + Group sameGroupName = new Group("GroupTest", "This group has a description", ""); + sameGroupName.doInsert(db.getConnection()); + Assert.assertEquals( + "This group has a description", Group.getGroupMatching(group, 2).getDescription()); + sameGroupName.doDelete(db.getConnection()); + } - @Test - public void Validate_Group_Created_With_Default_Contructor() { - group = new Group(); - Assert.assertEquals(group.getGroupid(), -1); - Assert.assertEquals(group.getName(), ""); - } + @Test + public void Given_Group_Inserted_GetGroupById_Returns_Correct_Group() { + Assert.assertEquals(group, Group.getGroupById(group.getGroupid())); + } - @Test - public void Validate_Getters_And_Setters() { - group = new Group(); - group.setGroupid(1); - group.setAuthid("Auth"); - group.setClassification("Class"); - group.setDescription("Description"); - Date date = new Date(); - group.setLast_mod(date); - group.setMembers("Members"); - group.setName("NewName"); - Assert.assertEquals(1, group.getGroupid()); - Assert.assertEquals("Auth", group.getAuthid()); - Assert.assertEquals("Class", group.getClassification()); - Assert.assertEquals("Description", group.getDescription()); - Assert.assertEquals(date, group.getLast_mod()); - Assert.assertEquals("Members", group.getMembers()); - } + @Test + public void Given_Group_AuthId_Updated_GetGroupByAuthId_Returns_Correct_Group() throws Exception { + group.setAuthid("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9"); + group.doUpdate(db.getConnection()); + Assert.assertEquals(group, Group.getGroupByAuthId("Basic TmFtZTp6Z04wMFkyS3gybFppbXltNy94ZDhuMkdEYjA9")); + } - @Test - public void Validate_Equals() { - group = new Group(); - group.setGroupid(1); - group.setAuthid("Auth"); - group.setClassification("Class"); - group.setDescription("Description"); - Date date = new Date(); - group.setLast_mod(date); - group.setMembers("Members"); - group.setName("NewName"); - Group group2 = new Group("NewName", "Description", "Members"); - group2.setGroupid(1); - group2.setAuthid("Auth"); - group2.setClassification("Class"); - group2.setLast_mod(date); - Assert.assertEquals(group, group2); - } + @After + public void tearDown() throws Exception { + group.doDelete(db.getConnection()); + } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java new file mode 100644 index 00000000..dd798ab6 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/PubFailRecordTest.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * ============LICENSE_START================================================== + * * org.onap.dmaap + * * =========================================================================== + * * Copyright © 2017 AT&T Intellectual Property. All rights reserved. + * * =========================================================================== + * * 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==================================================== + * * + * * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * * + ******************************************************************************/ +package org.onap.dmaap.datarouter.provisioning.beans; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +import java.text.ParseException; + +@RunWith(PowerMockRunner.class) +public class PubFailRecordTest { + + private PubFailRecord pubFailRecord; + + + @Test + public void Validate_PubFailRecord_Created_With_Default_Constructor() throws ParseException { + String[] args = {"2018-08-29-10-10-10-543.", "PBF", "238465493.fileName", + "1", "/publish/1/fileName", "PUT", "application/octet-stream", "285", "200", + "172.100.0.3", "user1", "403"}; + pubFailRecord = new PubFailRecord(args); + + Assert.assertEquals("user1", pubFailRecord.getUser()); + Assert.assertEquals("172.100.0.3", pubFailRecord.getSourceIP()); + Assert.assertEquals("403", pubFailRecord.getError()); + Assert.assertEquals(200, pubFailRecord.getContentLengthReceived()); + } +} |