From 1bd85c848ed6895ce99239fb68577033bceff6ef Mon Sep 17 00:00:00 2001 From: Manuel Aranda Date: Wed, 19 Jun 2024 15:30:50 +0200 Subject: [PATCH] refs #451. Testing Operative System --- README.md | 2 +- migrations/Version20240619104206.php | 35 ++++++++ ...grateHardwareAndHardwareProfileCommand.php | 85 ++++++++++++++++++- .../MigrateOperativeSystemCommand.php | 59 +++++++++++++ src/Dto/Input/PartitionInput.php | 7 +- src/Dto/Output/HardwareOutput.php | 10 ++- src/Dto/Output/HardwareProfileOutput.php | 10 +++ src/Dto/Output/PartitionOutput.php | 6 +- src/Entity/OperativeSystem.php | 44 ++++++++++ src/Entity/Partition.php | 30 +++---- src/Factory/OperativeSystemTypeFactory.php | 60 +++++++++++++ src/Factory/PartitionFactory.php | 1 - tests/Functional/PartitionTest.php | 9 +- 13 files changed, 329 insertions(+), 29 deletions(-) create mode 100644 migrations/Version20240619104206.php create mode 100644 src/Command/Migration/MigrateOperativeSystemCommand.php create mode 100644 src/Factory/OperativeSystemTypeFactory.php diff --git a/README.md b/README.md index c74951d..df885a9 100644 --- a/README.md +++ b/README.md @@ -130,6 +130,6 @@ Una vez tengamos la base de datos cargada, podremos ejecutar las migraciones de --- Migraciones de OpenGnsys. --- docker exec ogcore-php php bin/console opengnsys:migration:organizational-unit #cargamos las unidades organizativas docker exec ogcore-php php bin/console opengnsys:migrate-hardware-profiles #cargamos los perfiles de hardware -docker exec ogcore-php php bin/console pengnsys:migration:clients #cargamos los clientes +docker exec ogcore-php php bin/console opengnsys:migration:clients #cargamos los clientes ``` diff --git a/migrations/Version20240619104206.php b/migrations/Version20240619104206.php new file mode 100644 index 0000000..d4654c9 --- /dev/null +++ b/migrations/Version20240619104206.php @@ -0,0 +1,35 @@ +addSql('ALTER TABLE `partition` ADD operative_system_id INT DEFAULT NULL, DROP os_name'); + $this->addSql('ALTER TABLE `partition` ADD CONSTRAINT FK_9EB910E4F1E9F66E FOREIGN KEY (operative_system_id) REFERENCES operative_system (id)'); + $this->addSql('CREATE INDEX IDX_9EB910E4F1E9F66E ON `partition` (operative_system_id)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE `partition` DROP FOREIGN KEY FK_9EB910E4F1E9F66E'); + $this->addSql('DROP INDEX IDX_9EB910E4F1E9F66E ON `partition`'); + $this->addSql('ALTER TABLE `partition` ADD os_name VARCHAR(255) NOT NULL, DROP operative_system_id'); + } +} diff --git a/src/Command/Migration/MigrateHardwareAndHardwareProfileCommand.php b/src/Command/Migration/MigrateHardwareAndHardwareProfileCommand.php index 890f0fd..9ae4946 100644 --- a/src/Command/Migration/MigrateHardwareAndHardwareProfileCommand.php +++ b/src/Command/Migration/MigrateHardwareAndHardwareProfileCommand.php @@ -2,7 +2,9 @@ namespace App\Command\Migration; +use App\Entity\Hardware; use App\Entity\HardwareProfile; +use App\Entity\HardwareType; use App\Entity\OrganizationalUnit; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Query\ResultSetMapping; @@ -31,8 +33,68 @@ class MigrateHardwareAndHardwareProfileCommand extends Command $organizationalUnitRepository = $this->entityManager->getRepository(OrganizationalUnit::class); $hardwareProfileRepository = $this->entityManager->getRepository(HardwareProfile::class); + $hardwareTypeRepository = $this->entityManager->getRepository(HardwareType::class); + $hardwareRepository = $this->entityManager->getRepository(Hardware::class); - /** Obtener los centros de la base de datos antigua **/ + /** Obtener los perfiles hardware de la base de datos antigua **/ + $rsmHardwareTypes = new ResultSetMapping(); + $rsmHardwareTypes->addScalarResult('idtipohardware', 'idtipohardware'); + $rsmHardwareTypes->addScalarResult('descripcion', 'descripcion'); + + $hardwareTypesQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idtipohardware, descripcion FROM tipohardwares', $rsmHardwareTypes); + $hardwareTypes = $hardwareTypesQuery->getResult(); + + $output->writeln("TIPOS HARDWARE TOTAL: ". count($hardwareTypes)); + foreach ($hardwareTypes as $hardwareType){ + $hardwareProfileEntity = null; + $hardwareProfileEntity = $hardwareTypeRepository->findOneBy(['migrationId' => $hardwareType['idtipohardware']]); + if(!$hardwareProfileEntity){ + $hardwareProfileEntity = new HardwareType(); + $hardwareProfileEntity->setMigrationId($hardwareType['idtipohardware']); + $hardwareProfileEntity->setName($hardwareType['descripcion']); + } + + $this->entityManager->persist($hardwareProfileEntity); + } + + /** Obtener los hardware de la base de datos antigua **/ + $rsmHardware = new ResultSetMapping(); + $rsmHardware->addScalarResult('idhardware', 'idhardware'); + $rsmHardware->addScalarResult('idtipohardware', 'hardwares.idtipohardware'); + $rsmHardware->addScalarResult('descripcion', 'descripcion'); + $rsmHardware->addScalarResult('grupoid', 'hardwares.grupoid'); + $rsmHardware->addScalarResult('idcentro', 'hardwares.idcentro'); + + $hardwareQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idhardware, hardwares.idtipohardware, hardwares.descripcion, hardwares.grupoid, hardwares.idcentro FROM hardwares LEFT JOIN tipohardwares ON hardwares.idtipohardware = tipohardwares.idtipohardware', $rsmHardware); + $hardwareCollection = $hardwareQuery->getResult(); + + $output->writeln("HARDWARE TOTAL: ". count($hardwareCollection)); + foreach ($hardwareCollection as $hardware){ + $hardwareEntity = null; + $hardwareEntity = $hardwareRepository->findOneBy(['migrationId' => $hardware['idhardware']]); + if(!$hardwareEntity){ + $hardwareEntity = new Hardware(); + $hardwareEntity->setMigrationId($hardware['idhardware']); + $hardwareEntity->setDescription($hardware['descripcion']); + $hardwareEntity->setName($hardware['descripcion']); + } + + $hardwareType = $hardwareTypeRepository->findOneBy(['migrationId' => $hardware['hardwares.idtipohardware']]); + if ($hardwareType){ + $hardwareEntity->setType($hardwareType); + } + + $migrationId = $hardware['hardwares.grupoid'] === 0 ? $hardware['hardwares.idcentro'] : $hardware['hardwares.grupoid']; + $organizationalUnit = $organizationalUnitRepository->findOneBy(['migrationId' => $migrationId]); + + if ($organizationalUnit){ + $hardwareEntity->setOrganizationalUnit($organizationalUnit); + } + + $this->entityManager->persist($hardwareEntity); + } + + /** Obtener los perfiles hardware de la base de datos antigua **/ $rsmHardwareProfiles = new ResultSetMapping(); $rsmHardwareProfiles->addScalarResult('idperfilhard', 'idperfilhard'); $rsmHardwareProfiles->addScalarResult('descripcion', 'descripcion'); @@ -64,6 +126,27 @@ class MigrateHardwareAndHardwareProfileCommand extends Command $this->entityManager->persist($hardwareProfileEntity); } + + /** Obtener los hardware, y asignarselos a los perfiles hardware **/ + $rsmHardwareProfilesRelation = new ResultSetMapping(); + $rsmHardwareProfilesRelation->addScalarResult('idperfilhard', 'idperfilhard'); + $rsmHardwareProfilesRelation->addScalarResult('idhardware', 'idhardware'); + + $hardwareProfilesQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idperfilhard, idhardware FROM perfileshard_hardwares', $rsmHardwareProfilesRelation); + $hardwareProfileRelations = $hardwareProfilesQuery->getResult(); + + $output->writeln("PERFILES HARDWARE RELACIONES TOTAL: ". count($hardwareProfileRelations)); + foreach ($hardwareProfileRelations as $hardwareProfileRelation){ + $hardwareProfileEntity = $hardwareProfileRepository->findOneBy(['migrationId' => $hardwareProfileRelation['idperfilhard']]); + $hardwareEntity = $hardwareRepository->findOneBy(['migrationId' => $hardwareProfileRelation['idhardware']]); + + if ($hardwareProfileEntity && $hardwareEntity){ + $hardwareProfileEntity->addHardwareCollection($hardwareEntity); + } + $this->entityManager->persist($hardwareProfileEntity); + } + + $this->entityManager->flush(); return Command::SUCCESS; diff --git a/src/Command/Migration/MigrateOperativeSystemCommand.php b/src/Command/Migration/MigrateOperativeSystemCommand.php new file mode 100644 index 0000000..4fec7a6 --- /dev/null +++ b/src/Command/Migration/MigrateOperativeSystemCommand.php @@ -0,0 +1,59 @@ +doctrine->getManager('og_1'); + + $osRepository = $this->entityManager->getRepository(OperativeSystem::class); + + /** Obtener los sistemas operativos de la base de datos antigua **/ + $rsmOperativeSystems = new ResultSetMapping(); + $rsmOperativeSystems->addScalarResult('idnombreso', 'idnombreso'); + $rsmOperativeSystems->addScalarResult('nombreso', 'nombreso'); + + $operativeSystemQuery = $oldDatabaseEntityManager->createNativeQuery('SELECT idnombreso, nombreso FROM nombresos', $rsmOperativeSystems); + $opeativeSystems = $operativeSystemQuery->getResult(); + + /** Sistemas operativos **/ + $output->writeln("SISTEMAS OPERATIVOS TOTAL: ". count($opeativeSystems)); + foreach ($opeativeSystems as $client){ + $osEntity = $osRepository->findOneBy(['migrationId' => $client['idnombreso']]); + if(!$osEntity) { + $osEntity = new OperativeSystem(); + $osEntity->setMigrationId($client['idnombreso']); + $osEntity->setName($client['nombreso']); + } + + $this->entityManager->persist($osEntity); + } + $this->entityManager->flush(); + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Dto/Input/PartitionInput.php b/src/Dto/Input/PartitionInput.php index b4da4ed..03d633a 100644 --- a/src/Dto/Input/PartitionInput.php +++ b/src/Dto/Input/PartitionInput.php @@ -4,6 +4,7 @@ namespace App\Dto\Input; use ApiPlatform\Metadata\ApiProperty; use App\Dto\Output\ClientOutput; +use App\Dto\Output\OperativeSystemOutput; use App\Dto\Output\OrganizationalUnitOutput; use App\Entity\HardwareProfile; use App\Entity\Menu; @@ -41,7 +42,7 @@ final class PartitionInput #[Assert\NotNull()] #[Groups(['partition:write'])] #[ApiProperty(description: 'The operative system name of the partition', example: "Ubuntu")] - public ?string $osName = null; + public ?OperativeSystemOutput $operativeSystem = null; #[Assert\NotNull()] #[Groups(['partition:write'])] @@ -65,7 +66,7 @@ final class PartitionInput $this->size = $partition->getSize(); $this->cacheContent = $partition->getCacheContent(); $this->filesystem = $partition->getFilesystem(); - $this->osName = $partition->getOsName(); + $this->operativeSystem = new OperativeSystemOutput($partition->getOperativeSystem()); $this->client = new ClientOutput($partition->getClient()); $this->memoryUsage = $partition->getMemoryUsage(); } @@ -82,7 +83,7 @@ final class PartitionInput $partition->setSize($this->size * 100); $partition->setCacheContent($this->cacheContent); $partition->setFilesystem($this->filesystem); - $partition->setOsName($this->osName); + $partition->setOperativeSystem($this->operativeSystem->getEntity()); $partition->setClient($this->client->getEntity()); $partition->setMemoryUsage($this->memoryUsage * 100); diff --git a/src/Dto/Output/HardwareOutput.php b/src/Dto/Output/HardwareOutput.php index ae879d1..1c0e0b8 100644 --- a/src/Dto/Output/HardwareOutput.php +++ b/src/Dto/Output/HardwareOutput.php @@ -2,6 +2,7 @@ namespace App\Dto\Output; +use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\Get; use App\Entity\Hardware; use Symfony\Component\Serializer\Annotation\Groups; @@ -16,13 +17,14 @@ final class HardwareOutput extends AbstractOutput public ?string $description = ''; #[Groups(['hardware:read'])] + #[ApiProperty(readableLink: true)] public ?HardwareTypeOutput $type = null; #[Groups(['hardware:read'])] - public \DateTime $createAt; + public \DateTime $createdAt; #[Groups(['hardware:read'])] - public ?string $createBy = null; + public ?string $createdBy = null; public function __construct(Hardware $hardware) { @@ -31,7 +33,7 @@ final class HardwareOutput extends AbstractOutput $this->name = $hardware->getName(); $this->description = $hardware->getDescription(); $this->type = new HardwareTypeOutput($hardware->getType()); - $this->createAt = $hardware->getCreatedAt(); - $this->createBy = $hardware->getCreatedBy(); + $this->createdAt = $hardware->getCreatedAt(); + $this->createdBy = $hardware->getCreatedBy(); } } \ No newline at end of file diff --git a/src/Dto/Output/HardwareProfileOutput.php b/src/Dto/Output/HardwareProfileOutput.php index 66a91ce..a0a8cac 100644 --- a/src/Dto/Output/HardwareProfileOutput.php +++ b/src/Dto/Output/HardwareProfileOutput.php @@ -2,7 +2,9 @@ namespace App\Dto\Output; +use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\Get; +use App\Entity\Hardware; use App\Entity\HardwareProfile; use Symfony\Component\Serializer\Annotation\Groups; @@ -18,6 +20,9 @@ final class HardwareProfileOutput extends AbstractOutput #[Groups(['hardware-profile:read'])] public ?OrganizationalUnitOutput $organizationalUnit = null; + #[Groups(['hardware-profile:read'])] + public ?array $hardwareCollection = []; + #[Groups(['hardware-profile:read'])] public \DateTime $createdAt; @@ -33,6 +38,11 @@ final class HardwareProfileOutput extends AbstractOutput if($hardwareProfile->getOrganizationalUnit()) { $this->organizationalUnit = new OrganizationalUnitOutput($hardwareProfile->getOrganizationalUnit()); } + + $this->hardwareCollection = $hardwareProfile->getHardwareCollection()->map( + fn(Hardware $hardware) => new HardwareOutput($hardware) + )->toArray(); + $this->createdAt = $hardwareProfile->getCreatedAt(); $this->createdBy = $hardwareProfile->getCreatedBy(); } diff --git a/src/Dto/Output/PartitionOutput.php b/src/Dto/Output/PartitionOutput.php index 483aab7..f6eaadf 100644 --- a/src/Dto/Output/PartitionOutput.php +++ b/src/Dto/Output/PartitionOutput.php @@ -28,7 +28,7 @@ class PartitionOutput extends AbstractOutput public ?string $filesystem = null; #[Groups(['partition:read', 'client:read'])] - public ?string $osName = null; + public ?OperativeSystemOutput $operativeSystem = null; #[Groups(['partition:read'])] public ?int $memoryUsage = null; @@ -43,7 +43,9 @@ class PartitionOutput extends AbstractOutput $this->size = $partition->getSize() / 100; $this->cacheContent = $partition->getCacheContent(); $this->filesystem = $partition->getFilesystem(); - $this->osName = $partition->getOsName(); + if ($partition->getOperativeSystem()) { + $this->operativeSystem = new OperativeSystemOutput($partition->getOperativeSystem()); + } $this->memoryUsage = $partition->getMemoryUsage() / 100; } } \ No newline at end of file diff --git a/src/Entity/OperativeSystem.php b/src/Entity/OperativeSystem.php index 063fa5a..1f197fd 100644 --- a/src/Entity/OperativeSystem.php +++ b/src/Entity/OperativeSystem.php @@ -3,10 +3,54 @@ namespace App\Entity; use App\Repository\OperativeSystemRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: OperativeSystemRepository::class)] class OperativeSystem extends AbstractEntity { use NameableTrait; + + /** + * @var Collection + */ + #[ORM\OneToMany(mappedBy: 'operativeSystem', targetEntity: Partition::class)] + private Collection $partitions; + + public function __construct() + { + parent::__construct(); + $this->partitions = new ArrayCollection(); + } + + /** + * @return Collection + */ + public function getPartitions(): Collection + { + return $this->partitions; + } + + public function addPartition(Partition $partition): static + { + if (!$this->partitions->contains($partition)) { + $this->partitions->add($partition); + $partition->setOperativeSystem($this); + } + + return $this; + } + + public function removePartition(Partition $partition): static + { + if ($this->partitions->removeElement($partition)) { + // set the owning side to null (unless already changed) + if ($partition->getOperativeSystem() === $this) { + $partition->setOperativeSystem(null); + } + } + + return $this; + } } diff --git a/src/Entity/Partition.php b/src/Entity/Partition.php index 316eb28..c7af2c1 100644 --- a/src/Entity/Partition.php +++ b/src/Entity/Partition.php @@ -27,15 +27,15 @@ class Partition extends AbstractEntity #[ORM\Column(length: 255, nullable: true)] private ?string $filesystem = null; - #[ORM\Column(length: 255)] - private ?string $osName = null; - #[ORM\ManyToOne(inversedBy: 'partitions')] private ?Client $client = null; #[ORM\Column] private ?int $memoryUsage = null; + #[ORM\ManyToOne(inversedBy: 'partitions')] + private ?OperativeSystem $operativeSystem = null; + public function getDiskNumber(): ?int { return $this->diskNumber; @@ -108,18 +108,6 @@ class Partition extends AbstractEntity return $this; } - public function getOsName(): ?string - { - return $this->osName; - } - - public function setOsName(string $osName): static - { - $this->osName = $osName; - - return $this; - } - public function getClient(): ?Client { return $this->client; @@ -143,4 +131,16 @@ class Partition extends AbstractEntity return $this; } + + public function getOperativeSystem(): ?OperativeSystem + { + return $this->operativeSystem; + } + + public function setOperativeSystem(?OperativeSystem $operativeSystem): static + { + $this->operativeSystem = $operativeSystem; + + return $this; + } } diff --git a/src/Factory/OperativeSystemTypeFactory.php b/src/Factory/OperativeSystemTypeFactory.php new file mode 100644 index 0000000..c7d8f32 --- /dev/null +++ b/src/Factory/OperativeSystemTypeFactory.php @@ -0,0 +1,60 @@ + + */ +final class OperativeSystemTypeFactory extends ModelFactory +{ + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#factories-as-services + * + * @todo inject services if required + */ + public function __construct() + { + parent::__construct(); + } + + public static function class(): string + { + return OperativeSystemType::class; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#model-factories + * + * @todo add your default values here + */ + protected function getDefaults(): array + { + return [ + 'createdAt' => self::faker()->dateTime(), + 'name' => self::faker()->text(255), + 'updatedAt' => self::faker()->dateTime(), + ]; + } + + /** + * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#initialization + */ + protected function initialize(): static + { + return $this + // ->afterInstantiate(function(OperativeSystemType $operativeSystemType): void {}) + ; + } + + protected static function getClass(): string + { + return OperativeSystemType::class; + } +} diff --git a/src/Factory/PartitionFactory.php b/src/Factory/PartitionFactory.php index ba15bc4..397ef82 100644 --- a/src/Factory/PartitionFactory.php +++ b/src/Factory/PartitionFactory.php @@ -39,7 +39,6 @@ final class PartitionFactory extends ModelFactory return [ 'createdAt' => self::faker()->dateTime(), 'memoryUsage' => self::faker()->randomNumber(), - 'osName' => self::faker()->text(255), 'size' => self::faker()->randomNumber(), 'updatedAt' => self::faker()->dateTime() ]; diff --git a/tests/Functional/PartitionTest.php b/tests/Functional/PartitionTest.php index 0265dc6..0c5a577 100644 --- a/tests/Functional/PartitionTest.php +++ b/tests/Functional/PartitionTest.php @@ -4,10 +4,12 @@ namespace Functional; use App\Entity\Client; use App\Entity\Menu; +use App\Entity\OperativeSystem; use App\Entity\Partition; use App\Factory\ClientFactory; use App\Factory\HardwareProfileFactory; use App\Factory\MenuFactory; +use App\Factory\OperativeSystemFactory; use App\Factory\OrganizationalUnitFactory; use App\Factory\PartitionFactory; use App\Factory\UserFactory; @@ -73,9 +75,12 @@ class PartitionTest extends AbstractTest ClientFactory::createOne(['name' => self::CLIENT_CREATE, 'serialNumber' => '123abc', 'organizationalUnit' => $ou, 'hardwareProfile' => $hp]); $iri = $this->findIriBy(Client::class, ['name' => self::CLIENT_CREATE]); + OperativeSystemFactory::createOne(['name' => 'Ubuntu']); + $osIri = $this->findIriBy(OperativeSystem::class, ['name' => 'Ubuntu']); + $this->createClientWithCredentials()->request('POST', '/partitions',['json' => [ 'size' => 100, - 'osName' => 'Ubuntu', + 'operativeSystem' => $osIri, 'client' => $iri, 'memoryUsage' => 100 ]]); @@ -86,7 +91,7 @@ class PartitionTest extends AbstractTest '@context' => '/contexts/PartitionOutput', '@type' => 'Partition', 'size' => 100, - 'osName' => 'Ubuntu', + 'operativeSystem' => $osIri, 'memoryUsage' => 100 ]); }