package com.sdl.dxa.modules.audience.model;

import com.tridion.marketingsolution.profile.Contact;
import com.tridion.marketingsolution.utilities.TcmUri;
import org.junit.Test;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.IOException;
import java.sql.SQLException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings("Duplicates")
public class UserProfileImplTest {

    @Test
    public void shouldGetIdAndUsernameAndPasswordFromContact() throws IOException, SQLException {
        //given 
        Contact contact = mock(Contact.class);
        when(contact.getExtendedDetail("uKey")).thenReturn("user");
        when(contact.getExtendedDetail("pKey")).thenReturn("pass");
        TcmUri tcmUri = mock(TcmUri.class);
        when(tcmUri.toString()).thenReturn("id");
        when(contact.getId()).thenReturn(tcmUri);

        //when
        UserProfile profile = new UserProfileImpl(contact, "user", "uKey", "pKey", new ContactIdentifiers("uKey", "DXA"));

        //then
        assertEquals("user", profile.getUsername());
        assertEquals("pass", profile.getPassword());
        assertEquals("id", profile.getId());
    }

    @Test
    public void shouldReturnNonNullPasswordEncoder() {
        //when
        PasswordEncoder encoder = new UserProfileImpl(null, null, null, null, null).getPasswordEncoder();

        //then
        assertNotNull(encoder);
    }
}
@@ -170,16 +170,79 @@
 t.Test;%0A
+import org.springframework.security.core.CredentialsContainer;%0A
 import o
@@ -302,16 +302,16 @@
 ncoder;%0A
-
 %0Aimport 
@@ -475,28 +475,195 @@
 org.
-mockito.Mockito.mock
+junit.Assert.assertTrue;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Matchers.isNull;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.verify
 ;%0Aim
@@ -1773,10 +1773,442 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void shouldEraseCredentials() %7B%0A        //given %0A        Contact contact = mock(Contact.class);%0A%0A        //when%0A        UserProfileImpl userProfile = new UserProfileImpl(contact, null, null, %22pKey%22, null);%0A        userProfile.eraseCredentials();%0A%0A        //then%0A        verify(contact).setExtendedDetail(eq(%22pKey%22), isNull(String.class));%0A        assertTrue(userProfile instanceof CredentialsContainer);%0A    %7D
 %0A%7D

Test for erasing credentials