Testcases were skimming through the code, but actually make no asserts.
Change-Id: Icba8fe890c01fc11a4507ca468ab4d9b6141caf9
Issue-ID: DCAEGEN2-524
Signed-off-by: PawelSzalapski <pawel.szalapski@nokia.com>
import com.att.nsa.logging.log4j.EcompFields;\r
import com.google.common.reflect.TypeToken;\r
import com.google.gson.Gson;\r
+import java.util.Map;\r
import org.json.JSONObject;\r
import org.slf4j.Logger;\r
import org.slf4j.LoggerFactory;\r
import java.util.Date;\r
import java.util.HashMap;\r
import java.util.List;\r
-import java.util.TimeZone;\r
\r
\r
public class EventProcessor implements Runnable {\r
}.getType();\r
private final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, MM dd yyyy hh:mm:ss z");\r
\r
- private static HashMap<String, String[]> streamidHash = new HashMap<>();\r
- JSONObject event;\r
+ static Map<String, String[]> streamidHash = new HashMap<>();\r
+ public JSONObject event;\r
\r
public EventProcessor() {\r
- dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));\r
- String[] list = CommonStartup.streamid.split("\\|");\r
+ streamidHash = parseStreamIdToStreamHashMapping(CommonStartup.streamid);\r
+ }\r
+\r
+ private Map<String, String[]> parseStreamIdToStreamHashMapping(String streamId) {\r
+ Map<String, String[]> streamidHash = new HashMap<>();\r
+ String[] list = streamId.split("\\|");\r
for (String aList : list) {\r
String domain = aList.split("=")[0];\r
-\r
String[] streamIdList = aList.substring(aList.indexOf('=') + 1).split(",");\r
\r
- log.debug(String.format("Domain: %s streamIdList:%s", domain, Arrays.toString(streamIdList)));\r
streamidHash.put(domain, streamIdList);\r
}\r
-\r
+ return streamidHash;\r
}\r
\r
+\r
@Override\r
public void run() {\r
\r
import com.att.nsa.security.authenticators.SimpleAuthenticator;\r
import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
\r
-\r
public class RestfulCollectorServlet extends CommonServlet\r
{\r
\r
public static String authlist;\r
- \r
+\r
public RestfulCollectorServlet ( rrNvReadable settings ) throws loadException, missingReqdSetting\r
{\r
super ( settings, "collector", false );\r
// logging. The Restful Collector likely doesn't need API authentication, so for now,\r
// we init the base class services with an in-memory (and empty!) config DB.\r
commonServletSetup ( ConfigDbType.MEMORY );\r
- \r
+\r
VESLogger.setUpEcompLogging();\r
\r
// setup the servlet routing and error handling\r
if (CommonStartup.authflag > 0) {\r
NsaAuthenticator<NsaSimpleApiKey> NsaAuth;\r
NsaAuth = AuthlistHandler(authlist);\r
- \r
+\r
this.getSecurityManager().addAuthenticator(NsaAuth);\r
}\r
- \r
+\r
log.info ( "Restful Collector Servlet is up." );\r
}\r
catch ( SecurityException e )\r
if (authlist != null)\r
{\r
String authpair[] = authlist.split("\\|");\r
- for (String pair: authpair) { \r
- String lineid[] = pair.split(",");\r
- String listauthid = lineid[0]; \r
- String listauthpwd = new String(Base64.decodeBase64(lineid[1]));\r
- ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);\r
- }\r
- \r
+ for (String pair: authpair) {\r
+ String lineid[] = pair.split(",");\r
+ String listauthid = lineid[0];\r
+ String listauthpwd = new String(Base64.decodeBase64(lineid[1]));\r
+ ((SimpleAuthenticator) NsaAuth).add(listauthid,listauthpwd);\r
+ }\r
+\r
}\r
else\r
{\r
return NsaAuth;\r
\r
}\r
- \r
+\r
\r
private static final long serialVersionUID = 1L;\r
private static final Logger log = LoggerFactory.getLogger ( RestfulCollectorServlet.class );\r
}\r
+\r
--- /dev/null
+/*-
+ * ============LICENSE_START=======================================================
+ * PROJECT
+ * ================================================================================
+ * Copyright (C) 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=========================================================
+ */
+package org.onap.dcae.commonFunction;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+import com.att.nsa.cmdLine.NsaCommandLineUtil;
+import com.att.nsa.drumlin.service.framework.context.DrumlinRequest;
+import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;
+import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;
+import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;
+import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;
+import com.att.nsa.security.authenticators.SimpleAuthenticator;
+import com.att.nsa.security.db.simple.NsaSimpleApiKey;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import java.io.FileReader;
+import java.io.IOException;
+import java.util.Base64;
+import java.util.Map;
+import java.util.concurrent.LinkedBlockingQueue;
+import org.json.JSONArray;
+import org.json.JSONObject;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;
+import org.onap.dcae.restapi.RestfulCollectorServlet;
+
+
+public class TestCommonStartup {
+
+ @Test
+ public void testParseCLIArguments() {
+ // given
+ String args[] = {"-a", "aa"};
+ Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);
+ // when
+ nvReadableStack settings = new nvReadableStack();
+ settings.push(new nvReadableTable(argMap));
+
+ // then
+ assertEquals(settings.getString("a", "default"), "aa");
+ }
+
+ @Test
+ public void shouldPutValidVESEventOnProcessingQueueWithoutExceptions() throws IOException, QueueFullException {
+ // given
+ CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue<>(
+ CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);
+ JsonElement vesEvent = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt"));
+ JSONObject validVESEvent = new JSONObject(vesEvent.toString());
+ JSONArray jsonArrayMod = new JSONArray().put(validVESEvent);
+
+ // then
+ CommonStartup.handleEvents(jsonArrayMod);
+ }
+
+
+ @Test
+ public void testParseStreamIdToStreamHashMapping() {
+ // given
+ CommonStartup.streamid = "fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling";
+ EventProcessor eventProcessor = new EventProcessor();
+
+ // when
+ Map<String, String[]> streamHashMapping = EventProcessor.streamidHash;
+
+ // then
+ assertEquals(streamHashMapping.get("fault")[0], "sec_fault");
+ assertEquals(streamHashMapping.get("measurementsForVfScaling")[0], "sec_measurement");
+ }
+
+ @Test
+ public void testAuthListHandler() throws loadException, missingReqdSetting {
+ // given
+ final nvReadableStack settings = new nvReadableStack();
+
+ String user1 = "secureid";
+ String password1Hashed = "IWRjYWVSb2FkbTEyMyEt";
+ String password1UnHashed = decode("IWRjYWVSb2FkbTEyMyEt");
+ String user2 = "sample1";
+ String password2Hashed = "c2FtcGxlMQ";
+
+ String authlist = user1 + "," + password1Hashed + "|" + user2 + "," + password2Hashed;
+
+ RestfulCollectorServlet rsv = new RestfulCollectorServlet(settings);
+
+ DrumlinRequest drumlinRequestMock = Mockito.mock(DrumlinRequest.class);
+
+ String basicHeaderForUser1 = "Basic " + encode(user1, password1UnHashed);
+ when(drumlinRequestMock.getFirstHeader("Authorization")).thenReturn(basicHeaderForUser1);
+
+ // when
+ SimpleAuthenticator simpleAuthenticator = (SimpleAuthenticator) rsv.AuthlistHandler(authlist);
+ NsaSimpleApiKey authentic = simpleAuthenticator.isAuthentic(drumlinRequestMock);
+
+ // then
+ assertEquals(authentic.getSecret(), password1UnHashed);
+ }
+
+ private String decode(String hashedPassword) {
+ return new String(Base64.getDecoder().decode(hashedPassword.getBytes()));
+ }
+
+ private String encode(String user1, String password1UnHashed) {
+ return Base64.getEncoder().encodeToString((user1 + ":" + password1UnHashed).getBytes());
+ }
+
+}
+
+
+++ /dev/null
-/*-\r
- * ============LICENSE_START=======================================================\r
- * PROJECT\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-package org.onap.dcae.vestest;\r
-\r
-import java.io.FileReader;\r
-import java.io.IOException;\r
-import java.net.URL;\r
-import java.util.Map;\r
-import java.util.concurrent.LinkedBlockingQueue;\r
-\r
-import org.json.JSONArray;\r
-import org.json.JSONException;\r
-import org.json.JSONObject;\r
-import org.junit.After;\r
-import org.junit.Assert;\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-import org.onap.dcae.commonFunction.CommonStartup;\r
-import org.onap.dcae.commonFunction.EventProcessor;\r
-import org.onap.dcae.commonFunction.CommonStartup.QueueFullException;\r
-import org.onap.dcae.restapi.RestfulCollectorServlet;\r
-\r
-import com.att.nsa.cmdLine.NsaCommandLineUtil;\r
-import com.att.nsa.drumlin.service.framework.DrumlinServlet;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable.loadException;\r
-import com.att.nsa.drumlin.till.nv.rrNvReadable.missingReqdSetting;\r
-import com.att.nsa.security.NsaAuthenticator;\r
-import com.att.nsa.security.authenticators.SimpleAuthenticator;\r
-import com.att.nsa.security.db.simple.NsaSimpleApiKey;\r
-import com.google.gson.JsonParser;\r
-import com.att.nsa.drumlin.till.nv.impl.nvPropertiesFile;\r
-import com.att.nsa.drumlin.till.nv.impl.nvReadableStack;\r
-import com.att.nsa.drumlin.till.nv.impl.nvReadableTable;\r
-\r
-\r
-public class TestCommonStartup {\r
-\r
- String payload = null;\r
- @Before\r
- public void setUp() throws Exception {\r
-\r
- // process command line arguments\r
- payload = new JsonParser().parse(new FileReader("src/test/resources/VES_valid.txt")).toString();\r
- CommonStartup.fProcessingInputQueue = new LinkedBlockingQueue<JSONObject> (CommonStartup.KDEFAULT_MAXQUEUEDEVENTS);\r
- }\r
-\r
- @After\r
- public void tearDown() throws Exception {\r
-\r
- }\r
-\r
- @Test\r
- public void testCommonStartupload() {\r
-\r
- String args[] = { "junittest" };\r
- final Map<String, String> argMap = NsaCommandLineUtil.processCmdLine(args, true);\r
- final String config = NsaCommandLineUtil.getSetting(argMap, "c", "collector.properties");\r
- final URL settingStream = DrumlinServlet.findStream(config, CommonStartup.class);\r
-\r
- final nvReadableStack settings = new nvReadableStack();\r
- try {\r
- settings.push(new nvPropertiesFile(settingStream));\r
- } catch (loadException e) {\r
- // TODO Auto-generated catch block\r
- e.printStackTrace();\r
- }\r
- settings.push(new nvReadableTable(argMap));\r
- Assert.assertEquals("true", "true");\r
- }\r
-\r
- @Test\r
- public void testhandleevent() {\r
- JSONArray jsonArrayMod = new JSONArray().put(new JSONObject(payload));\r
- try {\r
-\r
-\r
- CommonStartup.handleEvents (jsonArrayMod);\r
- } catch ( JSONException | QueueFullException | IOException e) {\r
- // TODO Auto-generated catch block\r
- //e.printStackTrace();\r
- System.out.println("junit reported:" + e.getMessage());\r
- }\r
- Assert.assertEquals("true", "true");\r
- }\r
-\r
-\r
-/*\r
- @Test\r
- public void testServlet() {\r
- try\r
- {\r
- RestfulCollectorServlet rsv = new RestfulCollectorServlet(null);\r
- }\r
- catch (NullPointerException|loadException| missingReqdSetting e){\r
- System.out.println("junit reported:" + e.getMessage());\r
- }\r
- Assert.assertEquals("true", "true");\r
- }\r
-*/\r
-\r
-\r
- \r
- @Test\r
- public void testEventProcessorinstantiation()\r
- {\r
- CommonStartup.streamid="fault=sec_fault|syslog=sec_syslog|heartbeat=sec_heartbeat|measurementsForVfScaling=sec_measurement|mobileFlow=sec_mobileflow|other=sec_other|stateChange=sec_statechange|thresholdCrossingAlert=sec_thresholdCrossingAlert|voiceQuality=ves_voicequality|sipSignaling=ves_sipsignaling";\r
- EventProcessor ep = new EventProcessor ();\r
- Thread epThread=new Thread(ep);\r
- epThread.start();\r
- Assert.assertEquals("true", "true");\r
- epThread.interrupt();\r
-\r
- }\r
-\r
- @Test\r
- public void testAuthListHandler()\r
- {\r
- \r
- final Map<String, String> argMap = NsaCommandLineUtil.processCmdLine ( new String[0], true );\r
- final String config = NsaCommandLineUtil.getSetting ( argMap, "c", "collector.properties" );\r
- final URL settingStream = DrumlinServlet.findStream ( config, CommonStartup.class );\r
-\r
- final nvReadableStack settings = new nvReadableStack ();\r
- try {\r
- settings.push ( new nvPropertiesFile ( settingStream ) );\r
- settings.push ( new nvReadableTable ( argMap ) );\r
- } catch (loadException e1) {\r
- // TODO Auto-generated catch block\r
- e1.printStackTrace();\r
- }\r
- \r
- \r
- RestfulCollectorServlet rsv = null;\r
- NsaAuthenticator<NsaSimpleApiKey> NsaAuth = null;\r
- Boolean flag = false;\r
- try\r
- {\r
- rsv = new RestfulCollectorServlet(settings);\r
- }\r
- catch (NullPointerException|loadException| missingReqdSetting e){\r
- System.out.println("junit reported:" + e.getMessage());\r
- }\r
- String authlist = "secureid,IWRjYWVSb2FkbTEyMyEt|sample1,c2FtcGxlMQ==|vdnsagg,dmRuc2FnZw==";\r
- NsaAuth = rsv.AuthlistHandler(authlist);\r
- if (NsaAuth != null)\r
- {\r
- flag = true;\r
- }\r
- Assert.assertEquals(true, flag);\r
- \r
-\r
- }\r
-}\r
-\r
-\r