Skip to content

Commit

Permalink
Merge pull request #148 from kbss-cvut/fix/fta-fmea-ui-556-cant-renam…
Browse files Browse the repository at this point in the history
…e-or-delete-system

Fix cant rename or delete system
  • Loading branch information
blcham authored Aug 19, 2024
2 parents 9594d19 + 5c07a29 commit 760085f
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 36 deletions.
6 changes: 2 additions & 4 deletions src/main/java/cz/cvut/kbss/analysis/dao/ManagedEntityDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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{
Expand All @@ -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();
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/java/cz/cvut/kbss/analysis/dao/SystemDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<Component> findComponents(URI systemURI){
return em.createNativeQuery("""
SELECT ?uri {
Expand Down
34 changes: 25 additions & 9 deletions src/main/java/cz/cvut/kbss/analysis/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,22 +31,37 @@ public UserDao(EntityManager em, PersistenceConf config, IdentifierService ident
*/
public Optional<User> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,15 +36,17 @@ public class FaultEventRepositoryService extends BaseRepositoryService<FaultEven
private final FaultTreeDao faultTreeDao;
private final FaultEventTypeService faultEventTypeService;
private final FaultEventTypeDao faultEventTypeDao;
private final SecurityUtils securityUtils;

@Autowired
public FaultEventRepositoryService(@Qualifier("faultEventValidator") Validator validator, FaultEventDao faultEventDao, ComponentRepositoryService componentRepositoryService, FaultTreeDao faultTreeDao, FaultEventTypeService faultEventTypeService, FaultEventTypeDao faultEventTypeDao) {
public FaultEventRepositoryService(@Qualifier("faultEventValidator") Validator validator, FaultEventDao faultEventDao, ComponentRepositoryService componentRepositoryService, FaultTreeDao faultTreeDao, FaultEventTypeService faultEventTypeService, FaultEventTypeDao faultEventTypeDao, SecurityUtils securityUtils) {
super(validator);
this.faultEventDao = faultEventDao;
this.componentRepositoryService = componentRepositoryService;
this.faultTreeDao = faultTreeDao;
this.faultEventTypeService = faultEventTypeService;
this.faultEventTypeDao = faultEventTypeDao;
this.securityUtils = securityUtils;
}

@Override
Expand Down Expand Up @@ -221,7 +224,8 @@ protected void postRemove(@NonNull FaultEvent instance) {

protected void setChange(FaultEvent instance){
URI context = faultEventDao.getContext(instance);
faultTreeDao.setChangedByContext(context, new Date());
UserReference userReference = securityUtils.getCurrentUserReference();
faultTreeDao.setChangedByContext(context, new Date(), userReference.getUri());
}

public List<FaultEventType> getTopFaultEvents(URI systemUri) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
26 changes: 17 additions & 9 deletions src/test/java/cz/cvut/kbss/analysis/dao/FaultTreeDaoTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion src/test/java/cz/cvut/kbss/analysis/dao/UserDaoTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 760085f

Please sign in to comment.