Update aai-parent in babel to 1.13.3
[aai/babel.git] / src / test / java / org / onap / aai / babel / TestAuthFileWatcher.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright (c) 2017-2019 AT&T Intellectual Property. All rights reserved.
6  * Copyright (c) 2017-2019 European Software Marketing Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.aai.babel;
23
24 import static org.hamcrest.CoreMatchers.containsString;
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.util.TimerTask;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.mockito.Mockito;
34 import org.onap.aai.auth.AAIAuthException;
35 import org.onap.aai.auth.AAIMicroServiceAuth;
36 import org.onap.aai.auth.AAIMicroServiceAuthCore;
37 import org.onap.aai.auth.AuthFileWatcher;
38 import org.onap.aai.babel.config.BabelAuthConfig;
39
40 /**
41  * Tests {@link AuthFileWatcher}.
42  */
43
44 public class TestAuthFileWatcher {
45
46     private TimerTask task;
47     private File mockFile = Mockito.mock(File.class);
48
49     @BeforeEach
50     public void createTask() {
51         task = new AuthFileWatcher(mockFile);
52     }
53
54     @Test
55     public void testOnChangeDoesNotRun() {
56         assertDoesNotThrow(() -> {
57             task.run();
58         });
59     }
60
61     @Test
62     public void testOnChangeDoesRun() throws IOException, AAIAuthException {
63         assertDoesNotThrow(() -> {
64             System.setProperty("CONFIG_HOME", "src/test/resources");
65             BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
66             babelServiceAuthConfig.setAuthPolicyFile("auth_policy.json");
67             new AAIMicroServiceAuth(babelServiceAuthConfig);
68
69             Mockito.when(mockFile.lastModified()).thenReturn(1000L);
70             task.run();
71         });
72     }
73
74     @Test
75     public void testOnChangeRunAfterFailure() throws IOException {
76         File file = File.createTempFile("auth-policy", "json");
77         try {
78             AAIMicroServiceAuthCore.init(file.getAbsolutePath());
79         } catch (AAIAuthException e) {
80             assertThat(e.getMessage(), containsString("Error processing Auth policy file"));
81         }
82         file.deleteOnExit();
83         Mockito.when(mockFile.lastModified()).thenReturn(1000L);
84         task.run();
85     }
86
87 }