diff options
author | efiacor <fiachra.corcoran@est.tech> | 2019-07-23 16:22:03 +0000 |
---|---|---|
committer | efiacor <fiachra.corcoran@est.tech> | 2019-07-23 16:22:03 +0000 |
commit | f20778ffa99aa9c6f30a0f84112a5392b259ea63 (patch) | |
tree | 7c58a50f8b327a5f7f9580dbfb97fe24532ed777 /datarouter-prov/src/test/java/org | |
parent | d6302cb0b3db8043598e8b6bc3dc5ed436f848cb (diff) |
More unit test coverage and code cleanup
Change-Id: Ie28b50803c60cc79221a5c8aa08cf029f7a8486c
Issue-ID: DMAAP-1226
Signed-off-by: efiacor <fiachra.corcoran@est.tech>
Diffstat (limited to 'datarouter-prov/src/test/java/org')
6 files changed, 402 insertions, 142 deletions
diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java index edf9ef52..5239b800 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/InternalServletTest.java @@ -44,6 +44,7 @@ import javax.servlet.http.HttpServletResponse; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; import org.apache.commons.lang3.reflect.FieldUtils; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -54,6 +55,7 @@ import org.mockito.Mock; import org.onap.dmaap.datarouter.provisioning.beans.Deleteable; import org.onap.dmaap.datarouter.provisioning.beans.Insertable; import org.onap.dmaap.datarouter.provisioning.beans.LogRecord; +import org.onap.dmaap.datarouter.provisioning.beans.Parameters; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; @@ -277,6 +279,16 @@ public class InternalServletTest extends DrServletTestBase { } @Test + public void Given_Request_Is_HTTP_DELETE_With_LogRollInterval_Api_In_Endpoint_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/LOGROLL_INTERVAL"); + internalServlet.doDelete(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + Parameters p1 = Parameters.getParameter("NODES"); + Assert.assertEquals("{\"keyname\":\"NODES\",\"value\":\"dmaap-dr-node\"}", p1.asJSONObject().toString()); + Assert.assertEquals("PARAM: keyname=NODES, value=dmaap-dr-node", p1.toString()); + } + + @Test public void Given_Request_Is_HTTP_DELETE_With_Api_In_Endpoint_And_Delete_Fails_Then_Internal_Server_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/api/NODES"); @@ -332,8 +344,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -341,8 +352,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Logs_And_Content_Encoding_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Encoding")).thenReturn("not-supported"); when(request.getPathInfo()).thenReturn("/logs/"); internalServlet.doPost(request, response); @@ -364,8 +374,7 @@ public class InternalServletTest extends DrServletTestBase { } @Test - public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() - throws Exception { + public void Given_Request_Is_HTTP_POST_To_Drlogs_And_Then_Unsupported_Media_Type_Response_Is_Generated() { when(request.getHeader("Content-Type")).thenReturn("stub_contentType"); when(request.getPathInfo()).thenReturn("/drlogs/"); internalServlet.doPost(request, response); @@ -384,6 +393,13 @@ public class InternalServletTest extends DrServletTestBase { } @Test + public void Given_Request_Is_HTTP_POST_To_Api_And_Request_Succeeds() { + when(request.getPathInfo()).thenReturn("/api/NEW_PARAM?val=blah"); + internalServlet.doPost(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_OK)); + } + + @Test public void Given_Request_Is_HTTP_POST_With_Incorrect_Endpoint_Then_Not_Found_Error_Is_Generated() throws Exception { when(request.getPathInfo()).thenReturn("/incorrect/"); diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java new file mode 100644 index 00000000..7ef52ff8 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/EgressRouteTest.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import java.sql.SQLException; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class EgressRouteTest { + + private EgressRoute egressRoute; + + private static EntityManagerFactory emf; + private static EntityManager em; + 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(); + egressRoute = new EgressRoute(2, 1); + } + + @Test + public void Verify_NetworkRoute_Is_Added_Successfully() throws SQLException { + Assert.assertEquals(1, EgressRoute.getAllEgressRoutes().size()); + egressRoute.doInsert(db.getConnection()); + Assert.assertEquals(2, EgressRoute.getAllEgressRoutes().size()); + egressRoute.doDelete(db.getConnection()); + } + + @Test + public void Verify_NetworkRoute_Is_Removed_Successfully() throws SQLException { + Assert.assertEquals(1, EgressRoute.getAllEgressRoutes().size()); + EgressRoute egressRoute = new EgressRoute(1, 1); + egressRoute.doDelete(db.getConnection()); + Assert.assertEquals(0, EgressRoute.getAllEgressRoutes().size()); + } + + @Test + public void Verify_NetworkRoute_Is_Updated_Successfully() throws SQLException { + EgressRoute egressRoute = new EgressRoute(1, 1); + EgressRoute egressRoute1 = new EgressRoute(1, 1); + Assert.assertEquals(egressRoute.hashCode(), egressRoute1.hashCode()); + Assert.assertEquals(egressRoute, egressRoute1); + Assert.assertEquals(egressRoute.toString(), egressRoute1.toString()); + } +}
\ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java new file mode 100644 index 00000000..df786b55 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/beans/NetworkRouteTest.java @@ -0,0 +1,96 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.beans; + +import java.sql.SQLException; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.onap.dmaap.datarouter.provisioning.utils.DB; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class NetworkRouteTest { + + private NetworkRoute networkRoute; + + private static EntityManagerFactory emf; + private static EntityManager em; + 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(); + networkRoute = new NetworkRoute("node01.","node03.","node02."); + } + + @Test + public void Verify_NetworkRoute_Is_Added_Successfully() throws SQLException { + Assert.assertEquals(1, NetworkRoute.getAllNetworkRoutes().size()); + networkRoute.doInsert(db.getConnection()); + Assert.assertEquals(2, NetworkRoute.getAllNetworkRoutes().size()); + networkRoute.doDelete(db.getConnection()); + } + + @Test + public void Verify_NetworkRoute_Is_Removed_Successfully() throws SQLException { + Assert.assertEquals(1, NetworkRoute.getAllNetworkRoutes().size()); + NetworkRoute networkRoute = new NetworkRoute("stub_from.", "stub_to."); + networkRoute.doDelete(db.getConnection()); + Assert.assertEquals(0, NetworkRoute.getAllNetworkRoutes().size()); + } + + @Test + public void Verify_NetworkRoute_Is_Updated_Successfully() throws SQLException { + NetworkRoute networkRoute = new NetworkRoute("stub_from.", "stub_to.", "node02."); + networkRoute.doUpdate(db.getConnection()); + //Assert.assertTrue(NetworkRoute.getAllNetworkRoutes().contains(networkRoute)); + for (NetworkRoute net : + NetworkRoute.getAllNetworkRoutes()) { + Assert.assertEquals(5, net.getVianode()); + } + NetworkRoute networkRoute1 = new NetworkRoute("stub_from.", "stub_to.", "node02."); + Assert.assertEquals(networkRoute.hashCode(), networkRoute1.hashCode()); + Assert.assertEquals(networkRoute, networkRoute1); + Assert.assertEquals(networkRoute.toString(), networkRoute1.toString()); + } +}
\ No newline at end of file diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java new file mode 100644 index 00000000..056469a8 --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/DbTest.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.utils; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class DbTest { + private static EntityManagerFactory emf; + private static EntityManager em; + + private DB db = new DB(); + + @BeforeClass + public static void init() { + emf = Persistence.createEntityManagerFactory("db-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(); + } + + @Test + public void Verify_DB_Is_Initialised_Successfully() { + Assert.assertTrue(db.runRetroFits()); + } + +} diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java index 4dd1b471..ae81f15d 100755 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/LOGJSONObjectTest.java @@ -24,6 +24,7 @@ package org.onap.dmaap.datarouter.provisioning.utils; import java.io.CharArrayWriter; +import java.io.IOException; import java.io.Writer; import org.json.JSONArray; import org.json.JSONTokener; @@ -50,8 +51,7 @@ public class LOGJSONObjectTest { } @Test - public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() - throws Exception { + public void Construct_JSONObject_From_A_Subset_Of_Values_From_Another_JSONObject() { Map<String, Object> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); @@ -63,48 +63,41 @@ public class LOGJSONObjectTest { } @Test - public void Construct_JSONObject_From_A_JSONTokener() - throws Exception { + public void Construct_JSONObject_From_A_JSONTokener() { JSONTokener x = new JSONTokener("{\"key1\":\"value1\",\"key3\":\"value3\"}"); LOGJSONObject logJObject = new LOGJSONObject(x); assertThat(logJObject.toString(), is("{\"key1\":\"value1\",\"key3\":\"value3\"}")); } @Test - public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() - throws Exception { + public void Construct_JSONObject_From_A_Bean_Object_And_Populate_From_Its_Getters_And_Setters() { Map<String, Object> map = new HashMap<>(); map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); - Object bean = map; - LOGJSONObject logJObject = new LOGJSONObject(bean); + LOGJSONObject logJObject = new LOGJSONObject((Object) map); assertThat(logJObject.toString(), is("{\"empty\":false}")); } @Test - public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() - throws Exception { + public void Given_Method_Is_Accumulate_And_Value_Is_Valid_Put_Value_Into_New_JSONArray() { Map<String, Object> map = new HashMap<>(); map.put("key", 3); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.accumulate(s, null); assertThat(logJObject.get("key").toString(), is("[3,null]")); } @Test - public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() - throws Exception { + public void Given_Method_Is_Accumulate_And_Value_Is_Null_Dont_Add_Key_Value_Pair() { String s = "key"; logJO.accumulate(s, null); assertThat(logJO.has("key"), is(false)); } @Test - public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() - throws Exception { + public void Given_Method_Is_Append_And_Value_Is_Null_Append_New_Value() { String s = "key"; double d = 2.0; logJO.append(s, d); @@ -113,417 +106,334 @@ public class LOGJSONObjectTest { @Test - public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() - throws Exception { + public void Given_Method_Is_DoubleToString_And_Value_Is_NaN_Return_Null() { double d = 2.0; - assertThat(logJO.doubleToString(d), is("2")); + assertThat(LOGJSONObject.doubleToString(d), is("2")); } @Test - public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() - throws Exception { + public void Given_Method_Is_GetBoolean_And_Value_Is_False_Return_False() { Map<String, Object> map = new HashMap<>(); map.put("key", false); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getBoolean(s), is(false)); } @Test - public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() - throws Exception { + public void Given_Method_Is_GetBoolean_And_Value_Is_True_Return_True() { Map<String, Object> map = new HashMap<>(); map.put("key", true); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getBoolean(s), is(true)); } @Test - public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() - throws Exception { + public void Given_Method_Is_GetDouble_And_Value_Is_A_Double_Return_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", 2.0); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getDouble(s), is(2.0)); } @Test - public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() - throws Exception { + public void Given_Method_Is_GetInt_And_Value_Is_An_Int_Return_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", 3); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getInt(s), is(3)); } @Test - public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() - throws Exception { + public void Given_Method_Is_GetJSONArray_And_Value_Is_A_JSONArray_Return_Value() { JSONArray jA = new JSONArray(); Map<String, Object> map = new HashMap<>(); map.put("key", jA); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getJSONArray(s), is(jA)); } @Test - public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() - throws Exception { + public void Given_Method_Is_GetJSONObject_And_Value_Is_A_JSONObject_Return_Value() { LOGJSONObject logJObj = new LOGJSONObject(); logJObj.put("stub_key", 1); Map<String, Object> map = new HashMap<>(); map.put("key", logJObj); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getJSONObject(s), is(logJObj)); } @Test - public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() - throws Exception { + public void Given_Method_Is_GetLong_And_Value_Is_A_Long_Return_Value() { long l = 5; Map<String, Object> map = new HashMap<>(); map.put("key", l); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getLong(s), is(5L)); } @Test - public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() - throws Exception { + public void Given_Method_Is_getNames_And_Value_Is_A_LOGJSONObject_Return_StringArray() { LOGJSONObject logJObj = new LOGJSONObject(); logJObj.put("name1", "elyk"); String[] sArray = new String[logJObj.length()]; sArray[0] = "name1"; - LOGJSONObject logJObject = new LOGJSONObject(); - - assertThat(logJObject.getNames(logJObj), is(sArray)); + assertThat(LOGJSONObject.getNames(logJObj), is(sArray)); } @Test - public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() - throws Exception { + public void Given_Method_Is_GetString_And_Value_Is_A_String_Return_Value() { String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.getString(s), is("value")); } @Test - public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_Null_Put_Defualt_Value() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 1); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - String val = null; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_An_Int_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 3); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - int val = 2; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Long_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 4L); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - long val = 3; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Double_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 5.0); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - double val = 4.0; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() - throws Exception { + public void Given_Method_Is_Increment_And_Value_Is_A_Float_Put_Value_Plus_One() { Map<String, Object> mapResult = new HashMap<>(); mapResult.put("key", 5.0); LOGJSONObject logJObjectResult = new LOGJSONObject(mapResult); - float val = 4.0f; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; logJObject.increment(s); assertThat(logJObject.get("key"), is(logJObjectResult.get("key"))); } @Test - public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() - throws Exception { + public void Given_Method_Is_Names_And_Object_Contains_Keys_Put_Keys_Into_New_JSONArray() { JSONArray ja = new JSONArray(); ja.put("key"); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - - String s = "key"; assertThat(logJObject.names().get(0), is(ja.get(0))); } @Test - public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() - throws Exception { + public void Given_Method_Is_NumberToString_And_Number_is_Not_Null_Return_Reformatted_Number_As_String() { Number num = 3.0; - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.numberToString(num), is("3")); } @Test - public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() - throws Exception { + public void Given_Method_Is_OptBoolean_And_Value_is_Boolean_Return_Value() { boolean val = true; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optBoolean(s, false), is(true)); } @Test - public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptBoolean_And_Value_is_Not_Boolean_Return_Default_Value() { String val = "not_boolean"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optBoolean(s, false), is(false)); } @Test - public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() - throws Exception { + public void Given_Method_Is_OptDouble_And_Value_is_Double_Return_Value() { double val = 2.0; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optDouble(s, 0.0), is(2.0)); } @Test - public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptDouble_And_Value_is_Not_Double_Return_Default_Value() { String val = "not_double"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optDouble(s, 0.0), is(0.0)); } @Test - public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() - throws Exception { + public void Given_Method_Is_OptInt_And_Value_is_Int_Return_Value() { int val = 1; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optInt(s, 0), is(1)); } @Test - public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptInt_And_Value_Is_Null_Return_Default_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", null); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optInt(s, 0), is(0)); } @Test - public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() - throws Exception { + public void Given_Method_Is_OptLong_And_Value_is_Long_Return_Value() { long val = 4; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optLong(s, 0), is(4L)); } @Test - public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() - throws Exception { + public void Given_Method_Is_OptLong_And_Value_is_Not_Long_Return_Default_Value() { Map<String, Object> map = new HashMap<>(); map.put("key", null); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optLong(s, 0), is(0L)); } @Test - public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() - throws Exception { + public void Given_Method_Is_OptString_And_Value_is_String_Return_Value() { String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", val); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.optString(s, "default_value"), is("value")); } @Test - public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() - throws Exception { + public void Given_Method_Is_putOnce_And_KeyValuePair_Does_Not_Exist_In_logJObject_Put_KeyValuePair_Into_logJObject() { String val = "value"; Map<String, Object> map = new HashMap<>(); LOGJSONObject logJObject = new LOGJSONObject(map); - String s = "key"; assertThat(logJObject.putOnce(s, val).get("key"), is("value")); } @Test - public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() - throws Exception { + public void Given_Method_Is_StringToValue_And_Value_Is_Number_Return_Number() { String val = "312"; Map<String, Object> map = new HashMap<>(); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.stringToValue(val), is(312)); + assertThat(LOGJSONObject.stringToValue(val), is(312)); } @Test - public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() - throws Exception { + public void Given_Method_Is_ToJSONArray_And_KeyValue_Exists_Return_Value_Array() { JSONArray names = new JSONArray(); Map<String, Object> map = new HashMap<>(); map.put("name", "value"); names.put("name"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.toJSONArray(names).get(0), is("value")); } @Test - public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() - throws Exception { + public void Given_Method_Is_ValueToString_And_Value_Is_JSONArray_Return_Value_To_String() { JSONArray val = new JSONArray(); - Map<String, Object> map = new HashMap<>(); - map.put("key", "value"); val.put("value"); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.valueToString(val), is("[\"value\"]")); + assertThat(LOGJSONObject.valueToString(val), is("[\"value\"]")); } @Test - public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() - throws Exception { + public void Given_Method_Is_writeValue_And_Value_IS_Not_Null_Return_Writer_With_Value() throws IOException { Writer writer = new CharArrayWriter(); String val = "value"; - Map<String, Object> map = new HashMap<>(); - map.put("key", "value"); - LOGJSONObject logJObject = new LOGJSONObject(map); - - assertThat(logJObject.writeValue(writer, val, 3, 1).toString(), is("\"value\"")); + assertThat(LOGJSONObject.writeValue(writer, val, 3, 1).toString(), is("\"value\"")); } @Test - public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() - throws Exception { + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_One_Write_Value_With_Indent() { Writer writer = new CharArrayWriter(); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", "value"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.write(writer, 3, 1).toString(), is("{\"key\": \"value\"}")); } @Test - public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() - throws Exception { + public void Given_Method_Is_write_And_Length_Of_logJObject_Is_Not_One_Or_Zero_Write_Value_With_New_Indent() { Writer writer = new CharArrayWriter(); - String val = "value"; Map<String, Object> map = new HashMap<>(); map.put("key", "value"); map.put("key1", "value1"); LOGJSONObject logJObject = new LOGJSONObject(map); - assertThat(logJObject.write(writer, 3, 1).toString(), is("{\n \"key1\": \"value1\",\n \"key\": \"value\"\n }")); } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java new file mode 100644 index 00000000..604b4c0a --- /dev/null +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/utils/PurgeLogDirTaskTest.java @@ -0,0 +1,87 @@ +/*- + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation. + * ================================================================================ + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.dmaap.datarouter.provisioning.utils; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.Persistence; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.powermock.modules.junit4.PowerMockRunner; + +@RunWith(PowerMockRunner.class) +public class PurgeLogDirTaskTest { + + private static EntityManagerFactory emf; + private static EntityManager em; + private PurgeLogDirTask purgeLogDirTask = new PurgeLogDirTask(); + private File testLog; + + @Before + public void setUp() throws Exception { + testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs"); + prepFile(testLog); + } + + @After + public void tearDown() throws IOException { + Files.deleteIfExists(testLog.toPath()); + } + + @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(); + } + + @Test + public void Verify_Logs_Are_Purged() { + purgeLogDirTask.run(); + } + + private void prepFile(File logFile) { + try (FileWriter fileWriter = new FileWriter(logFile)) { + fileWriter.write("2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n"); + } + catch (IOException e){ + System.out.println(e.getMessage()); + } + } +} |