Mass removal of all Tabs (Style Warnings)
[aaf/authz.git] / cadi / aaf / src / test / java / org / onap / aaf / cadi / oauth / test / JU_TimedToken.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.cadi.oauth.test;
23
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.mockito.Mockito.when;
26 import static org.junit.Assert.assertThat;
27
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.aaf.cadi.oauth.TimedToken;
37 import org.onap.aaf.cadi.persist.Persist;
38
39 import aafoauth.v2_0.Token;
40
41 public class JU_TimedToken {
42
43     private static final byte[] hash = "hashstring".getBytes();
44
45     private static final int expires = 10000;
46
47     private Path path;
48
49     @Mock private Persist<Token, ?> persistMock;
50     @Mock private Token tokenMock;
51
52     @Before
53     public void setup() throws IOException {
54         MockitoAnnotations.initMocks(this);
55
56         when(tokenMock.getExpiresIn()).thenReturn(expires);
57         path = Files.createTempFile("fake", ".txt");
58     }
59
60     @Test
61     public void test() {
62         int actuallyExpires = ((int)(System.currentTimeMillis() / 1000)) + expires;
63         TimedToken ttoken = new TimedToken(persistMock, tokenMock, hash, path);
64
65         assertThat(ttoken.get(), is(tokenMock));
66         assertThat(ttoken.checkSyncTime(), is(true));
67         assertThat(ttoken.checkReloadable(), is(false));
68         assertThat(ttoken.hasBeenTouched(), is(false));
69         assertThat(Math.abs(ttoken.expires() - actuallyExpires) < 10, is(true));
70         assertThat(ttoken.expired(), is(false));
71
72         assertThat(ttoken.match(hash), is(true));
73         assertThat(ttoken.getHash(), is(hash));
74
75         assertThat(ttoken.path(), is(path));
76
77         assertThat(ttoken.count(), is(0));
78         ttoken.inc();
79         assertThat(ttoken.count(), is(1));
80         ttoken.clearCount();
81         assertThat(ttoken.count(), is(0));
82     }
83
84 }