diff options
author | Fiachra Corcoran <fiachra.corcoran@ericsson.com> | 2018-08-09 00:04:29 +0100 |
---|---|---|
committer | Ronan Keogh <ronan.keogh@ericsson.com> | 2018-08-17 17:58:55 +0100 |
commit | e06737d701ff5b3dcab311f4337ce40be52c966e (patch) | |
tree | 81d76de3d400c99c531147c242fc702506329140 /datarouter-prov/src/test/java | |
parent | 49fbf9a09d244bbdc19783d9ff10cd03cd89640a (diff) |
Update for OOM integration
Issue-ID: DMAAP-107
Change-Id: Iff9f93040f7b3120cffb5755adc693e24de991a7
Signed-off-by: Fiachra Corcoran <fiachra.corcoran@ericsson.com>
Diffstat (limited to 'datarouter-prov/src/test/java')
6 files changed, 212 insertions, 211 deletions
diff --git a/datarouter-prov/src/test/java/datarouter/provisioning/AllTests.java b/datarouter-prov/src/test/java/datarouter/provisioning/AllTests.java index 34fb144f..33a34c07 100644 --- a/datarouter-prov/src/test/java/datarouter/provisioning/AllTests.java +++ b/datarouter-prov/src/test/java/datarouter/provisioning/AllTests.java @@ -30,20 +30,21 @@ import org.junit.runners.Suite.SuiteClasses; @RunWith(Suite.class) @SuiteClasses({ - IntegrationTestDrFeedsPost.class, - IntegrationTestDrFeedsGet.class, - IntegrationTestDrFeedsPut.class, - IntegrationTestDrFeedsDelete.class, - IntegrationTestFeedPut.class, - IntegrationTestSubscribePost.class, - IntegrationTestInternalGet.class, - IntegrationTestInternalMisc.class, - IntegrationTestPublish.class, - IntegrationTestLogGet.class, - IntegrationTestFeedDelete.class, - IntegrationTestCleanup.class, - IntegrationTestRleBitSet.class - }) + IntegrationTestDrFeedsPost.class, + IntegrationTestDrFeedsPut.class, + IntegrationTestDrFeedsDelete.class, + IntegrationTestFeedPut.class, + IntegrationTestDrFeedsGet.class, + IntegrationTestInternalGet.class, + IntegrationTestInternalMisc.class, + IntegrationTestPublish.class, + IntegrationTestSubscribePost.class, + IntegrationTestLogGet.class, + IntegrationTestFeedDelete.class, + IntegrationTestCleanup.class, + IntegrationTestRleBitSet.class +}) public class AllTests { + } diff --git a/datarouter-prov/src/test/java/datarouter/provisioning/DbTestData.java b/datarouter-prov/src/test/java/datarouter/provisioning/DbTestData.java new file mode 100644 index 00000000..36a2eb01 --- /dev/null +++ b/datarouter-prov/src/test/java/datarouter/provisioning/DbTestData.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * ============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 datarouter.provisioning; + +import org.apache.http.HttpEntity; +import org.apache.http.HttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ByteArrayEntity; +import org.apache.http.entity.ContentType; +import org.apache.http.impl.client.AbstractHttpClient; +import org.apache.http.util.EntityUtils; +import org.json.JSONArray; +import org.json.JSONObject; +import org.onap.dmaap.datarouter.provisioning.FeedServlet; + +import java.io.IOException; +import java.util.Properties; + +/** + * The DbTestData class + * + * @version 1.0.1 + */ +public class DbTestData { + + private static boolean dbReady = false; + + public static void populateDb(AbstractHttpClient httpclient, Properties props) { + if (!dbReady) { + JSONObject jo = buildFeedRequest(); + for (int i = 0; i < 10; i++) { + jo.put("version", "" + System.currentTimeMillis()); + int statusCode = -1; + String url = props.getProperty("test.host"); + HttpPost httpPost = new HttpPost(url); + try { + httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit"); + String feedRequestString = jo.toString(); + HttpEntity body = new ByteArrayEntity(feedRequestString.getBytes(), + ContentType.create(FeedServlet.FEED_CONTENT_TYPE)); + httpPost.setEntity(body); + HttpResponse response = httpclient.execute(httpPost); + statusCode = response.getStatusLine().getStatusCode(); + HttpEntity entity = response.getEntity(); + EntityUtils.consume(entity); + } catch (IOException e) { + System.err.println(e); + } finally { + httpPost.releaseConnection(); + } + System.out.println(i + " " + statusCode); + } + dbReady = true; + } + } + + private static JSONObject buildFeedRequest() { + JSONObject jo = new JSONObject(); + jo.put("name", "feed"); + jo.put("version", "" + System.currentTimeMillis()); + jo.put("description", "Sample feed used by JUnit to test"); + + JSONObject jo2 = new JSONObject(); + jo2.put("classification", "unrestricted"); + + JSONObject jo3 = new JSONObject(); + jo3.put("id", "id001"); + jo3.put("password", "re1kwelj"); + + JSONObject jo4 = new JSONObject(); + jo4.put("id", "id002"); + jo4.put("password", "o9eqlmbd"); + + JSONArray ja = new JSONArray(); + ja.put(jo3); + ja.put(jo4); + jo2.put("endpoint_ids", ja); + + ja = new JSONArray(); + ja.put("10.0.0.1"); + ja.put("192.168.0.1"); + ja.put("135.207.136.128/25"); + jo2.put("endpoint_addrs", ja); + + jo.put("authorization", jo2); + return jo; + } +} diff --git a/datarouter-prov/src/test/java/datarouter/provisioning/FillDB.java b/datarouter-prov/src/test/java/datarouter/provisioning/FillDB.java deleted file mode 100644 index fd682bb3..00000000 --- a/datarouter-prov/src/test/java/datarouter/provisioning/FillDB.java +++ /dev/null @@ -1,148 +0,0 @@ -/******************************************************************************* - * ============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 datarouter.provisioning; - -import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; -import java.security.KeyManagementException; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.UnrecoverableKeyException; - -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.conn.scheme.Scheme; -import org.apache.http.conn.ssl.SSLSocketFactory; -import org.apache.http.entity.ByteArrayEntity; -import org.apache.http.entity.ContentType; -import org.apache.http.impl.client.AbstractHttpClient; -import org.apache.http.impl.client.DefaultHttpClient; -import org.apache.http.util.EntityUtils; -import org.json.JSONArray; -import org.json.JSONObject; -import org.onap.dmaap.datarouter.provisioning.FeedServlet; - -/** - * The FillDB class - * - * @version 1.0.1 - */ -public class FillDB { - /** - * This is the main method of the FillDB class. - * - * @throws KeyStoreException KeyStore exception - * @throws FileNotFoundException Exeception thrown when error locating file - * @throws KeyManagementException KeyManagement exception - * @throws UnrecoverableKeyException Exception thrown when Key cannot be recovered - * @throws NoSuchAlgorithmException Exception thrown when algorithm does not exist - */ - public static void main(String[] args) - throws KeyStoreException, FileNotFoundException, KeyManagementException, UnrecoverableKeyException, - NoSuchAlgorithmException { - AbstractHttpClient httpclient = new DefaultHttpClient(); - - String keystore = "/home/eby/dr2/misc/client.keystore"; - String kspass = "changeit"; - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); - FileInputStream instream = new FileInputStream(new File(keystore)); - try { - trustStore.load(instream, kspass.toCharArray()); - } catch (Exception x) { - System.err.println("READING KEYSTORE: " + x); - } finally { - try { - instream.close(); - } catch (Exception ignore) { - // Ignore exception - } - } - - SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore, "changeit", trustStore); - Scheme sch = new Scheme("https", 443, socketFactory); - httpclient.getConnectionManager().getSchemeRegistry().register(sch); - - JSONObject jo = buildFeedRequest(); - for (int i = 0; i < 10000; i++) { - jo.put("version", "" + System.currentTimeMillis()); - int rv = -1; - String url = "https://conwy.proto.research.att.com:6443/"; - HttpPost httpPost = new HttpPost(url); - try { - httpPost.addHeader(FeedServlet.BEHALF_HEADER, "JUnit"); - String feedRequestString = jo.toString(); - HttpEntity body = new ByteArrayEntity(feedRequestString.getBytes(), - ContentType.create(FeedServlet.FEED_CONTENT_TYPE)); - httpPost.setEntity(body); - - HttpResponse response = httpclient.execute(httpPost); - rv = response.getStatusLine().getStatusCode(); - HttpEntity entity = response.getEntity(); - EntityUtils.consume(entity); - } catch (IOException e) { - System.err.println(e); - } finally { - httpPost.releaseConnection(); - } - System.out.println(i + " " + rv); - } - } - - private static JSONObject buildFeedRequest() { - JSONObject jo = new JSONObject(); - jo.put("name", "feed"); - jo.put("version", "" + System.currentTimeMillis()); - jo.put("description", "Sample feed used by JUnit to test"); - - JSONObject jo2 = new JSONObject(); - jo2.put("classification", "unrestricted"); - - - JSONObject jo3 = new JSONObject(); - jo3.put("id", "id001"); - jo3.put("password", "re1kwelj"); - - JSONObject jo4 = new JSONObject(); - jo4.put("id", "id002"); - jo4.put("password", "o9eqlmbd"); - - JSONArray ja = new JSONArray(); - ja.put(jo3); - ja.put(jo4); - jo2.put("endpoint_ids", ja); - - ja = new JSONArray(); - ja.put("10.0.0.1"); - ja.put("192.168.0.1"); - ja.put("135.207.136.128/25"); - jo2.put("endpoint_addrs", ja); - - jo.put("authorization", jo2); - return jo; - } -} diff --git a/datarouter-prov/src/test/java/datarouter/provisioning/IntegrationTestBase.java b/datarouter-prov/src/test/java/datarouter/provisioning/IntegrationTestBase.java index 52e80a8f..71446219 100644 --- a/datarouter-prov/src/test/java/datarouter/provisioning/IntegrationTestBase.java +++ b/datarouter-prov/src/test/java/datarouter/provisioning/IntegrationTestBase.java @@ -49,14 +49,17 @@ import org.junit.Before; import org.onap.dmaap.datarouter.provisioning.FeedServlet; public class IntegrationTestBase { - /** The properties file to read the DB properties from. */ - public static final String CONFIG_FILE = "integration_test.properties"; + + /** + * The properties file to read the DB properties from. + */ + private static final String CONFIG_FILE = "integration_test.properties"; public Properties props; protected AbstractHttpClient httpclient; - protected String s33; - protected String s257; - protected static JSONObject db_state; + String s33; + String s257; + static JSONObject db_state; /** * This is the setUp method. @@ -65,13 +68,10 @@ public class IntegrationTestBase { public void setUp() throws Exception { if (props == null) { props = new Properties(); - InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE); - try { + try (InputStream inStream = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)) { props.load(inStream); } catch (Exception e) { e.printStackTrace(); - } finally { - inStream.close(); } } @@ -83,8 +83,8 @@ public class IntegrationTestBase { // keystore String store = props.getProperty("test.keystore"); - String pass = props.getProperty("test.kspassword"); - KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); + String pass = props.getProperty("test.kspassword"); + KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); FileInputStream instream = new FileInputStream(new File(store)); try { keyStore.load(instream, pass.toCharArray()); @@ -99,8 +99,8 @@ public class IntegrationTestBase { } store = props.getProperty("test.truststore"); - pass = props.getProperty("test.tspassword"); - KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); + pass = props.getProperty("test.tspassword"); + KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); instream = new FileInputStream(new File(store)); try { trustStore.load(instream, pass.toCharArray()); @@ -117,15 +117,17 @@ public class IntegrationTestBase { SSLSocketFactory socketFactory = new SSLSocketFactory(keyStore, "changeit", trustStore); Scheme sch = new Scheme("https", 443, socketFactory); httpclient.getConnectionManager().getSchemeRegistry().register(sch); + + //DbTestData.populateDb(httpclient, props); } /** * This is the getDBstate method. */ - public JSONObject getDBstate() { + void getDBstate() { // set db_state! if (db_state == null) { - String url = props.getProperty("test.host") + "/internal/prov"; + String url = props.getProperty("test.host") + "/internal/prov"; HttpGet httpGet = new HttpGet(url); try { httpGet.addHeader(FeedServlet.BEHALF_HEADER, "JUnit"); @@ -133,7 +135,7 @@ public class IntegrationTestBase { HttpEntity entity = response.getEntity(); String ctype = entity.getContentType().getValue().trim(); // save the response body as db_state - boolean ok = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1); + boolean ok = ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE1); ok |= ctype.equals(FeedServlet.PROVFULL_CONTENT_TYPE2); if (ok) { db_state = null; @@ -151,7 +153,6 @@ public class IntegrationTestBase { httpGet.releaseConnection(); } } - return db_state; } /** diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java index 5966b775..99142ac9 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/BaseServletTest.java @@ -29,11 +29,14 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; +import org.onap.dmaap.datarouter.provisioning.utils.DB; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.HashSet; +import java.util.Properties; import java.util.Set; import static org.hamcrest.Matchers.is; @@ -44,6 +47,7 @@ import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class BaseServletTest { + private BaseServlet baseServlet; @Mock @@ -51,20 +55,25 @@ public class BaseServletTest { @Before public void setUp() throws Exception { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsg_flag", false, true); + Properties props = new Properties(); + props.setProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false"); + FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true); SynchronizerTask synchronizerTask = mock(SynchronizerTask.class); + when(synchronizerTask.getState()).thenReturn(SynchronizerTask.UNKNOWN); FieldUtils.writeDeclaredStaticField(BaseServlet.class, "synctask", synchronizerTask, true); baseServlet = new BaseServlet(); } @Test - public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() throws Exception { + public void Given_Request_Path_Info_Is_Valid_Then_Id_Is_Extracted_Correctly() { when(request.getPathInfo()).thenReturn("/123"); assertThat(baseServlet.getIdFromPath(request), is(123)); } + @Test - public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() throws Exception { + public void Given_Request_Path_Info_Is_Not_Valid_Then_Minus_One_Is_Returned() { when(request.getPathInfo()).thenReturn("/abc"); assertThat(baseServlet.getIdFromPath(request), is(-1)); when(request.getPathInfo()).thenReturn("/"); @@ -76,8 +85,10 @@ public class BaseServletTest { when(request.isSecure()).thenReturn(true); Set<String> authAddressesAndNetworks = new HashSet<String>(); authAddressesAndNetworks.add(("127.0.0.1")); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "require_cert", false, true); + FieldUtils + .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, + true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true); assertThat(baseServlet.isAuthorizedForProvisioning(request), is(nullValue())); } } diff --git a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java index 0e717b35..858a71c2 100644 --- a/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java +++ b/datarouter-prov/src/test/java/org/onap/dmaap/datarouter/provisioning/FeedServletTest.java @@ -23,7 +23,6 @@ package org.onap.dmaap.datarouter.provisioning; import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.log4j.Logger; import org.json.JSONObject; import org.junit.Before; import org.junit.Test; @@ -33,6 +32,7 @@ import org.onap.dmaap.datarouter.authz.AuthorizationResponse; import org.onap.dmaap.datarouter.authz.Authorizer; import org.onap.dmaap.datarouter.provisioning.beans.Feed; import org.onap.dmaap.datarouter.provisioning.beans.Updateable; +import org.onap.dmaap.datarouter.provisioning.utils.DB; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; import org.powermock.modules.junit4.PowerMockRunner; @@ -41,8 +41,8 @@ import javax.servlet.ServletInputStream; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.OutputStream; import java.util.HashSet; +import java.util.Properties; import java.util.Set; import static org.hamcrest.Matchers.notNullValue; @@ -53,6 +53,7 @@ import static org.onap.dmaap.datarouter.provisioning.BaseServlet.BEHALF_HEADER; @RunWith(PowerMockRunner.class) @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Feed") public class FeedServletTest { + private static FeedServlet feedServlet; @Mock @@ -71,7 +72,8 @@ public class FeedServletTest { } @Test - public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { when(request.isSecure()).thenReturn(false); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); @@ -79,7 +81,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() + throws Exception { setBehalfHeader(null); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -87,7 +90,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() + throws Exception { when(request.getPathInfo()).thenReturn(null); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -95,7 +99,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_DELETE_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() + throws Exception { setFeedToReturnInvalidFeedIdSupplied(); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); @@ -103,7 +108,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() + throws Exception { setAuthoriserToReturnRequestNotAuthorized(); feedServlet.doDelete(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); @@ -111,19 +117,22 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Fails_An_Internal_Server_Error_Is_Reported() + throws Exception { FeedServlet feedServlet = new FeedServlet() { protected boolean doUpdate(Updateable bean) { return false; } }; feedServlet.doDelete(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); + verify(response) + .sendError(eq(HttpServletResponse.SC_INTERNAL_SERVER_ERROR), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_DELETE_And_Delete_On_Database_Succeeds_A_NO_CONTENT_Response_Is_Generated() + throws Exception { FeedServlet feedServlet = new FeedServlet() { protected boolean doUpdate(Updateable bean) { return true; @@ -134,14 +143,16 @@ public class FeedServletTest { } @Test - public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { when(request.isSecure()).thenReturn(false); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() + throws Exception { setBehalfHeader(null); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -149,7 +160,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() + throws Exception { when(request.getPathInfo()).thenReturn(null); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -157,7 +169,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() + throws Exception { setFeedToReturnInvalidFeedIdSupplied(); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); @@ -165,7 +178,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_GET_And_Request_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() + throws Exception { setAuthoriserToReturnRequestNotAuthorized(); feedServlet.doGet(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); @@ -182,14 +196,16 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_Is_Not_Secure_When_HTTPS_Is_Required_Then_Forbidden_Response_Is_Generated() + throws Exception { when(request.isSecure()).thenReturn(false); feedServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_PUT_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_BEHALF_HEADER_Is_Not_Set_In_Request_Then_Bad_Request_Response_Is_Generated() + throws Exception { setBehalfHeader(null); feedServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -197,7 +213,8 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_Path_Header_Is_Not_Set_In_Request_With_Valid_Path_Then_Bad_Request_Response_Is_Generated() + throws Exception { when(request.getPathInfo()).thenReturn(null); feedServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_BAD_REQUEST), argThat(notNullValue(String.class))); @@ -205,21 +222,25 @@ public class FeedServletTest { @Test - public void Given_Request_Is_HTTP_PUT_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_Feed_Id_Is_Invalid_Then_Not_Found_Response_Is_Generated() + throws Exception { setFeedToReturnInvalidFeedIdSupplied(); feedServlet.doPut(request, response); verify(response).sendError(eq(HttpServletResponse.SC_NOT_FOUND), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_Content_Header_Is_Not_Supported_Type_Then_Unsupported_Media_Type_Response_Is_Generated() + throws Exception { when(request.getContentType()).thenReturn("stub_contentType"); feedServlet.doPut(request, response); - verify(response).sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class))); + verify(response) + .sendError(eq(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE), argThat(notNullValue(String.class))); } @Test - public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() throws Exception { + public void Given_Request_Is_HTTP_PUT_And_Request_Contains_Badly_Formed_JSON_Then_Bad_Request_Response_Is_Generated() + throws Exception { when(request.getHeader("Content-Type")).thenReturn("application/vnd.att-dr.feed; version=1.0"); ServletInputStream inStream = mock(ServletInputStream.class); when(request.getInputStream()).thenReturn(inStream); @@ -228,8 +249,12 @@ public class FeedServletTest { } - private void initialiseBaseServletToBypassRetreiviingInitialisationParametersFromDatabase() throws IllegalAccessException { - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsg_flag", false, true); + private void initialiseBaseServletToBypassRetreiviingInitialisationParametersFromDatabase() + throws IllegalAccessException { + Properties props = new Properties(); + props.setProperty("org.onap.dmaap.datarouter.provserver.isaddressauthenabled", "false"); + FieldUtils.writeDeclaredStaticField(DB.class, "props", props, true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "startmsgFlag", false, true); SynchronizerTask synchronizerTask = mock(SynchronizerTask.class); when(synchronizerTask.getState()).thenReturn(SynchronizerTask.UNKNOWN); FieldUtils.writeDeclaredStaticField(SynchronizerTask.class, "synctask", synchronizerTask, true); @@ -239,8 +264,10 @@ public class FeedServletTest { when(request.isSecure()).thenReturn(true); Set<String> authAddressesAndNetworks = new HashSet<String>(); authAddressesAndNetworks.add(("127.0.0.1")); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, true); - FieldUtils.writeDeclaredStaticField(BaseServlet.class, "require_cert", false, true); + FieldUtils + .writeDeclaredStaticField(BaseServlet.class, "authorizedAddressesAndNetworks", authAddressesAndNetworks, + true); + FieldUtils.writeDeclaredStaticField(BaseServlet.class, "requireCert", false, true); } private void setBehalfHeader(String headerValue) { @@ -282,7 +309,7 @@ public class FeedServletTest { private void setPokerToNotCreateTimersWhenDeleteFeedIsCalled() throws Exception { Poker poker = mock(Poker.class); - FieldUtils.writeDeclaredStaticField(Poker.class, "p", poker, true); + FieldUtils.writeDeclaredStaticField(Poker.class, "poker", poker, true); } private void setupValidAuthorisedRequest() throws Exception { |