2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.oauth.test;
24 import static org.hamcrest.CoreMatchers.is;
25 import static org.mockito.Mockito.when;
26 import static org.junit.Assert.assertThat;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
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;
39 import aafoauth.v2_0.Token;
41 public class JU_TimedToken {
43 private static final byte[] hash = "hashstring".getBytes();
45 private static final int expires = 10000;
49 @Mock private Persist<Token, ?> persistMock;
50 @Mock private Token tokenMock;
53 public void setup() throws IOException {
54 MockitoAnnotations.initMocks(this);
56 when(tokenMock.getExpiresIn()).thenReturn(expires);
57 path = Files.createTempFile("fake", ".txt");
62 int actuallyExpires = ((int)(System.currentTimeMillis() / 1000)) + expires;
63 TimedToken ttoken = new TimedToken(persistMock, tokenMock, hash, path);
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));
72 assertThat(ttoken.match(hash), is(true));
73 assertThat(ttoken.getHash(), is(hash));
75 assertThat(ttoken.path(), is(path));
77 assertThat(ttoken.count(), is(0));
79 assertThat(ttoken.count(), is(1));
81 assertThat(ttoken.count(), is(0));