|
| 1 | +package org.testar.statemodel.persistence.orientdb; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Test; |
| 5 | +import org.testar.monkey.alayer.Tags; |
| 6 | +import org.testar.monkey.alayer.Verdict; |
| 7 | +import org.testar.statemodel.AbstractState; |
| 8 | +import org.testar.statemodel.ConcreteState; |
| 9 | +import org.testar.statemodel.persistence.orientdb.entity.DocumentEntity; |
| 10 | +import org.testar.statemodel.persistence.orientdb.entity.EdgeEntity; |
| 11 | +import org.testar.statemodel.persistence.orientdb.entity.EntityManager; |
| 12 | +import org.testar.statemodel.persistence.orientdb.entity.PropertyValue; |
| 13 | +import org.testar.statemodel.persistence.orientdb.entity.VertexEntity; |
| 14 | +import org.testar.statemodel.sequence.SequenceNode; |
| 15 | +import org.testar.statemodel.util.EventHelper; |
| 16 | + |
| 17 | +import java.util.Collections; |
| 18 | +import java.util.HashMap; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import static org.junit.Assert.assertEquals; |
| 23 | +import static org.junit.Assert.assertFalse; |
| 24 | +import static org.junit.Assert.assertNotNull; |
| 25 | +import static org.mockito.ArgumentMatchers.any; |
| 26 | +import static org.mockito.Mockito.doAnswer; |
| 27 | +import static org.mockito.Mockito.mock; |
| 28 | + |
| 29 | +public class OrientDBManagerTest { |
| 30 | + |
| 31 | + private EntityManager entityManager; |
| 32 | + private OrientDBManager orientDBManager; |
| 33 | + private Map<String, Map<String, Object>> persistedConcreteStates; |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setUp() { |
| 37 | + entityManager = mock(EntityManager.class); |
| 38 | + persistedConcreteStates = new HashMap<>(); |
| 39 | + doAnswer(invocation -> { |
| 40 | + DocumentEntity documentEntity = invocation.getArgument(0); |
| 41 | + capturePersistedConcreteState(documentEntity); |
| 42 | + return null; |
| 43 | + }).when(entityManager).saveEntity(any(DocumentEntity.class)); |
| 44 | + orientDBManager = new OrientDBManager(new EventHelper(), entityManager); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void testPersistSequenceNodeDoesNotEnableConcreteStateUpdates() { |
| 49 | + AbstractState abstractState = new AbstractState("abstract-state", Collections.emptySet()); |
| 50 | + abstractState.setModelIdentifier("model-identifier"); |
| 51 | + |
| 52 | + List<Verdict> failVerdicts = Collections.singletonList(new Verdict(Verdict.Severity.WARNING_ACCESSIBILITY_FAULT, "Accessibility issue")); |
| 53 | + List<Verdict> okVerdicts = Collections.singletonList(Verdict.OK); |
| 54 | + |
| 55 | + ConcreteState failingConcreteState = new ConcreteState("concrete-state", abstractState); |
| 56 | + failingConcreteState.addAttribute(Tags.OracleVerdicts, failVerdicts); |
| 57 | + |
| 58 | + ConcreteState okConcreteState = new ConcreteState("concrete-state", abstractState); |
| 59 | + okConcreteState.addAttribute(Tags.OracleVerdicts, okVerdicts); |
| 60 | + |
| 61 | + SequenceNode failingSequenceNode = new SequenceNode("sequence-id", 1, failingConcreteState, null, Collections.emptySet()); |
| 62 | + SequenceNode okSequenceNode = new SequenceNode("sequence-id", 2, okConcreteState, null, Collections.emptySet()); |
| 63 | + |
| 64 | + orientDBManager.persistSequenceNode(failingSequenceNode); |
| 65 | + orientDBManager.persistSequenceNode(okSequenceNode); |
| 66 | + |
| 67 | + Map<String, Object> persistedConcreteState = persistedConcreteStates.get("model-identifier-concrete-state"); |
| 68 | + |
| 69 | + assertNotNull(persistedConcreteState); |
| 70 | + assertFalse(okVerdicts.equals(persistedConcreteState.get("OracleVerdicts"))); |
| 71 | + assertEquals(failVerdicts, persistedConcreteState.get("OracleVerdicts")); |
| 72 | + } |
| 73 | + |
| 74 | + private void capturePersistedConcreteState(DocumentEntity documentEntity) { |
| 75 | + if (!(documentEntity instanceof EdgeEntity)) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + EdgeEntity edgeEntity = (EdgeEntity) documentEntity; |
| 80 | + if (!"Accessed".equals(edgeEntity.getEntityClass().getClassName())) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + VertexEntity targetEntity = edgeEntity.getTargetEntity(); |
| 85 | + if (!"ConcreteState".equals(targetEntity.getEntityClass().getClassName())) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + String concreteStateKey = (String) targetEntity.getPropertyValue("widgetId").getValue(); |
| 90 | + if (persistedConcreteStates.containsKey(concreteStateKey) && !targetEntity.updateEnabled()) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + Map<String, Object> persistedProperties = new HashMap<>(); |
| 95 | + for (String propertyName : targetEntity.getPropertyNames()) { |
| 96 | + PropertyValue propertyValue = targetEntity.getPropertyValue(propertyName); |
| 97 | + persistedProperties.put(propertyName, propertyValue == null ? null : propertyValue.getValue()); |
| 98 | + } |
| 99 | + persistedConcreteStates.put(concreteStateKey, persistedProperties); |
| 100 | + } |
| 101 | +} |
0 commit comments