Github user mdunker commented on a diff in the pull request:

    https://github.com/apache/usergrid/pull/573#discussion_r122740756
  
    --- Diff: 
stack/corepersistence/token/src/test/java/org/apache/usergrid/persistence/token/TokenSerializationTest.java
 ---
    @@ -0,0 +1,202 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +
    +package org.apache.usergrid.persistence.token;
    +
    +import net.jcip.annotations.NotThreadSafe;
    +import org.apache.usergrid.persistence.model.util.UUIDGenerator;
    +import org.apache.usergrid.persistence.token.impl.TokenSerializationImpl;
    +import org.junit.Rule;
    +import org.junit.Test;
    +import org.junit.runner.RunWith;
    +
    +import org.apache.usergrid.persistence.core.guice.MigrationManagerRule;
    +import org.apache.usergrid.persistence.core.test.ITRunner;
    +import org.apache.usergrid.persistence.core.test.UseModules;
    +
    +import com.google.inject.Inject;
    +
    +import java.nio.ByteBuffer;
    +import java.util.*;
    +
    +import static org.junit.Assert.assertEquals;
    +
    +
    +@RunWith( ITRunner.class )
    +@UseModules( { TestTokenModule.class } )
    +@NotThreadSafe
    +public class TokenSerializationTest {
    +
    +
    +    @Inject
    +    @Rule
    +    public MigrationManagerRule migrationManagerRule;
    +
    +    @Inject
    +    public TokenSerialization tokenSerialization;
    +
    +    @Test
    +    public void putTokenInfo() {
    +
    +        UUID uuid = UUIDGenerator.newTimeUUID();
    +        Map<String, Object> tokenDetails = new HashMap<>();
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token");
    +
    +        ByteBuffer principalKeyBuffer = 
ByteBuffer.wrap("test-principal".getBytes());
    +
    +        tokenSerialization.putTokenInfo(uuid, tokenDetails, 
principalKeyBuffer, 60);
    +
    +    }
    +
    +    @Test
    +    public void getTokenInfo() {
    +
    +        UUID uuid = UUIDGenerator.newTimeUUID();
    +        Map<String, Object> tokenDetails = new HashMap<>();
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token");
    +
    +        ByteBuffer principalKeyBuffer = 
ByteBuffer.wrap("test-principal".getBytes());
    +
    +        tokenSerialization.putTokenInfo(uuid, tokenDetails, 
principalKeyBuffer, 60);
    +
    +        Map<String, Object> returnedDetails = 
tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE),
    +            returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE)
    +        );
    +    }
    +
    +    @Test
    +    public void updateAccessTokenTime() {
    +
    +        UUID uuid = UUIDGenerator.newTimeUUID();
    +        Map<String, Object> tokenDetails = new HashMap<>();
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token");
    +
    +        long accessedTime = System.currentTimeMillis()-1000000;
    +        long inactiveTime = accessedTime+2000000;
    +
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_ACCESSED, 
accessedTime);
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_INACTIVE, 
inactiveTime);
    +
    +        ByteBuffer principalKeyBuffer = 
ByteBuffer.wrap("test-principal".getBytes());
    +
    +        tokenSerialization.putTokenInfo(uuid, tokenDetails, 
principalKeyBuffer, 60);
    +
    +        Map<String, Object> returnedDetails = 
tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            accessedTime,
    +            returnedDetails.get(TokenSerializationImpl.TOKEN_ACCESSED)
    +        );
    +
    +        long newAccessedTime = System.currentTimeMillis();
    +        long newInactiveTime = newAccessedTime+1000000;
    +
    +        tokenSerialization.updateTokenAccessTime(uuid, 
newAccessedTime,newInactiveTime, 1200);
    +
    +        returnedDetails = tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            newAccessedTime,
    +            returnedDetails.get(TokenSerializationImpl.TOKEN_ACCESSED)
    +        );
    +    }
    +
    +    @Test
    +    public void deleteTokens() {
    +
    +        UUID uuid = UUIDGenerator.newTimeUUID();
    +        Map<String, Object> tokenDetails = new HashMap<>();
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token");
    +
    +        ByteBuffer principalKeyBuffer = 
ByteBuffer.wrap("test-principal".getBytes());
    +
    +        tokenSerialization.putTokenInfo(uuid, tokenDetails, 
principalKeyBuffer, 60);
    +
    +        Map<String, Object> returnedDetails = 
tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE),
    +            returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE)
    +        );
    +
    +        tokenSerialization.deleteTokens(Collections.singletonList(uuid), 
principalKeyBuffer);
    +
    +        returnedDetails = tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            0,
    +            returnedDetails.size()
    +        );
    +    }
    +
    +    @Test
    +    public void revokeToken() {
    +
    +        UUID uuid = UUIDGenerator.newTimeUUID();
    +        Map<String, Object> tokenDetails = new HashMap<>();
    +        tokenDetails.put(TokenSerializationImpl.TOKEN_TYPE, "test-token");
    +
    +        ByteBuffer principalKeyBuffer = 
ByteBuffer.wrap("test-principal".getBytes());
    +
    +        tokenSerialization.putTokenInfo(uuid, tokenDetails, 
principalKeyBuffer, 60);
    +
    +        Map<String, Object> returnedDetails = 
tokenSerialization.getTokenInfo(uuid);
    +
    +        assertEquals(
    +            tokenDetails.get(TokenSerializationImpl.TOKEN_TYPE),
    +            returnedDetails.get(TokenSerializationImpl.TOKEN_TYPE)
    +        );
    +
    +        tokenSerialization.revokeToken(uuid, principalKeyBuffer);
    --- End diff --
    
    Do you want to revokeToken with a null principalKeyBuffer too, since that 
is a use case? I assume that if the principal token info has not been deleted 
during the revokeToken, getting tokens for the principal will prune that token? 
Anything to test there?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to