diff options
-rwxr-xr-x | datarouter-subscriber/pom.xml | 7 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java | 190 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberMain.java (renamed from datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/Subscriber.java) | 50 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberProps.java | 63 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberServlet.java | 168 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/resources/docker/startup.sh | 2 | ||||
-rw-r--r-- | datarouter-subscriber/src/main/resources/subscriber.properties | 2 | ||||
-rwxr-xr-x | datarouter-subscriber/src/test/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServletTest.java | 107 | ||||
-rw-r--r-- | datarouter-subscriber/src/test/resources/log4j.properties | 31 | ||||
-rw-r--r-- | datarouter-subscriber/src/test/resources/testsubscriber.properties | 31 |
10 files changed, 444 insertions, 207 deletions
diff --git a/datarouter-subscriber/pom.xml b/datarouter-subscriber/pom.xml index 52cb25c7..a7a31513 100755 --- a/datarouter-subscriber/pom.xml +++ b/datarouter-subscriber/pom.xml @@ -119,6 +119,11 @@ <version>1.2.17</version> <scope>compile</scope> </dependency> + <dependency> + <groupId>org.apache.commons</groupId> + <artifactId>commons-io</artifactId> + <version>1.3.2</version> + </dependency> </dependencies> <profiles> <profile> @@ -210,7 +215,7 @@ <archive> <manifest> <addClasspath>true</addClasspath> - <mainClass>org.onap.dmaap.datarouter.subscriber.Subscriber</mainClass> + <mainClass>org.onap.dmaap.datarouter.subscriber.SubscriberMain</mainClass> </manifest> </archive> </configuration> diff --git a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java new file mode 100644 index 00000000..58bc4c40 --- /dev/null +++ b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServlet.java @@ -0,0 +1,190 @@ +/******************************************************************************* + * ============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.subscriber; + +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; + +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; + +public class SampleSubscriberServlet extends HttpServlet { + + private static Logger logger = + Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SampleSubscriberServlet"); + private String outputDirectory; + private String basicAuth; + + /** + * Configure the SampleSubscriberServlet. + * + * <ul> + * <li>Login - The login expected in the Authorization header (default "LOGIN"). + * <li>Password - The password expected in the Authorization header (default "PASSWORD"). + * <li>outputDirectory - The directory where files are placed (default + * "/opt/app/subscriber/delivery"). + * </ul> + */ + @Override + public void init() { + SubscriberProps props = SubscriberProps.getInstance(); + String login = props.getValue("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN"); + String password = + props.getValue("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD"); + outputDirectory = + props.getValue( + "org.onap.dmaap.datarouter.subscriber.delivery.dir", "/opt/app/subscriber/delivery"); + try { + Files.createDirectory(Paths.get(outputDirectory)); + } catch (IOException e) { + logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage()); + } + basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes()); + } + + @Override + protected void doPut(HttpServletRequest req, HttpServletResponse resp) { + try { + common(req, resp, false); + } catch (IOException e) { + logger.info( + "SampleSubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(), + e); + } + } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { + try { + common(req, resp, true); + } catch (IOException e) { + logger.info( + "SampleSubServlet: Failed to doDelete: " + + req.getRemoteAddr() + + " : " + + req.getPathInfo(), + e); + } + } + /** + * Process a PUT or DELETE request. + * + * <ol> + * <li>Verify that the request contains an Authorization header or else UNAUTHORIZED. + * <li>Verify that the Authorization header matches the configured Login and Password or else + * FORBIDDEN. + * <li>If the request is PUT, store the message body as a file in the configured outputDirectory + * directory protecting against evil characters in the received FileID. The file is created + * initially with its name prefixed with a ".", and once it is complete, it is renamed to + * remove the leading "." character. + * <li>If the request is DELETE, instead delete the file (if it exists) from the configured + * outputDirectory directory. + * <li>Respond with NO_CONTENT. + * </ol> + */ + private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete) + throws IOException { + String authHeader = req.getHeader("Authorization"); + if (authHeader == null) { + logger.info( + "SampleSubServlet: Rejecting request with no Authorization header from " + + req.getRemoteAddr() + + ": " + + req.getPathInfo()); + resp.sendError(HttpServletResponse.SC_UNAUTHORIZED); + return; + } + if (!basicAuth.equals(authHeader)) { + logger.info( + "SampleSubServlet: Rejecting request with incorrect Authorization header from " + + req.getRemoteAddr() + + ": " + + req.getPathInfo()); + resp.sendError(HttpServletResponse.SC_FORBIDDEN); + return; + } + String fileid = req.getPathInfo(); + fileid = fileid.substring(fileid.lastIndexOf('/') + 1); + String queryString = req.getQueryString(); + if (queryString != null) { + fileid = fileid + "?" + queryString; + } + String publishid = req.getHeader("X-ATT-DR-PUBLISH-ID"); + String filename = + URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A"); + String fullPath = outputDirectory + "/" + filename; + String tmpPath = outputDirectory + "/." + filename; + try { + if (isdelete) { + Files.deleteIfExists(Paths.get(fullPath)); + logger.info( + "SampleSubServlet: Received delete for file id " + + fileid + + " from " + + req.getRemoteAddr() + + " publish id " + + publishid + + " as " + + fullPath); + } else { + new File(tmpPath).createNewFile(); + try (InputStream is = req.getInputStream(); + OutputStream os = new FileOutputStream(tmpPath)) { + byte[] buf = new byte[65536]; + int i; + while ((i = is.read(buf)) > 0) { + os.write(buf, 0, i); + } + } + Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING); + logger.info( + "SampleSubServlet: Received file id " + + fileid + + " from " + + req.getRemoteAddr() + + " publish id " + + publishid + + " as " + + fullPath); + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + } + resp.setStatus(HttpServletResponse.SC_NO_CONTENT); + } catch (IOException ioe) { + Files.deleteIfExists(Paths.get(tmpPath)); + logger.info( + "SampleSubServlet: Failed to process file " + + fullPath + + " from " + + req.getRemoteAddr() + + ": " + + req.getPathInfo()); + throw ioe; + } + } +} diff --git a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/Subscriber.java b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberMain.java index b6edb670..bbe5e325 100644 --- a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/Subscriber.java +++ b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberMain.java @@ -24,45 +24,23 @@ package org.onap.dmaap.datarouter.subscriber; import org.apache.log4j.Logger; -import org.eclipse.jetty.servlet.*; -import org.eclipse.jetty.util.ssl.*; -import org.eclipse.jetty.server.*; import org.eclipse.jetty.http.HttpVersion; +import org.eclipse.jetty.server.*; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.servlet.ServletHolder; +import org.eclipse.jetty.util.ssl.SslContextFactory; -import java.io.FileInputStream; -import java.io.IOException; import java.util.Arrays; -import java.util.Properties; - -public class Subscriber { - private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.subscriber.Subscriber"); +public class SubscriberMain { - private static final String CONTEXT_PATH = "/"; - private static final String URL_PATTERN = "/*"; - - static Properties props; - - private static void loadProps() { - if (props == null) { - props = new Properties(); - try { - props.load(new FileInputStream(System.getProperty( - "org.onap.dmaap.datarouter.subscriber.properties", - "/opt/app/subscriber/etc/subscriber.properties"))); - } catch (IOException e) { - logger.fatal("SubServlet: Exception opening properties: " + e.getMessage()); - System.exit(1); - } - } - } + private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SubscriberMain"); public static void main(String[] args) throws Exception { - //Load the properties - loadProps(); - - int httpsPort = Integer.parseInt(props.getProperty("org.onap.dmaap.datarouter.subscriber.https.port", "8443")); - int httpPort = Integer.parseInt(props.getProperty("org.onap.dmaap.datarouter.subscriber.http.port", "8080")); + SubscriberProps props = SubscriberProps.getInstance( + System.getProperty("org.onap.dmaap.datarouter.subscriber.properties", "subscriber.properties")); + int httpsPort = Integer.parseInt(props.getValue("org.onap.dmaap.datarouter.subscriber.https.port", "8443")); + int httpPort = Integer.parseInt(props.getValue("org.onap.dmaap.datarouter.subscriber.http.port", "8080")); Server server = new Server(); HttpConfiguration httpConfig = new HttpConfiguration(); @@ -91,7 +69,7 @@ public class Subscriber { /*Skip SSLv3 Fixes*/ sslContextFactory.addExcludeProtocols("SSLv3"); - logger.info("Excluded protocols for Subscriber:" + Arrays.toString(sslContextFactory.getExcludeProtocols())); + logger.info("Excluded protocols for SubscriberMain:" + Arrays.toString(sslContextFactory.getExcludeProtocols())); /*End of SSLv3 Fixes*/ // HTTPS Configuration @@ -104,17 +82,17 @@ public class Subscriber { server.setConnectors(new Connector[]{ httpServerConnector }); } ctxt = new ServletContextHandler(0); - ctxt.setContextPath(CONTEXT_PATH); + ctxt.setContextPath("/"); server.setHandler(ctxt); - ctxt.addServlet(new ServletHolder(new SubscriberServlet()), URL_PATTERN); + ctxt.addServlet(new ServletHolder(new SampleSubscriberServlet()), "/*"); try { server.start(); } catch ( Exception e ) { logger.info("Jetty failed to start. Reporting will be unavailable-"+e); } server.join(); - logger.info("org.onap.dmaap.datarouter.subscriber.Subscriber started-"+ server.getState()); + logger.info("org.onap.dmaap.datarouter.subscriber.SubscriberMain started-"+ server.getState()); } }
\ No newline at end of file diff --git a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberProps.java b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberProps.java new file mode 100644 index 00000000..39ab166b --- /dev/null +++ b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberProps.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * ============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.subscriber; + +import java.io.IOException; +import java.util.Properties; + +public class SubscriberProps { + + private static SubscriberProps instance = null; + private Properties properties; + + private SubscriberProps(String propsPath) throws IOException{ + properties = new Properties(); + properties.load(getClass().getClassLoader().getResourceAsStream(propsPath)); + + } + + public static SubscriberProps getInstance(String propsPath) { + if(instance == null) { + try { + instance = new SubscriberProps(propsPath); + } catch (IOException ioe) { + ioe.printStackTrace(); + } + } + return instance; + } + + public static SubscriberProps getInstance() { + return instance; + } + + public String getValue(String key) { + return properties.getProperty(key); + } + + public String getValue(String key, String defaultValue) { + return properties.getProperty(key, defaultValue); + } + +}
\ No newline at end of file diff --git a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberServlet.java b/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberServlet.java deleted file mode 100644 index 72afcf06..00000000 --- a/datarouter-subscriber/src/main/java/org/onap/dmaap/datarouter/subscriber/SubscriberServlet.java +++ /dev/null @@ -1,168 +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 org.onap.dmaap.datarouter.subscriber; - -import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; - -import javax.servlet.ServletConfig; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.io.*; -import java.net.URLEncoder; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.StandardCopyOption; -import java.nio.file.attribute.PosixFilePermissions; - -import static org.onap.dmaap.datarouter.subscriber.Subscriber.props; - -public class SubscriberServlet extends HttpServlet { - - private static Logger logger = Logger.getLogger("org.onap.dmaap.datarouter.subscriber.SubscriberServlet"); - private String outputDirectory; - private String basicAuth; - - /** - * Configure this subscriberservlet. Configuration parameters from config.getInitParameter() are: - * <ul> - * <li>Login - The login expected in the Authorization header (default "LOGIN"). - * <li>Password - The password expected in the Authorization header (default "PASSWORD"). - * <li>outputDirectory - The directory where files are placed (default "tmp"). - * </ul> - */ - @Override - public void init(ServletConfig config) { - String login = props.getProperty("org.onap.dmaap.datarouter.subscriber.auth.user", "LOGIN"); - String password = props.getProperty("org.onap.dmaap.datarouter.subscriber.auth.password", "PASSWORD"); - outputDirectory = props.getProperty("org.onap.dmaap.datarouter.subscriber.delivery.dir", "/tmp"); - try { - Files.createDirectory(Paths.get(outputDirectory), PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwxrwxrwx"))); - } catch (IOException e) { - logger.info("SubServlet: Failed to create delivery dir: " + e.getMessage()); - e.printStackTrace(); - } - basicAuth = "Basic " + Base64.encodeBase64String((login + ":" + password).getBytes()); - } - - @Override - protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { - File filesPath = new File(outputDirectory); - File[] filesArr = filesPath.listFiles(); - assert filesArr != null; - for (File file: filesArr) { - try (BufferedReader in = new BufferedReader(new FileReader(file))) { - String line = in.readLine(); - while (line != null) { - line = in.readLine(); - } - } - } - } - /** - * Invoke common(req, resp, false). - */ - @Override - protected void doPut(HttpServletRequest req, HttpServletResponse resp) { - try { - common(req, resp, false); - } catch (IOException e) { - logger.info("SubServlet: Failed to doPut: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e); - } - } - /** - * Invoke common(req, resp, true). - */ - @Override - protected void doDelete(HttpServletRequest req, HttpServletResponse resp) { - try { - common(req, resp, true); - } catch (IOException e) { - logger.info("SubServlet: Failed to doDelete: " + req.getRemoteAddr() + " : " + req.getPathInfo(), e); - } - } - /** - * Process a PUT or DELETE request. - * <ol> - * <li>Verify that the request contains an Authorization header - * or else UNAUTHORIZED. - * <li>Verify that the Authorization header matches the configured - * Login and Password or else FORBIDDEN. - * <li>If the request is PUT, store the message body as a file - * in the configured outputDirectory directory protecting against - * evil characters in the received FileID. The file is created - * initially with its name prefixed with a ".", and once it is complete, it is - * renamed to remove the leading "." character. - * <li>If the request is DELETE, instead delete the file (if it exists) from the configured outputDirectory directory. - * <li>Respond with NO_CONTENT. - * </ol> - */ - private void common(HttpServletRequest req, HttpServletResponse resp, boolean isdelete) throws IOException { - String authHeader = req.getHeader("Authorization"); - if (authHeader == null) { - logger.info("Rejecting request with no Authorization header from " + req.getRemoteAddr() + ": " + req.getPathInfo()); - resp.sendError(HttpServletResponse.SC_UNAUTHORIZED); - return; - } - if (!basicAuth.equals(authHeader)) { - logger.info("Rejecting request with incorrect Authorization header from " + req.getRemoteAddr() + ": " + req.getPathInfo()); - resp.sendError(HttpServletResponse.SC_FORBIDDEN); - return; - } - String fileid = req.getPathInfo(); - fileid = fileid.substring(fileid.lastIndexOf('/') + 1); - String queryString = req.getQueryString(); - if (queryString != null) { - fileid = fileid + "?" + queryString; - } - String publishid = req.getHeader("X-ATT-DR-PUBLISH-ID"); - String filename = URLEncoder.encode(fileid, "UTF-8").replaceAll("^\\.", "%2E").replaceAll("\\*", "%2A"); - String fullPath = outputDirectory + "/" + filename; - String tmpPath = outputDirectory + "/." + filename; - try { - if (isdelete) { - Files.deleteIfExists(Paths.get(fullPath)); - logger.info("Received delete for file id " + fileid + " from " + req.getRemoteAddr() + " publish id " + publishid + " as " + fullPath); - } else { - new File(tmpPath).createNewFile(); - try (InputStream is = req.getInputStream(); OutputStream os = new FileOutputStream(tmpPath)) { - byte[] buf = new byte[65536]; - int i; - while ((i = is.read(buf)) > 0) { - os.write(buf, 0, i); - } - } - Files.move(Paths.get(tmpPath), Paths.get(fullPath), StandardCopyOption.REPLACE_EXISTING); - logger.info("Received file id " + fileid + " from " + req.getRemoteAddr() + " publish id " + publishid + " as " + fullPath); - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - } - resp.setStatus(HttpServletResponse.SC_NO_CONTENT); - } catch (IOException ioe) { - Files.deleteIfExists(Paths.get(tmpPath)); - logger.info("Failed to process file " + fullPath + " from " + req.getRemoteAddr() + ": " + req.getPathInfo()); - throw ioe; - } - } -} diff --git a/datarouter-subscriber/src/main/resources/docker/startup.sh b/datarouter-subscriber/src/main/resources/docker/startup.sh index 53b1053d..fb5610d7 100644 --- a/datarouter-subscriber/src/main/resources/docker/startup.sh +++ b/datarouter-subscriber/src/main/resources/docker/startup.sh @@ -5,7 +5,7 @@ CLASSPATH=$ETC for FILE in `find $LIB -name *.jar`; do CLASSPATH=$CLASSPATH:$FILE done -java -classpath $CLASSPATH org.onap.dmaap.datarouter.subscriber.Subscriber +java -classpath $CLASSPATH org.onap.dmaap.datarouter.subscriber.SubscriberMain runner_file="$LIB/subscriber-jar-with-dependencies.jar" echo "Starting using" $runner_file diff --git a/datarouter-subscriber/src/main/resources/subscriber.properties b/datarouter-subscriber/src/main/resources/subscriber.properties index 771fdd34..ed3237b4 100644 --- a/datarouter-subscriber/src/main/resources/subscriber.properties +++ b/datarouter-subscriber/src/main/resources/subscriber.properties @@ -21,7 +21,7 @@ # * #------------------------------------------------------------------------------- -#Subscriber properties +#SubscriberMain properties org.onap.dmaap.datarouter.subscriber.http.port = 7070 org.onap.dmaap.datarouter.subscriber.https.port = 7443 org.onap.dmaap.datarouter.subscriber.auth.user = LOGIN diff --git a/datarouter-subscriber/src/test/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServletTest.java b/datarouter-subscriber/src/test/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServletTest.java new file mode 100755 index 00000000..e31b3473 --- /dev/null +++ b/datarouter-subscriber/src/test/java/org/onap/dmaap/datarouter/subscriber/SampleSubscriberServletTest.java @@ -0,0 +1,107 @@ +/******************************************************************************* + * ============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.subscriber; + +import org.apache.commons.io.FileUtils; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.powermock.modules.junit4.PowerMockRunner; + +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; + +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.*; + +@RunWith(PowerMockRunner.class) +public class SampleSubscriberServletTest { + + private SampleSubscriberServlet sampleSubServlet; + private SubscriberProps props = SubscriberProps.getInstance(); + + @Mock private HttpServletRequest request; + @Mock private HttpServletResponse response; + + @Before + public void setUp() { + props = + SubscriberProps.getInstance( + System.getProperty( + "org.onap.dmaap.datarouter.subscriber.properties", "testsubscriber.properties")); + sampleSubServlet = new SampleSubscriberServlet(); + sampleSubServlet.init(); + } + + @After + public void tearDown() throws Exception { + FileUtils.deleteDirectory( + new File(props.getValue("org.onap.dmaap.datarouter.subscriber.delivery.dir"))); + } + + @Test + public void + Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Null_Then_Unathorized_Response_Is_Generated() + throws Exception { + when(request.getHeader("Authorization")).thenReturn(null); + sampleSubServlet.doPut(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED)); + } + + @Test + public void + Given_Request_Is_HTTP_PUT_And_Request_Header_Is_Not_Authorized_Then_Forbidden_Response_Is_Generated() + throws Exception { + when(request.getHeader("Authorization")).thenReturn("Invalid Header"); + sampleSubServlet.doPut(request, response); + verify(response).sendError(eq(HttpServletResponse.SC_FORBIDDEN)); + } + + @Test + public void Given_Request_Is_HTTP_PUT_Then_Request_Succeeds() throws Exception { + setUpSuccessfulFlow(); + sampleSubServlet.doPut(request, response); + verify(response, times(2)).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); + } + + @Test + public void Given_Request_Is_HTTP_DELETE_Then_Request_Succeeds() throws Exception { + setUpSuccessfulFlow(); + sampleSubServlet.doDelete(request, response); + verify(response).setStatus(eq(HttpServletResponse.SC_NO_CONTENT)); + } + + private void setUpSuccessfulFlow() throws IOException { + when(request.getHeader("Authorization")).thenReturn("Basic TE9HSU46UEFTU1dPUkQ="); + when(request.getPathInfo()).thenReturn("/publish/1/testfile"); + when(request.getHeader("X-ATT-DR-PUBLISH-ID")).thenReturn("1"); + when(request.getQueryString()).thenReturn(null); + ServletInputStream inStream = mock(ServletInputStream.class); + when(request.getInputStream()).thenReturn(inStream); + } +} diff --git a/datarouter-subscriber/src/test/resources/log4j.properties b/datarouter-subscriber/src/test/resources/log4j.properties new file mode 100644 index 00000000..b8d349e6 --- /dev/null +++ b/datarouter-subscriber/src/test/resources/log4j.properties @@ -0,0 +1,31 @@ +#------------------------------------------------------------------------------- +# ============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. +# * +#------------------------------------------------------------------------------- + +log4j.rootLogger=info,Root + +log4j.appender.Root=org.apache.log4j.DailyRollingFileAppender +log4j.appender.Root.file=./logs/subscriber.log +log4j.appender.Root.datePattern='.'yyyyMMdd +log4j.appender.Root.append=true +log4j.appender.Root.layout=org.apache.log4j.PatternLayout +log4j.appender.Root.layout.ConversionPattern=%d %p %t %m%n diff --git a/datarouter-subscriber/src/test/resources/testsubscriber.properties b/datarouter-subscriber/src/test/resources/testsubscriber.properties new file mode 100644 index 00000000..2bdd3629 --- /dev/null +++ b/datarouter-subscriber/src/test/resources/testsubscriber.properties @@ -0,0 +1,31 @@ +#------------------------------------------------------------------------------- +# ============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. +# * +#------------------------------------------------------------------------------- + +#SubscriberMain test properties +org.onap.dmaap.datarouter.subscriber.http.port = 7070 +org.onap.dmaap.datarouter.subscriber.https.port = 7443 +org.onap.dmaap.datarouter.subscriber.auth.user = LOGIN +org.onap.dmaap.datarouter.subscriber.auth.password = PASSWORD +org.onap.dmaap.datarouter.subscriber.delivery.dir = tmp + + |