/*
 * EmbeddedUserServiceTest.java
 *
 * Copyright (C) 2018 [ A Legge Up ]
 *
 * This software may be modified and distributed under the terms
 * of the MIT license.  See the LICENSE file for details.
 */

package com.aleggeup.confagrid.service.impl;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.security.SecureRandom;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import com.aleggeup.confagrid.model.User;
import com.aleggeup.confagrid.repository.UserRepository;
import com.aleggeup.confagrid.service.UserService;

@RunWith(MockitoJUnitRunner.class)
public class EmbeddedUserServiceTest {

    private static final String USER_NAME = "username";
    private static final User TEST_USER = new User("test", "test", null);
    private static final Long COUNT = 10L;
    private static final String UNENCRYPTED_PASSWORD = "password";
    private static final String ENCRYPTED_PASSWORD = "$2a$12$OiHAOiHAOiHAOiHAOiHAOek2Bn3t./pRbHo5UYYCAx.Htp/SiLvay";

    @Mock
    private UserRepository mockUserRepository;

    @Mock
    private SecureRandom mockSecureRandom;

    private UserService userService;

    @Before
    public void setUp() {
        userService = new EmbeddedUserService(mockUserRepository, mockSecureRandom);
    }

    @After
    public void tearDown() {
        Mockito.verifyNoMoreInteractions(mockUserRepository, mockSecureRandom);
    }

    @Test
    public void testSave() {
        userService.save(TEST_USER);

        Mockito.verify(mockUserRepository).save(TEST_USER);
    }

    @Test
    public void testGetById() {
        Mockito.when(mockUserRepository.findOne(USER_NAME)).thenReturn(TEST_USER);
        final User user = userService.getById(USER_NAME);

        assertEquals(TEST_USER, user);

        Mockito.verify(mockUserRepository).findOne(USER_NAME);
    }

    @Test
    public void testContainsKey() {
        Mockito.when(mockUserRepository.exists(USER_NAME)).thenReturn(true);

        assertTrue(userService.containsKey(USER_NAME));

        Mockito.verify(mockUserRepository).exists(USER_NAME);
    }

    @Test
    public void testEncrypt() {
        Mockito.doAnswer(new Answer<Void>() {

            @Override
            public Void answer(InvocationOnMock mock) throws Throwable {
                final byte[] bytes = mock.getArgument(0);
                for (int i = 0; i < bytes.length; ++i) {
                    bytes[i] = 0x42;
                }
                return null;
            }
        }).when(mockSecureRandom).nextBytes(ArgumentMatchers.any());

        assertEquals(ENCRYPTED_PASSWORD, userService.encrypt(UNENCRYPTED_PASSWORD));

        Mockito.verify(mockSecureRandom).nextBytes(ArgumentMatchers.any());
    }

    @Test
    public void testMatches() {
        assertTrue(userService.matches(UNENCRYPTED_PASSWORD, ENCRYPTED_PASSWORD));
    }

    @Test
    public void testCount() {
        Mockito.when(mockUserRepository.count()).thenReturn(COUNT);

        assertEquals(COUNT, Long.valueOf(userService.count()));

        Mockito.verify(mockUserRepository).count();
    }
}

@@ -569,56 +569,8 @@
 to;%0A
-import org.mockito.invocation.InvocationOnMock;%0A
 impo
@@ -2393,20 +2393,17 @@
 oAnswer(
-new 
+(
 Answer%3CV
@@ -2410,106 +2410,17 @@
 oid%3E
-(
 ) 
-%7B%0A%0A            @Override%0A            public Void answer(InvocationOnMock mock) throws Throwable
+mock -%3E
  %7B%0A 
@@ -2426,28 +2426,24 @@
             
-  
-  
 final byte%5B%5D
@@ -2484,20 +2484,16 @@
         
-    
 for (int
@@ -2541,20 +2541,16 @@
         
-   
- 
 bytes%5Bi%5D
@@ -2566,32 +2566,27 @@
             
-    %7D%0A  
+%7D%0A%0A
             
@@ -2585,18 +2585,16 @@
         
-  
 return n
@@ -2598,30 +2598,16 @@
 n null;%0A
-            %7D%0A
         

Change Mockito.doAnswer parameter to lambda version