/*
 * Copyright 2016-2017 the original author or authors.
 *
 * Licensed 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 de.vorb.platon.security;

import de.vorb.platon.persistence.PropertyRepository;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class DatabaseSecretKeyProviderTest {

    @Mock
    private PropertyRepository propertyRepository;

    private static final String SECRET_KEY = "secret_key";

    @InjectMocks
    private DatabaseSecretKeyProvider secretKeyProvider;

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void noSecretKeyAvailable() throws Exception {

        Mockito.when(propertyRepository.findValueByKey(Mockito.eq(SECRET_KEY))).thenReturn(null);

        secretKeyProvider.getSecretKey();

        Mockito.verify(propertyRepository).insertValue(Mockito.eq(SECRET_KEY), Mockito.any());
    }

    @Test
    public void secretKeyExists() throws Exception {

        final SecretKey storedSecretKey = KeyGenerator.getInstance(
                HmacRequestVerifier.HMAC_ALGORITHM.toString()).generateKey();

        Mockito.when(propertyRepository.findValueByKey(Mockito.eq(SECRET_KEY)))
                .thenReturn(Base64.getEncoder().encodeToString(storedSecretKey.getEncoded()));

        final SecretKey secretKeyFromRepo = secretKeyProvider.getSecretKey();

        assertThat(secretKeyFromRepo).isEqualTo(storedSecretKey);
    }
}

@@ -707,33 +707,8 @@
 y;%0A%0A
-import org.junit.Before;%0A
 impo
@@ -820,36 +820,8 @@
 ck;%0A
-import org.mockito.Mockito;%0A
 impo
@@ -1012,16 +1012,259 @@
 ertThat;
+%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Mockito.never;%0Aimport static org.mockito.Mockito.reset;%0Aimport static org.mockito.Mockito.verify;%0Aimport static org.mockito.Mockito.when;
 %0A%0A@RunWi
@@ -1342,70 +1342,8 @@
  %7B%0A%0A
-    @Mock%0A    private PropertyRepository propertyRepository;%0A%0A
     
@@ -1482,64 +1482,63 @@
    @
-Before%0A    public void setUp() throws Exception %7B%0A%0A    %7D
+Mock%0A    private PropertyRepository propertyRepository;
 %0A%0A  
@@ -1603,37 +1603,344 @@
 ption %7B%0A
-%0A        Mockito.when
+        final SecretKey secretKey = assertThatSecretKeyIsGenerated();%0A        assertThatSecretKeyIsNotRecreated(secretKey);%0A    %7D%0A%0A    private void assertThatSecretKeyIsNotRecreated(SecretKey secretKey) %7B%0A        reset(propertyRepository);%0A        assertThat(secretKeyProvider.getSecretKey()).isSameAs(secretKey);%0A        verify
 (propert
@@ -1942,32 +1942,42 @@
 opertyRepository
+, never())
 .findValueByKey(
@@ -1972,32 +1972,143 @@
 dValueByKey(
-Mockito.
+any());%0A    %7D%0A%0A    private SecretKey assertThatSecretKeyIsGenerated() %7B%0A        when(propertyRepository.findValueByKey(
 eq(SECRET_KE
@@ -2130,24 +2130,51 @@
 (null);%0A
-%0A
        
+ final SecretKey secretKey =
  secretK
@@ -2196,33 +2196,32 @@
 ecretKey();%0A
-%0A
         
 Mockito.veri
@@ -2208,24 +2208,16 @@
         
-Mockito.
 verify(p
@@ -2247,24 +2247,16 @@
 rtValue(
-Mockito.
 eq(SECRE
@@ -2267,22 +2267,41 @@
 Y), 
-Mockito.any())
+any());%0A%0A        return secretKey
 ;%0A  
@@ -2524,24 +2524,16 @@
         
-Mockito.
 when(pro
@@ -2563,24 +2563,16 @@
 ueByKey(
-Mockito.
 eq(SECRE

Improve DatabaseSecretKeyProvider test