Skip to content

Commit

Permalink
Added homework unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arminzavada committed Nov 2, 2024
1 parent 912917f commit b6abcfb
Show file tree
Hide file tree
Showing 20 changed files with 1,140 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hu.bme.mit.ase.gradle.conventions

import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.tasks.testing.logging.TestExceptionFormat

plugins {
`java-library`
Expand All @@ -10,6 +11,21 @@ plugins {

val libs = the<LibrariesForLibs>()

val mockitoAgent = configurations.create("mockitoAgent")

dependencies {
testImplementation(libs.junit.jupiter.core)
testImplementation(libs.junit.jupiter.params)
testImplementation(libs.mockito.core)

@Suppress("UnstableApiUsage")
mockitoAgent(libs.mockito.core) {
isTransitive = false
}

testRuntimeOnly(libs.junit.jupiter.engine)
}

repositories {
mavenCentral()
}
Expand All @@ -20,18 +36,9 @@ java.toolchain {

tasks {
test {
jvmArgs("-javaagent:${mockitoAgent.asPath}")
useJUnitPlatform()
testLogging.showStandardStreams = true
testLogging.exceptionFormat = TestExceptionFormat.FULL
}
}

dependencies {
implementation(libs.slf4j.api)

runtimeOnly(libs.slf4j.log4j.impl)

testImplementation(libs.junit.jupiter.core)
testImplementation(libs.junit.jupiter.params)

testRuntimeOnly(libs.junit.jupiter.engine)
}
6 changes: 2 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

[versions]
slf4j = "1.7.25"
log4j = "2.23.1"
jUnit = "5.10.0"
mockito = "5.14.2"
python-gradle-plugin = "2.7.3"

[libraries]
slf4j-api = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
slf4j-log4j-impl = { group = "org.apache.logging.log4j", name = "log4j-slf4j-impl", version.ref = "log4j" }
junit-jupiter-core = { group = "org.junit.jupiter", name = "junit-jupiter-api", version.ref = "jUnit" }
junit-jupiter-engine = { group = "org.junit.jupiter", name = "junit-jupiter-engine", version.ref = "jUnit" }
junit-jupiter-params = { group = "org.junit.jupiter", name = "junit-jupiter-params", version.ref = "jUnit" }
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockito" }
python-gradle-plugin = { group = "com.pswidersk", name = "python-gradle-plugin", version.ref = "python-gradle-plugin" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hu.bme.mit.ase.cps.types.computers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class LaptopTest {

@Test
@DisplayName("Laptop should be constructable with name and IP parameters")
void laptopShouldBeConstructable() {
new Laptop("laptop-1", "192.168.0.104");
}

@Test
@DisplayName("Laptop should correctly return name and IP after construction")
void laptopConstructorShouldSetFields() {
var laptop = new Laptop("laptop-1", "192.168.0.104");
Assertions.assertAll(
() -> Assertions.assertEquals("laptop-1", laptop.getName(), "Expected Laptop name to be 'laptop-1', but was '" + laptop.getName() + "'"),
() -> Assertions.assertEquals("192.168.0.104", laptop.getIp(), "Expected Laptop IP to be '192.168.0.104', but was '" + laptop.getIp() + "'")
);
}

@Test
@DisplayName("Laptop should return correct OS")
void testLaptopGetOs() {
var laptop = new Laptop("laptop-1", "192.168.0.104");
Assertions.assertEquals("Windows10", laptop.getOs(), "Expected Laptop OS to be 'Windows10', but was '" + laptop.getOs() + "'");
}

@Test
@DisplayName("Laptop should return correct description")
void testLaptopGetDescription() {
var laptop = new Laptop("laptop-1", "192.168.0.104");
Assertions.assertEquals("Portable computer for professional and personal use", laptop.getDescription(),
"Expected Laptop description to be 'Portable computer for professional and personal use', but was '" + laptop.getDescription() + "'");
}

@Test
@DisplayName("Laptop should return correct resources")
void testLaptopGetResources() {
var laptop = new Laptop("laptop-1", "192.168.0.104");
var resources = laptop.getResources();
Assertions.assertAll(
() -> Assertions.assertEquals("8GB", resources.getMemory(), "Expected Laptop memory to be '8GB', but was '" + resources.getMemory() + "'"),
() -> Assertions.assertEquals("4-core Intel i5", resources.getCpu(), "Expected Laptop CPU to be '4-core Intel i5', but was '" + resources.getCpu() + "'"),
() -> Assertions.assertEquals("256GB", resources.getStorage(), "Expected Laptop storage to be '256GB', but was '" + resources.getStorage() + "'")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hu.bme.mit.ase.cps.types.computers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class PCTest {

@Test
@DisplayName("PC should be constructable with name and IP parameters")
void pcShouldBeConstructable() {
new PC("pc-1", "192.168.0.103");
}

@Test
@DisplayName("PC should correctly return name and IP after construction")
void pcConstructorShouldSetFields() {
var pc = new PC("pc-1", "192.168.0.103");
Assertions.assertAll(
() -> Assertions.assertEquals("pc-1", pc.getName(), "Expected PC name to be 'pc-1', but was '" + pc.getName() + "'"),
() -> Assertions.assertEquals("192.168.0.103", pc.getIp(), "Expected PC IP to be '192.168.0.103', but was '" + pc.getIp() + "'")
);
}

@Test
@DisplayName("PC should return correct OS")
void testPCGetOs() {
var pc = new PC("pc-1", "192.168.0.103");
Assertions.assertEquals("UbuntuLinux", pc.getOs(), "Expected PC OS to be 'UbuntuLinux', but was '" + pc.getOs() + "'");
}

@Test
@DisplayName("PC should return correct description")
void testPCGetDescription() {
var pc = new PC("pc-1", "192.168.0.103");
Assertions.assertEquals("Standard desktop computer for general-purpose use", pc.getDescription(),
"Expected PC description to be 'Standard desktop computer for general-purpose use', but was '" + pc.getDescription() + "'");
}

@Test
@DisplayName("PC should return correct resources")
void testPCGetResources() {
var pc = new PC("pc-1", "192.168.0.103");
var resources = pc.getResources();
Assertions.assertAll(
() -> Assertions.assertEquals("16GB", resources.getMemory(), "Expected PC memory to be '16GB', but was '" + resources.getMemory() + "'"),
() -> Assertions.assertEquals("8-core Intel i7", resources.getCpu(), "Expected PC CPU to be '8-core Intel i7', but was '" + resources.getCpu() + "'"),
() -> Assertions.assertEquals("512GB", resources.getStorage(), "Expected PC storage to be '512GB', but was '" + resources.getStorage() + "'")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hu.bme.mit.ase.cps.types.computers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class RaspberryPi3Test {

@Test
@DisplayName("RaspberryPi3 should be constructable with name and IP parameters")
void raspberryPi3ShouldBeConstructable() {
new RaspberryPi3("pi3-1", "192.168.0.101");
}

@Test
@DisplayName("RaspberryPi3 should correctly return name and IP after construction")
void raspberryPi3ConstructorShouldSetFields() {
var pi3 = new RaspberryPi3("pi3-1", "192.168.0.101");
Assertions.assertAll(
() -> Assertions.assertEquals("pi3-1", pi3.getName(), "Expected RaspberryPi3 name to be 'pi3-1', but was '" + pi3.getName() + "'"),
() -> Assertions.assertEquals("192.168.0.101", pi3.getIp(), "Expected RaspberryPi3 IP to be '192.168.0.101', but was '" + pi3.getIp() + "'")
);
}

@Test
@DisplayName("RaspberryPi3 should return correct OS")
void testRaspberryPi3GetOs() {
var pi3 = new RaspberryPi3("pi3-1", "192.168.0.101");
Assertions.assertEquals("RaspberryPiOS", pi3.getOs(), "Expected RaspberryPi3 OS to be 'RaspberryPiOS', but was '" + pi3.getOs() + "'");
}

@Test
@DisplayName("RaspberryPi3 should return correct description")
void testRaspberryPi3GetDescription() {
var pi3 = new RaspberryPi3("pi3-1", "192.168.0.101");
Assertions.assertEquals("Entry-level computer for lightweight tasks", pi3.getDescription(),
"Expected RaspberryPi3 description to be 'Entry-level computer for lightweight tasks', but was '" + pi3.getDescription() + "'");
}

@Test
@DisplayName("RaspberryPi3 should return correct resources")
void testRaspberryPi3GetResources() {
var pi3 = new RaspberryPi3("pi3-1", "192.168.0.101");
var resources = pi3.getResources();
Assertions.assertAll(
() -> Assertions.assertEquals("1GB", resources.getMemory(), "Expected RaspberryPi3 memory to be '1GB', but was '" + resources.getMemory() + "'"),
() -> Assertions.assertEquals("4-core ARM Cortex-A53", resources.getCpu(), "Expected RaspberryPi3 CPU to be '4-core ARM Cortex-A53', but was '" + resources.getCpu() + "'"),
() -> Assertions.assertEquals("16GB", resources.getStorage(), "Expected RaspberryPi3 storage to be '16GB', but was '" + resources.getStorage() + "'")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hu.bme.mit.ase.cps.types.computers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class RaspberryPi4Test {

@Test
@DisplayName("RaspberryPi4 should be constructable with name and IP parameters")
void raspberryPi4ShouldBeConstructable() {
new RaspberryPi4("pi4-1", "192.168.0.102");
}

@Test
@DisplayName("RaspberryPi4 should correctly return name and IP after construction")
void raspberryPi4ConstructorShouldSetFields() {
var pi4 = new RaspberryPi4("pi4-1", "192.168.0.102");
Assertions.assertAll(
() -> Assertions.assertEquals("pi4-1", pi4.getName(), "Expected RaspberryPi4 name to be 'pi4-1', but was '" + pi4.getName() + "'"),
() -> Assertions.assertEquals("192.168.0.102", pi4.getIp(), "Expected RaspberryPi4 IP to be '192.168.0.102', but was '" + pi4.getIp() + "'")
);
}

@Test
@DisplayName("RaspberryPi4 should return correct OS")
void testRaspberryPi4GetOs() {
var pi4 = new RaspberryPi4("pi4-1", "192.168.0.102");
Assertions.assertEquals("RaspberryPiOS", pi4.getOs(), "Expected RaspberryPi4 OS to be 'RaspberryPiOS', but was '" + pi4.getOs() + "'");
}

@Test
@DisplayName("RaspberryPi4 should return correct description")
void testRaspberryPi4GetDescription() {
var pi4 = new RaspberryPi4("pi4-1", "192.168.0.102");
Assertions.assertEquals("Improved model with enhanced performance", pi4.getDescription(),
"Expected RaspberryPi4 description to be 'Improved model with enhanced performance', but was '" + pi4.getDescription() + "'");
}

@Test
@DisplayName("RaspberryPi4 should return correct resources")
void testRaspberryPi4GetResources() {
var pi4 = new RaspberryPi4("pi4-1", "192.168.0.102");
var resources = pi4.getResources();
Assertions.assertAll(
() -> Assertions.assertEquals("4GB", resources.getMemory(), "Expected RaspberryPi4 memory to be '4GB', but was '" + resources.getMemory() + "'"),
() -> Assertions.assertEquals("4-core ARM Cortex-A72", resources.getCpu(), "Expected RaspberryPi4 CPU to be '4-core ARM Cortex-A72', but was '" + resources.getCpu() + "'"),
() -> Assertions.assertEquals("32GB", resources.getStorage(), "Expected RaspberryPi4 storage to be '32GB', but was '" + resources.getStorage() + "'")
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hu.bme.mit.ase.cps.types.computers;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class ServerTest {

@Test
@DisplayName("Server should be constructable with name and IP parameters")
void serverShouldBeConstructable() {
new Server("server-1", "192.168.0.105");
}

@Test
@DisplayName("Server should correctly return name and IP after construction")
void serverConstructorShouldSetFields() {
var server = new Server("server-1", "192.168.0.105");
Assertions.assertAll(
() -> Assertions.assertEquals("server-1", server.getName(), "Expected Server name to be 'server-1', but was '" + server.getName() + "'"),
() -> Assertions.assertEquals("192.168.0.105", server.getIp(), "Expected Server IP to be '192.168.0.105', but was '" + server.getIp() + "'")
);
}

@Test
@DisplayName("Server should return correct OS")
void testServerGetOs() {
var server = new Server("server-1", "192.168.0.105");
Assertions.assertEquals("CentOS", server.getOs(), "Expected Server OS to be 'CentOS', but was '" + server.getOs() + "'");
}

@Test
@DisplayName("Server should return correct description")
void testServerGetDescription() {
var server = new Server("server-1", "192.168.0.105");
Assertions.assertEquals("High-performance server for enterprise applications", server.getDescription(),
"Expected Server description to be 'High-performance server for enterprise applications', but was '" + server.getDescription() + "'");
}

@Test
@DisplayName("Server should return correct resources")
void testServerGetResources() {
var server = new Server("server-1", "192.168.0.105");
var resources = server.getResources();
Assertions.assertAll(
() -> Assertions.assertEquals("64GB", resources.getMemory(), "Expected Server memory to be '64GB', but was '" + resources.getMemory() + "'"),
() -> Assertions.assertEquals("16-core AMD EPYC", resources.getCpu(), "Expected Server CPU to be '16-core AMD EPYC', but was '" + resources.getCpu() + "'"),
() -> Assertions.assertEquals("2TB", resources.getStorage(), "Expected Server storage to be '2TB', but was '" + resources.getStorage() + "'")
);
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,54 @@
package hu.bme.mit.ase.cps.deployments;

import hu.bme.mit.ase.cps.types.computers.Computer;
import hu.bme.mit.ase.cps.types.software.Software;

public abstract class CPS {
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public abstract void installSoftware(Computer computer, String software);
record Connection(Computer source, Computer target) { }

public abstract void deployComputer(Computer computer);
public class CPS {

public abstract void connectComputers(Computer source, Computer target);
private final List<Computer> deployedComputers = new ArrayList<>();
private final Set<Connection> connections = new HashSet<>();

public abstract boolean checkReachability(Computer source, Computer target);
public void deployComputer(Computer computer) {
deployedComputers.add(computer);
}

public List<Computer> getDeployedComputers() {
return deployedComputers;
}

public Computer getComputerByName(String name) {
return getDeployedComputers().stream().filter((computer -> computer.getName().equals(name))).findFirst().orElse(null);
}

public void installSoftware(Computer computer, String software) {

}

public void connectComputers(Computer source, Computer target) {
connections.add(new Connection(source, target));
connections.add(new Connection(target, source));
}

public boolean checkReachability(Computer source, Computer target) {
if (source.equals(target)) {
return true;
}

return hasConnection(source, target);
}

public boolean hasConnection(Computer a, Computer b) {
return connections.contains(new Connection(a, b));
}

public int getNumberOfConnections() {
return connections.size() / 2;
}

}
Loading

0 comments on commit b6abcfb

Please sign in to comment.