modules/enterprise/server/plugins/drift-mongodb/src/main/java/org/rhq/enterprise/server/plugins/drift/mongodb/Mapper.java | 65 ++++++++++ modules/enterprise/server/plugins/drift-mongodb/src/test/java/org/rhq/enterprise/server/plugins/drift/mongodb/MapperTest.java | 65 ++++++++++ 2 files changed, 130 insertions(+)
New commits: commit 8f06ccce4935edf163141f5ef473efd4dece0929 Author: John Sanda jsanda@redhat.com Date: Tue Feb 28 10:29:59 2012 -0500
Initial commit for Mapper and MapperTest
This commit starts refactoring code that maps MongoDB entities into DTOs into Mapper.java. Having a distinct DTO mapping layer makes it easier to test and also easier to maintain.
diff --git a/modules/enterprise/server/plugins/drift-mongodb/src/main/java/org/rhq/enterprise/server/plugins/drift/mongodb/Mapper.java b/modules/enterprise/server/plugins/drift-mongodb/src/main/java/org/rhq/enterprise/server/plugins/drift/mongodb/Mapper.java new file mode 100644 index 0000000..89ceaea --- /dev/null +++ b/modules/enterprise/server/plugins/drift-mongodb/src/main/java/org/rhq/enterprise/server/plugins/drift/mongodb/Mapper.java @@ -0,0 +1,65 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package org.rhq.enterprise.server.plugins.drift.mongodb; + +import org.rhq.core.domain.drift.dto.DriftChangeSetDTO; +import org.rhq.core.domain.drift.dto.DriftDTO; +import org.rhq.core.domain.drift.dto.DriftFileDTO; +import org.rhq.enterprise.server.plugins.drift.mongodb.entities.MongoDBChangeSet; +import org.rhq.enterprise.server.plugins.drift.mongodb.entities.MongoDBChangeSetEntry; + +public class Mapper { + + public DriftChangeSetDTO toDTO(MongoDBChangeSet changeSet) { + DriftChangeSetDTO dto = new DriftChangeSetDTO(); + dto.setId(changeSet.getId()); + dto.setResourceId(changeSet.getResourceId()); + dto.setDriftDefinitionId(changeSet.getDriftDefinitionId()); + dto.setDriftHandlingMode(changeSet.getDriftHandlingMode()); + dto.setVersion(changeSet.getVersion()); + dto.setCtime(changeSet.getCtime()); + dto.setCategory(changeSet.getCategory()); + + return dto; + } + + public DriftDTO toDTO(MongoDBChangeSetEntry entry) { + DriftDTO dto = new DriftDTO(); + dto.setId(entry.getId()); + dto.setCategory(entry.getCategory()); + dto.setCtime(entry.getCtime()); + dto.setPath(entry.getPath()); + dto.setDirectory(entry.getDirectory()); + if (entry.getNewFileHash() != null) { + dto.setNewDriftFile(newDriftFileDTO(entry.getNewFileHash())); + } + if (entry.getOldFileHash() != null) { + dto.setOldDriftFile(newDriftFileDTO(entry.getOldFileHash())); + } + + return null; + } + + private DriftFileDTO newDriftFileDTO(String hash) { + DriftFileDTO dto = new DriftFileDTO(); + dto.setHashId(hash); + return dto; + } + +} diff --git a/modules/enterprise/server/plugins/drift-mongodb/src/test/java/org/rhq/enterprise/server/plugins/drift/mongodb/MapperTest.java b/modules/enterprise/server/plugins/drift-mongodb/src/test/java/org/rhq/enterprise/server/plugins/drift/mongodb/MapperTest.java new file mode 100644 index 0000000..bf7b62c --- /dev/null +++ b/modules/enterprise/server/plugins/drift-mongodb/src/test/java/org/rhq/enterprise/server/plugins/drift/mongodb/MapperTest.java @@ -0,0 +1,65 @@ +/* + * RHQ Management Platform + * Copyright (C) 2005-2008 Red Hat, Inc. + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +package org.rhq.enterprise.server.plugins.drift.mongodb; + +import org.bson.types.ObjectId; +import org.rhq.core.domain.drift.dto.DriftChangeSetDTO; +import org.rhq.enterprise.server.plugins.drift.mongodb.entities.MongoDBChangeSet; +import org.testng.annotations.Test; + +import static org.rhq.core.domain.drift.DriftChangeSetCategory.DRIFT; +import static org.rhq.core.domain.drift.DriftConfigurationDefinition.DriftHandlingMode.normal; +import static org.rhq.test.AssertUtils.assertPropertiesMatch; + +public class MapperTest { + + @Test + public void mapChangeSetToDTO() { + MongoDBChangeSet changeSet = new MongoDBChangeSet(); + changeSet.setId(new ObjectId()); + changeSet.setCategory(DRIFT); + changeSet.setDriftDefinitionId(1); + changeSet.setDriftDefinitionName("testdef"); + changeSet.setResourceId(1); + changeSet.setVersion(1); + changeSet.setDriftHandlingMode(normal); + + Mapper mapper = new Mapper(); + DriftChangeSetDTO actual = mapper.toDTO(changeSet); + + DriftChangeSetDTO expected = new DriftChangeSetDTO(); + expected.setId(changeSet.getId()); + expected.setCategory(changeSet.getCategory()); + expected.setCtime(changeSet.getCtime()); + expected.setDriftDefinitionId(changeSet.getDriftDefinitionId()); + expected.setResourceId(changeSet.getResourceId()); + expected.setDriftHandlingMode(changeSet.getDriftHandlingMode()); + expected.setVersion(changeSet.getVersion()); + + assertPropertiesMatch(expected, actual, "Failed to map " + MongoDBChangeSet.class.getSimpleName() + " to " + + DriftChangeSetDTO.class.getSimpleName()); + } + +// @Test +// public void mapNewEntryToDTO() { +// MongoDBChangeSetEntry entry = new MongoDBChangeSetEntry("./foo", FILE_ADDED); +// entry.setNewFileHash("1ab2c34d"); +// } + +}
rhq-commits@lists.fedorahosted.org