From 25e049b8c4a6f2a3baf8523551d6d844f888093a Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Sun, 18 Aug 2024 22:10:43 +0200 Subject: [PATCH 1/3] [Fix kbss-cvut/fta-fmea-ui#556] Persist systems in a context --- .../java/cz/cvut/kbss/analysis/dao/SystemDao.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java index 2733731..550fb3e 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java @@ -7,6 +7,7 @@ import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.model.EntityManager; +import cz.cvut.kbss.jopa.model.descriptors.EntityDescriptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -23,6 +24,20 @@ protected SystemDao(EntityManager em, PersistenceConf config, IdentifierService super(System.class, em, config, identifierService, securityUtils); } + @Override + public EntityDescriptor getEntityDescriptor(System entity) { + if(entity.getUri() == null) + entity.setUri(identifierService.generateNewInstanceUri(typeUri.toString())); + EntityDescriptor entityDescriptor = getEntityDescriptor(entity.getUri()); + return entityDescriptor; + } + + public EntityDescriptor getEntityDescriptor(URI uri){ + EntityDescriptor descriptor = new EntityDescriptor(uri); + setEntityDescriptor(descriptor); + return descriptor; + } + public List findComponents(URI systemURI){ return em.createNativeQuery(""" SELECT ?uri { From 504dd531602afce8cc1d4deb04126d0dfa364250 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Sun, 18 Aug 2024 22:11:47 +0200 Subject: [PATCH 2/3] [Fix kbss-cvut/fta-fmea-ui#556] Re-implement fetching reference of current signed-in user to avoid error caused by calling em.find to get user entity. --- .../kbss/analysis/dao/ManagedEntityDao.java | 6 ++-- .../cz/cvut/kbss/analysis/dao/UserDao.java | 34 ++++++++++++++----- .../service/FaultEventRepositoryService.java | 8 +++-- .../service/security/SecurityUtils.java | 10 +++++- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/ManagedEntityDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/ManagedEntityDao.java index fca2658..8e4f488 100644 --- a/src/main/java/cz/cvut/kbss/analysis/dao/ManagedEntityDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/ManagedEntityDao.java @@ -3,7 +3,6 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.ManagedEntity; -import cz.cvut.kbss.analysis.model.UserReference; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.util.Vocabulary; @@ -45,8 +44,7 @@ protected void setEntityDescriptor(EntityDescriptor descriptor) { super.setEntityDescriptor(descriptor); } - public void setChangedByContext(URI context, Date date){ - UserReference user = securityUtils.getCurrentUserReference(); + public void setChangedByContext(URI context, Date date, URI userUri){ em.createNativeQuery(""" DELETE{ GRAPH ?context{ @@ -70,7 +68,7 @@ public void setChangedByContext(URI context, Date date){ .setParameter("pModified", P_MODIFIED) .setParameter("pLastEditor", P_LAST_EDITOR) .setParameter("newModified", date) - .setParameter("newLastEditor", user.getUri()) + .setParameter("newLastEditor", userUri) .executeUpdate(); } diff --git a/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java b/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java index fa95fca..7f9ba78 100755 --- a/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java +++ b/src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java @@ -3,6 +3,7 @@ import cz.cvut.kbss.analysis.config.conf.PersistenceConf; import cz.cvut.kbss.analysis.exception.PersistenceException; import cz.cvut.kbss.analysis.model.User; +import cz.cvut.kbss.analysis.model.UserReference; import cz.cvut.kbss.analysis.service.IdentifierService; import cz.cvut.kbss.analysis.util.Vocabulary; import cz.cvut.kbss.jopa.exceptions.NoResultException; @@ -30,22 +31,37 @@ public UserDao(EntityManager em, PersistenceConf config, IdentifierService ident */ public Optional findByUsername(String username) { Objects.requireNonNull(username); + URI userUri = findUriByUsername(username); + return userUri == null + ? Optional.empty() + : find(userUri); + } + + public UserReference findUserReferenceByUsername(String username) { + URI uri = findUriByUsername(username); + if(uri == null) + return null; + UserReference userReference = new UserReference(); + userReference.setUsername(username); + userReference.setUri(uri); + return userReference; + } + + public URI findUriByUsername(String username) { try { - return Optional - .of(em - .createNativeQuery(""" + return em.createNativeQuery(""" SELECT ?x WHERE { ?x a ?type ; ?hasUsername ?val . FILTER(str(?val) = ?username) } """, - type) - .setParameter("type", typeUri) - .setParameter("hasUsername", URI.create(Vocabulary.s_p_accountName)) - .setParameter("username", username) - .getSingleResult()); + URI.class) + .setParameter("type", typeUri) + .setParameter("hasUsername", URI.create(Vocabulary.s_p_accountName)) + .setParameter("username", username) + .getSingleResult(); } catch (NoResultException e) { - return Optional.empty(); + return null; } catch (RuntimeException e) { throw new PersistenceException(e); } diff --git a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java index ed1012d..db4f288 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryService.java @@ -8,6 +8,7 @@ import cz.cvut.kbss.analysis.model.*; import cz.cvut.kbss.analysis.model.diagram.Rectangle; import cz.cvut.kbss.analysis.model.fta.FtaEventType; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.service.strategy.DirectFtaEvaluation; import cz.cvut.kbss.analysis.util.Vocabulary; import lombok.NonNull; @@ -35,15 +36,17 @@ public class FaultEventRepositoryService extends BaseRepositoryService getTopFaultEvents(URI systemUri) { diff --git a/src/main/java/cz/cvut/kbss/analysis/service/security/SecurityUtils.java b/src/main/java/cz/cvut/kbss/analysis/service/security/SecurityUtils.java index d67558b..1941169 100755 --- a/src/main/java/cz/cvut/kbss/analysis/service/security/SecurityUtils.java +++ b/src/main/java/cz/cvut/kbss/analysis/service/security/SecurityUtils.java @@ -15,6 +15,8 @@ import org.springframework.security.oauth2.jwt.Jwt; import org.springframework.stereotype.Service; +import java.util.Optional; + /** * Handle user session-related functions. */ @@ -52,8 +54,14 @@ public User getCurrentUser() { return userDao.findByUsername(username).orElseThrow().copy(); } } + public UserReference getCurrentUserReference() { - return new UserReference(getCurrentUser()); + String username = Optional.ofNullable(SecurityContextHolder.getContext()) + .map(c -> c.getAuthentication()) + .map(a -> a.getName()).orElse(null); + if(username == null) + return null; + return userDao.findUserReferenceByUsername(username); } // TODO map role, but I am not sure which changes in the model when be required if I add addRole method to User From 5c07a29f5c06c6bd47a72f673a7c359aca05fe82 Mon Sep 17 00:00:00 2001 From: Bogdan Kostov Date: Mon, 19 Aug 2024 13:24:00 +0200 Subject: [PATCH 3/3] Fix failing tests --- .../kbss/analysis/dao/FaultEventDaoTest.java | 3 ++- .../kbss/analysis/dao/FaultTreeDaoTest.java | 26 ++++++++++++------- .../cvut/kbss/analysis/dao/UserDaoTest.java | 3 ++- .../config/TestPersistenceConfig.java | 9 +++++-- .../ComponentRepositoryServiceTest.java | 9 ++++--- .../FaultEventRepositoryServiceTest.java | 26 ++++++++++++++++++- .../FaultTreeRepositoryServiceTest.java | 10 +++++++ .../service/SystemRepositoryServiceTest.java | 6 ++--- .../service/UserRepositoryServiceTest.java | 6 +++++ 9 files changed, 78 insertions(+), 20 deletions(-) diff --git a/src/test/java/cz/cvut/kbss/analysis/dao/FaultEventDaoTest.java b/src/test/java/cz/cvut/kbss/analysis/dao/FaultEventDaoTest.java index 4d4793f..7cf3a65 100755 --- a/src/test/java/cz/cvut/kbss/analysis/dao/FaultEventDaoTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/dao/FaultEventDaoTest.java @@ -1,5 +1,6 @@ package cz.cvut.kbss.analysis.dao; +import cz.cvut.kbss.analysis.config.conf.SecurityConf; import cz.cvut.kbss.analysis.environment.Generator; import cz.cvut.kbss.analysis.model.FaultEvent; import cz.cvut.kbss.analysis.model.diagram.Rectangle; @@ -11,7 +12,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; -@ContextConfiguration(classes = {FaultEventDao.class}) +@ContextConfiguration(classes = {FaultEventDao.class, UserDao.class, SecurityConf.class}) class FaultEventDaoTest extends BaseDaoTestRunner{ @Autowired diff --git a/src/test/java/cz/cvut/kbss/analysis/dao/FaultTreeDaoTest.java b/src/test/java/cz/cvut/kbss/analysis/dao/FaultTreeDaoTest.java index 83d3435..7880748 100755 --- a/src/test/java/cz/cvut/kbss/analysis/dao/FaultTreeDaoTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/dao/FaultTreeDaoTest.java @@ -1,5 +1,6 @@ package cz.cvut.kbss.analysis.dao; +import cz.cvut.kbss.analysis.config.conf.SecurityConf; import cz.cvut.kbss.analysis.environment.Generator; import cz.cvut.kbss.analysis.model.FaultEvent; import cz.cvut.kbss.analysis.model.FaultTree; @@ -15,7 +16,7 @@ import java.util.Set; import java.util.stream.Collectors; -@ContextConfiguration(classes = {FaultTreeDao.class, FaultEventDao.class}) +@ContextConfiguration(classes = {FaultTreeDao.class, FaultEventDao.class, UserDao.class, SecurityConf.class}) class FaultTreeDaoTest extends BaseDaoTestRunner { @Autowired @@ -96,21 +97,28 @@ public void symmetricBranches_shouldQueryAll() { transactional(() -> faultTreeDao.update(tree)); // probabilities propagation update (simulation) // create subtree - FaultEvent X = createEvent("X"); - FaultEvent Y = createEvent("Y"); - FaultEvent Z = createEvent("Z"); - X.addChild(Y); - X.addChild(Z); - transactional(() -> faultEventDao.persist(X)); + FaultEvent BX = createEvent("X"); + FaultEvent BY = createEvent("Y"); + FaultEvent BZ = createEvent("Z"); + BX.addChild(BY); + BX.addChild(BZ); + + FaultEvent CX = createEvent("X"); + FaultEvent CY = createEvent("Y"); + FaultEvent CZ = createEvent("Z"); + CX.addChild(CY); + CX.addChild(CZ); + transactional(() -> faultEventDao.persist(BX)); + transactional(() -> faultEventDao.persist(CX)); transactional(() -> faultTreeDao.update(tree)); // probabilities propagation update (simulation) // add subtree below B - B.addChild(X); + B.addChild(BX); transactional(() -> faultEventDao.update(B)); transactional(() -> faultTreeDao.update(tree)); // probabilities propagation update (simulation) // add subtree below C - C.addChild(X); + C.addChild(CX); transactional(() -> faultEventDao.update(C)); transactional(() -> faultTreeDao.update(tree)); // probabilities propagation update (simulation) diff --git a/src/test/java/cz/cvut/kbss/analysis/dao/UserDaoTest.java b/src/test/java/cz/cvut/kbss/analysis/dao/UserDaoTest.java index 336519e..6041558 100755 --- a/src/test/java/cz/cvut/kbss/analysis/dao/UserDaoTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/dao/UserDaoTest.java @@ -1,5 +1,6 @@ package cz.cvut.kbss.analysis.dao; +import cz.cvut.kbss.analysis.config.conf.SecurityConf; import cz.cvut.kbss.analysis.model.User; import cz.cvut.kbss.jopa.model.EntityManager; import org.junit.jupiter.api.Assertions; @@ -10,7 +11,7 @@ import java.util.Optional; import java.util.UUID; -@ContextConfiguration(classes = {UserDao.class}) +@ContextConfiguration(classes = {UserDao.class, SecurityConf.class}) class UserDaoTest extends BaseDaoTestRunner { @Autowired diff --git a/src/test/java/cz/cvut/kbss/analysis/environment/config/TestPersistenceConfig.java b/src/test/java/cz/cvut/kbss/analysis/environment/config/TestPersistenceConfig.java index e971c42..5d413ab 100755 --- a/src/test/java/cz/cvut/kbss/analysis/environment/config/TestPersistenceConfig.java +++ b/src/test/java/cz/cvut/kbss/analysis/environment/config/TestPersistenceConfig.java @@ -3,14 +3,19 @@ import com.github.ledsoft.jopa.spring.transaction.DelegatingEntityManager; import com.github.ledsoft.jopa.spring.transaction.JopaTransactionManager; import cz.cvut.kbss.analysis.environment.TestPersistenceFactory; +import cz.cvut.kbss.analysis.service.IdentifierService; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.jopa.model.EntityManagerFactory; -import org.springframework.context.annotation.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; @Configuration @ComponentScan(basePackages = "cz.cvut.kbss.analysis.persistence") -@Import({TestPersistenceFactory.class}) +@Import({TestPersistenceFactory.class, IdentifierService.class, SecurityUtils.class }) @EnableTransactionManagement public class TestPersistenceConfig { diff --git a/src/test/java/cz/cvut/kbss/analysis/service/ComponentRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/analysis/service/ComponentRepositoryServiceTest.java index 5fe1154..9a6b0fc 100755 --- a/src/test/java/cz/cvut/kbss/analysis/service/ComponentRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/service/ComponentRepositoryServiceTest.java @@ -10,7 +10,6 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mapdb.Fun; import org.mockito.*; import java.util.Optional; @@ -121,13 +120,17 @@ void linkComponents_shouldFindComponents_shouldSetParentComponent_shouldCallUpda Mockito.when(componentDao.find(eq(linkComponent.getUri()))).thenReturn(Optional.of(linkComponent)); Mockito.when(componentDao.exists(eq(component.getUri()))).thenReturn(true); + Mockito.when(componentDao.exists(eq(linkComponent.getUri()))).thenReturn(true); Mockito.when(componentValidator.supports(any())).thenReturn(true); Mockito.when(componentDao.update(eq(component))).thenReturn(component); + Mockito.when(componentDao.update(eq(linkComponent))).thenReturn(linkComponent); repositoryService.linkComponents(component.getUri(), linkComponent.getUri()); - Mockito.verify(componentDao).update(component); - Assertions.assertEquals(linkComponent.getUri(), component.getParentComponent()); + Assertions.assertEquals(linkComponent, component.getParentComponent()); + Assertions.assertTrue(linkComponent.getComponents() != null ); + Assertions.assertTrue(linkComponent.getComponents().size() == 1); + Assertions.assertTrue(linkComponent.getComponents().contains( component)); } @Test diff --git a/src/test/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryServiceTest.java index 4267acc..a607ba6 100755 --- a/src/test/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/service/FaultEventRepositoryServiceTest.java @@ -7,7 +7,10 @@ import cz.cvut.kbss.analysis.model.Component; import cz.cvut.kbss.analysis.model.FailureMode; import cz.cvut.kbss.analysis.model.FaultEvent; +import cz.cvut.kbss.analysis.model.UserReference; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import cz.cvut.kbss.analysis.service.validation.FaultEventValidator; +import cz.cvut.kbss.analysis.util.Vocabulary; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -16,6 +19,7 @@ import org.mockito.Mockito; import org.mockito.MockitoAnnotations; +import java.net.URI; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -34,6 +38,12 @@ public class FaultEventRepositoryServiceTest { @Mock FaultTreeDao faultTreeDao; + @Mock + SecurityUtils securityUtils; + + @Mock + FaultEventTypeService faultEventTypeService; + @InjectMocks FaultEventRepositoryService repositoryService; @@ -42,18 +52,29 @@ void setUp() { MockitoAnnotations.initMocks(this); } + protected void setUserReference(){ + UserReference user = new UserReference(); + user.setUri(URI.create(Vocabulary.s_c_Person + "/test-user")); + Mockito.when(securityUtils.getCurrentUserReference()).thenReturn(user); + } + @Test void update_shouldCallPreUpdate() { + + FaultEvent event = new FaultEvent(); event.setUri(Generator.generateUri()); + Mockito.when(faultEventDao.find(event.getUri())).thenReturn(Optional.of(event)); Mockito.when(faultEventDao.exists(event.getUri())).thenReturn(true); Mockito.when(faultEventValidator.supports(any())).thenReturn(true); Mockito.when(faultEventDao.update(eq(event))).thenReturn(event); + setUserReference(); repositoryService.update(event); Mockito.verify(faultEventValidator).validate(eq(event), any()); +// Mockito.verify(repositoryService).preUpdate(event); } @Test @@ -71,6 +92,7 @@ void remove_isNotRootEvent_shouldJustRun() { FaultEvent event = new FaultEvent(); event.setUri(Generator.generateUri()); + setUserReference(); Mockito.when(faultTreeDao.isRootEvent(eq(event.getUri()))).thenReturn(false); repositoryService.remove(event); @@ -86,7 +108,7 @@ void addInputEvent_shouldDo2PhaseUpdate() { FaultEvent parentEvent = new FaultEvent(); parentEvent.setUri(Generator.generateUri()); - + setUserReference(); Mockito.when(faultEventDao.find(eq(parentEvent.getUri()))).thenReturn(Optional.of(parentEvent)); Mockito.when(faultEventDao.exists(parentEvent.getUri())).thenReturn(true); Mockito.when(faultEventValidator.supports(any())).thenReturn(true); @@ -132,6 +154,7 @@ void addFailureMode_shouldSetData_shouldUpdateEvent() { component.setUri(Generator.generateUri()); failureMode.setItem(component); + setUserReference(); Mockito.when(faultEventDao.find(eq(event.getUri()))).thenReturn(Optional.of(event)); Mockito.when(componentRepositoryService.findRequired(eq(failureMode.getItem().getUri()))).thenReturn(component); Mockito.when(faultEventDao.exists(event.getUri())).thenReturn(true); @@ -156,6 +179,7 @@ void deleteFailureMode_shouldRemoveFromEventFromFailureModeEffects() { failureMode.setUri(Generator.generateUri()); event.setBehavior(failureMode); + setUserReference(); Mockito.when(faultEventDao.find(eq(event.getUri()))).thenReturn(Optional.of(event)); Mockito.when(faultEventDao.exists(event.getUri())).thenReturn(true); Mockito.when(faultEventValidator.supports(any())).thenReturn(true); diff --git a/src/test/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryServiceTest.java index 88ec1d5..56ae30e 100755 --- a/src/test/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/service/FaultTreeRepositoryServiceTest.java @@ -3,7 +3,9 @@ import cz.cvut.kbss.analysis.dao.FaultTreeDao; import cz.cvut.kbss.analysis.environment.Generator; import cz.cvut.kbss.analysis.model.FaultEvent; +import cz.cvut.kbss.analysis.model.FaultEventType; import cz.cvut.kbss.analysis.model.FaultTree; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,6 +16,7 @@ import org.springframework.validation.Validator; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -32,6 +35,9 @@ class FaultTreeRepositoryServiceTest { @InjectMocks FaultTreeRepositoryService repositoryService; + @Mock + SecurityUtils securityUtils; + @BeforeEach void setUp() { MockitoAnnotations.initMocks(this); @@ -42,6 +48,10 @@ void findWithPropagation_shouldFindTree_shouldUpdateProbabilities() { FaultTree tree = new FaultTree(); tree.setUri(Generator.generateUri()); tree.setManifestingEvent(new FaultEvent()); + FaultEventType fet = new FaultEventType(); + fet.setAuxiliary(true); + tree.getManifestingEvent().setSupertypes(new HashSet<>()); + tree.getManifestingEvent().getSupertypes().add(fet); Mockito.when(faultTreeDao.find(tree.getUri())).thenReturn(Optional.of(tree)); Mockito.when(faultTreeDao.exists(tree.getUri())).thenReturn(true); diff --git a/src/test/java/cz/cvut/kbss/analysis/service/SystemRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/analysis/service/SystemRepositoryServiceTest.java index 63eba71..75d5547 100755 --- a/src/test/java/cz/cvut/kbss/analysis/service/SystemRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/service/SystemRepositoryServiceTest.java @@ -1,11 +1,10 @@ package cz.cvut.kbss.analysis.service; import cz.cvut.kbss.analysis.dao.SystemDao; -import cz.cvut.kbss.analysis.dto.update.FailureModesTableUpdateDTO; import cz.cvut.kbss.analysis.environment.Generator; import cz.cvut.kbss.analysis.model.Component; -import cz.cvut.kbss.analysis.model.FailureModesTable; import cz.cvut.kbss.analysis.model.System; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -17,7 +16,6 @@ import java.util.Optional; -import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -28,6 +26,8 @@ class SystemRepositoryServiceTest { @Mock ComponentRepositoryService componentRepositoryService; @Mock + SecurityUtils securityUtils; + @Mock Validator validator; @InjectMocks SystemRepositoryService repositoryService; diff --git a/src/test/java/cz/cvut/kbss/analysis/service/UserRepositoryServiceTest.java b/src/test/java/cz/cvut/kbss/analysis/service/UserRepositoryServiceTest.java index 15fb44c..72a488b 100755 --- a/src/test/java/cz/cvut/kbss/analysis/service/UserRepositoryServiceTest.java +++ b/src/test/java/cz/cvut/kbss/analysis/service/UserRepositoryServiceTest.java @@ -7,6 +7,7 @@ import cz.cvut.kbss.analysis.exception.LogicViolationException; import cz.cvut.kbss.analysis.exception.UsernameNotAvailableException; import cz.cvut.kbss.analysis.model.User; +import cz.cvut.kbss.analysis.service.security.SecurityUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -29,6 +30,8 @@ class UserRepositoryServiceTest { Validator validator; @InjectMocks UserRepositoryService repositoryService; + @Mock + SecurityUtils securityUtils; @BeforeEach void setUp() { @@ -70,8 +73,10 @@ void updateCurrent_passwordsDoNotMatch_shouldThrowException() { UserUpdateDTO updateDTO = new UserUpdateDTO(); updateDTO.setUri(user.getUri()); + Mockito.when(securityUtils.getCurrentUser()).thenReturn(user); Mockito.when(passwordEncoder.matches(updateDTO.getPassword(), user.getPassword())).thenReturn(false); + assertThrows(LogicViolationException.class, () -> repositoryService.updateCurrent(updateDTO)); } @@ -85,6 +90,7 @@ void updateCurrent_passwordsOk_shouldEncodePassword_shouldCallUpdate() { updateDTO.setNewPassword("oldPassword"); updateDTO.setUri(user.getUri()); + Mockito.when(securityUtils.getCurrentUser()).thenReturn(user); Mockito.when(passwordEncoder.matches(updateDTO.getPassword(), user.getPassword())).thenReturn(true); Mockito.when(userDao.exists(user.getUri())).thenReturn(true); Mockito.when(validator.supports(any())).thenReturn(true);