Add tests for uncovered FileWatcher code
Move the onChange() method so that it can be tested.
Change-Id: I8a011ae581a16cf31e9573e6d4c9b2fa1c858b8a
Issue-ID: AAI-2280
Signed-off-by: mark.j.leonard <mark.j.leonard@gmail.com>
diff --git a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
index 0eec7e1..bdb3bc7 100644
--- a/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
+++ b/src/main/java/org/onap/aai/auth/AAIMicroServiceAuthCore.java
@@ -91,19 +91,8 @@
}
AAIMicroServiceAuthCore.reloadUsers();
- TimerTask task = new FileWatcher(new File(policyAuthFileName)) {
- @Override
- protected void onChange(File file) {
- // here we implement the onChange
- applicationLogger.debug("File " + file.getName() + " has been changed!");
- try {
- AAIMicroServiceAuthCore.reloadUsers();
- } catch (AAIAuthException e) {
- applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
- }
- applicationLogger.debug("File " + file.getName() + " has been reloaded!");
- }
- };
+
+ TimerTask task = new AuthFileWatcher(new File(policyAuthFileName));
if (!timerSet) {
timerSet = true;
@@ -130,7 +119,7 @@
* <li>If this fails, try resolving the path relative to the configuration home location (either
* <code>$CONFIG_HOME</code> or <code>$APP_HOME/appconfig</code>).</li>
* <li>If this fails try resolving relative to the <code>auth</code> folder under configuration home.</li>
- *
+ *
* @param authPolicyFile
* filename or path
* @return the Optional canonical path to the located policy file
diff --git a/src/main/java/org/onap/aai/auth/AuthFileWatcher.java b/src/main/java/org/onap/aai/auth/AuthFileWatcher.java
new file mode 100644
index 0000000..07b522d
--- /dev/null
+++ b/src/main/java/org/onap/aai/auth/AuthFileWatcher.java
@@ -0,0 +1,46 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright © 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright © 2017-2019 European Software Marketing Ltd.
+ * ================================================================================
+ * 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.aai.auth;
+
+import java.io.File;
+import org.onap.aai.babel.logging.ApplicationMsgs;
+import org.onap.aai.babel.logging.LogHelper;
+
+public class AuthFileWatcher extends FileWatcher {
+
+ private static LogHelper applicationLogger = LogHelper.INSTANCE;
+
+ public AuthFileWatcher(File file) {
+ super(file);
+ }
+
+ @Override
+ protected void onChange(File file) {
+ applicationLogger.debug("File " + file.getName() + " has been changed!");
+ try {
+ AAIMicroServiceAuthCore.reloadUsers();
+ } catch (AAIAuthException e) {
+ applicationLogger.error(ApplicationMsgs.PROCESS_REQUEST_ERROR, e);
+ }
+ applicationLogger.debug("File " + file.getName() + " has been reloaded!");
+ }
+}
diff --git a/src/test/java/org/onap/aai/babel/TestAuthFileWatcher.java b/src/test/java/org/onap/aai/babel/TestAuthFileWatcher.java
new file mode 100644
index 0000000..e213e15
--- /dev/null
+++ b/src/test/java/org/onap/aai/babel/TestAuthFileWatcher.java
@@ -0,0 +1,81 @@
+/**
+ * ============LICENSE_START=======================================================
+ * org.onap.aai
+ * ================================================================================
+ * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (c) 2017-2019 European Software Marketing Ltd.
+ * ================================================================================
+ * 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.aai.babel;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.TimerTask;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.onap.aai.auth.AAIAuthException;
+import org.onap.aai.auth.AAIMicroServiceAuth;
+import org.onap.aai.auth.AAIMicroServiceAuthCore;
+import org.onap.aai.auth.AuthFileWatcher;
+import org.onap.aai.babel.config.BabelAuthConfig;
+
+/**
+ * Tests {@link AuthFileWatcher}.
+ */
+
+public class TestAuthFileWatcher {
+
+ private TimerTask task;
+ private File mockFile = Mockito.mock(File.class);
+
+ @Before
+ public void createTask() {
+ task = new AuthFileWatcher(mockFile);
+ }
+
+ @Test
+ public void testOnChangeDoesNotRun() {
+ task.run();
+ }
+
+ @Test
+ public void testOnChangeDoesRun() throws IOException, AAIAuthException {
+ System.setProperty("CONFIG_HOME", "src/test/resources");
+ BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
+ babelServiceAuthConfig.setAuthPolicyFile("auth_policy.json");
+ new AAIMicroServiceAuth(babelServiceAuthConfig);
+
+ Mockito.when(mockFile.lastModified()).thenReturn(1000L);
+ task.run();
+ }
+
+ @Test
+ public void testOnChangeRunAfterFailure() throws IOException {
+ File file = File.createTempFile("auth-policy", "json");
+ try {
+ AAIMicroServiceAuthCore.init(file.getAbsolutePath());
+ } catch (AAIAuthException e) {
+ assertThat(e.getMessage(), containsString("Error processing Auth policy file"));
+ }
+ Mockito.when(mockFile.lastModified()).thenReturn(1000L);
+ task.run();
+ }
+
+}