Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kostobog committed Aug 19, 2024
1 parent 504dd53 commit 5c07a29
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 20 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -34,6 +38,12 @@ public class FaultEventRepositoryServiceTest {
@Mock
FaultTreeDao faultTreeDao;

@Mock
SecurityUtils securityUtils;

@Mock
FaultEventTypeService faultEventTypeService;

@InjectMocks
FaultEventRepositoryService repositoryService;

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

Expand All @@ -32,6 +35,9 @@ class FaultTreeRepositoryServiceTest {
@InjectMocks
FaultTreeRepositoryService repositoryService;

@Mock
SecurityUtils securityUtils;

@BeforeEach
void setUp() {
MockitoAnnotations.initMocks(this);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -28,6 +26,8 @@ class SystemRepositoryServiceTest {
@Mock
ComponentRepositoryService componentRepositoryService;
@Mock
SecurityUtils securityUtils;
@Mock
Validator validator;
@InjectMocks
SystemRepositoryService repositoryService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,6 +30,8 @@ class UserRepositoryServiceTest {
Validator validator;
@InjectMocks
UserRepositoryService repositoryService;
@Mock
SecurityUtils securityUtils;

@BeforeEach
void setUp() {
Expand Down Expand Up @@ -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));
}

Expand All @@ -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);
Expand Down

0 comments on commit 5c07a29

Please sign in to comment.