Delete temp file in FileWatcher test
[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.junit.Assert.assertThat;
26
27 import java.io.File;
28 import java.io.IOException;
29 import java.util.TimerTask;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.onap.aai.auth.AAIAuthException;
34 import org.onap.aai.auth.AAIMicroServiceAuth;
35 import org.onap.aai.auth.AAIMicroServiceAuthCore;
36 import org.onap.aai.auth.AuthFileWatcher;
37 import org.onap.aai.babel.config.BabelAuthConfig;
38
39 /**
40  * Tests {@link AuthFileWatcher}.
41  */
42
43 public class TestAuthFileWatcher {
44
45     private TimerTask task;
46     private File mockFile = Mockito.mock(File.class);
47
48     @Before
49     public void createTask() {
50         task = new AuthFileWatcher(mockFile);
51     }
52
53     @Test
54     public void testOnChangeDoesNotRun() {
55         task.run();
56     }
57
58     @Test
59     public void testOnChangeDoesRun() throws IOException, AAIAuthException {
60         System.setProperty("CONFIG_HOME", "src/test/resources");
61         BabelAuthConfig babelServiceAuthConfig = new BabelAuthConfig();
62         babelServiceAuthConfig.setAuthPolicyFile("auth_policy.json");
63         new AAIMicroServiceAuth(babelServiceAuthConfig);
64
65         Mockito.when(mockFile.lastModified()).thenReturn(1000L);
66         task.run();
67     }
68
69     @Test
70     public void testOnChangeRunAfterFailure() throws IOException {
71         File file = File.createTempFile("auth-policy", "json");
72         try {
73             AAIMicroServiceAuthCore.init(file.getAbsolutePath());
74         } catch (AAIAuthException e) {
75             assertThat(e.getMessage(), containsString("Error processing Auth policy file"));
76         }
77         file.deleteOnExit();
78         Mockito.when(mockFile.lastModified()).thenReturn(1000L);
79         task.run();
80     }
81
82 }