package org.onap.aai.babel;
import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.io.FileWriter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
import org.onap.aai.auth.AAIAuthException;
import org.onap.aai.auth.AAIMicroServiceAuth;
import org.onap.aai.auth.AAIMicroServiceAuthCore;
private static final String VALID_ADMIN_USER = "cn=common-name, ou=org-unit, o=org, l=location, st=state, c=us";
private static final String TEST_POLICY_FILE = "auth_policy.json";
- @Before
+ @BeforeEach
public void setup() {
System.setProperty("CONFIG_HOME", "src/test/resources");
}
* @throws AAIAuthException
* if the Auth policy file cannot be loaded
*/
- @Test(expected = AAIAuthException.class)
+ @Test
public void missingPolicyFile() throws AAIAuthException {
- String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
- try {
- AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
- BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
- babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
- new AAIMicroServiceAuth(babelServiceAuthConfig);
- } finally {
- AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
- }
+ assertThrows(AAIAuthException.class, () -> {
+ String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
+ try {
+ AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
+ BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
+ babelServiceAuthConfig.setAuthPolicyFile("invalid.file.name");
+ new AAIMicroServiceAuth(babelServiceAuthConfig);
+ } finally {
+ AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
+ }
+ });
}
/**
* @throws AAIAuthException
* if the Auth policy file cannot be loaded
*/
- @Test(expected = AAIAuthException.class)
+ @Test
public void testNullPolicyFile() throws AAIAuthException {
- String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
- try {
- AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
- BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
- babelServiceAuthConfig.setAuthPolicyFile(null);
- new AAIMicroServiceAuth(babelServiceAuthConfig);
- } finally {
- AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
- }
+ assertThrows(AAIAuthException.class, () -> {
+ String defaultFile = AAIMicroServiceAuthCore.getDefaultAuthFileName();
+ try {
+ AAIMicroServiceAuthCore.setDefaultAuthFileName("invalid.default.file");
+ BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
+ babelServiceAuthConfig.setAuthPolicyFile(null);
+ new AAIMicroServiceAuth(babelServiceAuthConfig);
+ } finally {
+ AAIMicroServiceAuthCore.setDefaultAuthFileName(defaultFile);
+ }
+ });
}
/**
* @throws IOException
* for I/O failures, e.g. when creating the temporary auth policy file
*/
- @Test(expected = AAIAuthException.class)
+ @Test
public void testReloadDeletedFile() throws AAIAuthException, JSONException, IOException {
File file = createTestPolicyFile();
AAIMicroServiceAuthCore.init(file.getAbsolutePath());
assertThat(file.delete(), is(true));
- AAIMicroServiceAuthCore.reloadUsers();
+ try {
+ AAIMicroServiceAuthCore.reloadUsers();
+ Assertions.fail("Expected an AAIAuthException to be thrown");
+ } catch (AAIAuthException e) {
+ assertTrue(true);
+ }
}
/**
* @throws IOException
* for I/O failures, e.g. when creating the temporary auth policy file
*/
- @Test(expected = AAIAuthException.class)
+ @Test
public void testReloadInvalidFile() throws AAIAuthException, JSONException, IOException {
- File file = createTestPolicyFile();
- AAIMicroServiceAuthCore.init(file.getAbsolutePath());
- writeToFile(file, "not valid JSON content");
- AAIMicroServiceAuthCore.reloadUsers();
+ assertThrows(AAIAuthException.class, () -> {
+ File file = createTestPolicyFile();
+ AAIMicroServiceAuthCore.init(file.getAbsolutePath());
+ writeToFile(file, "not valid JSON content");
+ AAIMicroServiceAuthCore.reloadUsers();
+ });
}
/**
@Test
public void createLocalAuthFileOnChange()
throws JSONException, AAIAuthException, IOException, InterruptedException {
- File file = createTestPolicyFile();
+ assertDoesNotThrow(() -> {
+ File file = createTestPolicyFile();
- BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
- babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
- new AAIMicroServiceAuth(babelAuthConfig);
+ BabelAuthConfig babelAuthConfig = new BabelAuthConfig();
+ babelAuthConfig.setAuthPolicyFile(file.getAbsolutePath());
+ new AAIMicroServiceAuth(babelAuthConfig);
- // Make changes to the temp file
- writeToFile(file, "");
+ // Make changes to the temp file
+ writeToFile(file, "");
- // Wait for the file to be reloaded
- TimeUnit.SECONDS.sleep(3);
+ // Wait for the file to be reloaded
+ TimeUnit.SECONDS.sleep(3);
- AAIMicroServiceAuthCore.cleanup();
+ AAIMicroServiceAuthCore.cleanup();
+ });
}
/**
*/
@Test
public void createAuthFromDefaultFileAppHome() throws AAIAuthException {
- System.clearProperty("CONFIG_HOME");
- System.setProperty("APP_HOME", "src/test/resources");
- BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
- babelServiceAuthConfig.setAuthPolicyFile("non-existent-file");
- new AAIMicroServiceAuth(babelServiceAuthConfig);
+ assertDoesNotThrow(() -> {
+ System.clearProperty("CONFIG_HOME");
+ System.setProperty("APP_HOME", "src/test/resources");
+ BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
+ babelServiceAuthConfig.setAuthPolicyFile("non-existent-file");
+ new AAIMicroServiceAuth(babelServiceAuthConfig);
+ // The default policy will have been loaded from APP_HOME/appconfig
+ });
// The default policy will have been loaded from APP_HOME/appconfig
}
}
/**
+ * Write String content to a file, flush and close.
+ *
* @param file
+ * the file to write to
* @param text
+ * the String content to write to the file
* @throws IOException
+ * if the file cannot be opened for writing, or an I/O error occurs
*/
private void writeToFile(File file, String text) throws IOException {
FileWriter fileWriter = new FileWriter(file);
}
/**
- * Assert authorisation results for an admin user based on the test policy file.
+ * Assert authorization results for an admin user based on the test policy file.
*
* @param auth
* the Auth Service to test