summaryrefslogtreecommitdiff
path: root/src/test/java/simulator/builder/SimulationBuilderTest.java
blob: 82860f04004bc17418562ab4b75300568b5ce0f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package simulator.builder;

import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.*;
import java.io.File;
import java.nio.file.*;

/**
 * Tests for the SimulationBuilder framework
 */
class SimulationBuilderTest {
    
    private static final String TEST_DIR = "target/test-simulations/";
    
    @BeforeEach
    void setUp() throws Exception {
        // Create test directory
        Files.createDirectories(Paths.get(TEST_DIR));
    }
    
    @AfterEach
    void tearDown() throws Exception {
        // Clean up test files
        File dir = new File(TEST_DIR);
        if (dir.exists()) {
            for (File file : dir.listFiles()) {
                file.delete();
            }
        }
    }
    
    @Test
    void testCreateBasicRaftSimulation() throws Exception {
        String filename = TEST_DIR + "test-raft.dat";
        
        // Create a basic Raft simulation
        new SimulationBuilder()
            .withProcesses(3)
            .withProtocol(SimulationBuilder.Protocols.RAFT)
            .activateServers(0, 1, 2)
            .save(filename);
        
        // Verify file was created
        File file = new File(filename);
        assertTrue(file.exists(), "Simulation file should be created");
        assertTrue(file.length() > 1000, "File should have content");
        
        // Verify it contains Raft protocol
        String content = Files.readString(file.toPath());
        assertTrue(content.contains("VSRaftProtocol"), "Should contain Raft protocol classname");
    }
    
    @Test
    void testCreateRaftWithClients() throws Exception {
        String filename = TEST_DIR + "test-raft-clients.dat";
        
        // Use factory method
        SimulationFactory.createRaftSimulation(3, 2)
            .save(filename);
        
        // Verify file was created
        File file = new File(filename);
        assertTrue(file.exists(), "Simulation file should be created");
        
        // Should have 5 processes (3 servers + 2 clients)
        String content = Files.readString(file.toPath());
        assertTrue(content.contains("VSRaftProtocol"), "Should contain Raft protocol");
    }
    
    @Test
    void testCreatePingPongSimulation() throws Exception {
        String filename = TEST_DIR + "test-pingpong.dat";
        
        SimulationFactory.createPingPongSimulation(2)
            .save(filename);
        
        File file = new File(filename);
        assertTrue(file.exists(), "Simulation file should be created");
        
        String content = Files.readString(file.toPath());
        assertTrue(content.contains("VSPingPongProtocol"), "Should contain PingPong protocol");
    }
    
    @Test
    void testCreateComplexSimulation() throws Exception {
        String filename = TEST_DIR + "test-complex.dat";
        
        // Create a complex simulation with events
        new SimulationBuilder()
            .withProcesses(5)
            .withProtocol(SimulationBuilder.Protocols.RAFT)
            .withDuration(30000)
            .activateServers(0, 1, 2)
            .activateClients(1000, 3, 4)
            .addCrashEvent(0, 5000)
            .addRecoveryEvent(0, 10000)
            .save(filename);
        
        File file = new File(filename);
        assertTrue(file.exists(), "Simulation file should be created");
        assertTrue(file.length() > 5000, "Complex simulation should be larger");
        
        String content = Files.readString(file.toPath());
        assertTrue(content.contains("VSProcessCrashEvent"), "Should contain crash event");
        assertTrue(content.contains("VSProcessRecoverEvent"), "Should contain recovery event");
    }
    
    @Test
    void testAllProtocolTypes() throws Exception {
        // Test that all protocol constants work
        String[] protocols = {
            SimulationBuilder.Protocols.RAFT,
            SimulationBuilder.Protocols.PING_PONG,
            SimulationBuilder.Protocols.BERKLEY_TIME,
            SimulationBuilder.Protocols.BROADCAST,
            SimulationBuilder.Protocols.ONE_PHASE_COMMIT,
            SimulationBuilder.Protocols.TWO_PHASE_COMMIT,
            SimulationBuilder.Protocols.RELIABLE_MULTICAST
        };
        
        for (String protocol : protocols) {
            String filename = TEST_DIR + "test-" + protocol.substring(protocol.lastIndexOf('.') + 1) + ".dat";
            
            new SimulationBuilder()
                .withProcesses(3)
                .withProtocol(protocol)
                .activateServers(0, 1, 2)
                .save(filename);
            
            File file = new File(filename);
            assertTrue(file.exists(), "Should create file for " + protocol);
            assertTrue(file.length() > 1000, "File should have content for " + protocol);
        }
    }
    
    @Test
    void testInvalidConfiguration() {
        // Test that invalid configurations throw exceptions
        assertThrows(IllegalArgumentException.class, () -> {
            SimulationFactory.createRaftSimulation(2, 0); // Too few servers
        });
        
        assertThrows(IllegalArgumentException.class, () -> {
            SimulationFactory.createBerkeleyTimeSimulation(1); // Too few processes
        });
    }
}