Skip to content

Commit

Permalink
Added ChangeSetValidator to detect duplicate ids
Browse files Browse the repository at this point in the history
  • Loading branch information
d0x committed Aug 3, 2014
1 parent 1c0d951 commit ae4d5d9
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 9 deletions.
13 changes: 11 additions & 2 deletions src/main/java/org/mongeez/Mongeez.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
Expand All @@ -17,8 +17,11 @@
import org.mongeez.reader.ChangeSetFileProvider;
import org.mongeez.reader.ChangeSetReaderFactory;
import org.mongeez.reader.FilesetXMLChangeSetFileProvider;
import org.mongeez.validation.ChangeSetsValidator;
import org.mongeez.validation.DefaultChangeSetsValidator;

import com.mongodb.Mongo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
Expand All @@ -33,7 +36,8 @@ public class Mongeez {
private Mongo mongo = null;
private String dbName;
private MongoAuth auth = null;
private ChangeSetFileProvider changeSetFileProvider;
private ChangeSetFileProvider changeSetFileProvider = null;
private ChangeSetsValidator changeSetsValidator = new DefaultChangeSetsValidator();
private String context = null;

public void process() {
Expand All @@ -50,6 +54,7 @@ private List<ChangeSet> getChangeSets() {
changeSets.addAll(readerFactory.getChangeSetReader(file).getChangeSets(file));
}
logChangeSets(changeSets);
changeSetsValidator.validate(changeSets);
return changeSets;
}

Expand Down Expand Up @@ -82,6 +87,10 @@ public void setAuth(MongoAuth auth) {
this.auth = auth;
}

public void setChangeSetsValidator(ChangeSetsValidator changeSetsValidator) {
this.changeSetsValidator = changeSetsValidator;
}

/**
* Convenience method to set the ChangeSetFileProvider to an XML fileset based on the specified file
*/
Expand Down
22 changes: 16 additions & 6 deletions src/main/java/org/mongeez/MongeezRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package org.mongeez;

import com.mongodb.Mongo;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;

import org.mongeez.reader.ChangeSetFileProvider;
import org.mongeez.validation.ChangeSetsValidator;
import org.mongeez.validation.DefaultChangeSetsValidator;

/**
* @author oleksii
Expand All @@ -27,12 +29,14 @@ public class MongeezRunner implements InitializingBean {
private Mongo mongo;
private String dbName;
private Resource file;

private String userName;
private String passWord;

private ChangeSetFileProvider changeSetFileProvider;

private ChangeSetsValidator changeSetsValidator;

@Override
public void afterPropertiesSet() throws Exception {
if (isExecuteEnabled()) {
Expand All @@ -44,11 +48,17 @@ public void execute() {
Mongeez mongeez = new Mongeez();
mongeez.setMongo(mongo);
mongeez.setDbName(dbName);
if(changeSetsValidator != null) {
mongeez.setChangeSetsValidator(changeSetsValidator);
}
else {
mongeez.setChangeSetsValidator(new DefaultChangeSetsValidator());
}
if (changeSetFileProvider != null) {
mongeez.setChangeSetFileProvider(changeSetFileProvider);
} else {
mongeez.setFile(file);

if(!StringUtils.isEmpty(userName) && !StringUtils.isEmpty(passWord)){
MongoAuth auth = new MongoAuth(userName, passWord);
mongeez.setAuth(auth);
Expand Down Expand Up @@ -93,5 +103,5 @@ public void setUserName(String userName) {
public void setPassWord(String passWord) {
this.passWord = passWord;
}

}
10 changes: 10 additions & 0 deletions src/main/java/org/mongeez/validation/ChangeSetsValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mongeez.validation;

import java.util.List;


import org.mongeez.commands.ChangeSet;

public interface ChangeSetsValidator {
public void validate(List<ChangeSet> changeSets) throws ValidationException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.mongeez.validation;

import java.util.List;

import org.mongeez.commands.ChangeSet;

public class DefaultChangeSetsValidator implements ChangeSetsValidator {

@Override
public void validate(List<ChangeSet> changesets) throws ValidationException {
changeSetIdsNotUnique(changesets);
}

private void changeSetIdsNotUnique(List<ChangeSet> changeSets) {
for (int i = 0; i < changeSets.size() / 2; i++) {
ChangeSet changeSetI = changeSets.get(i);
String changeSetIId = changeSetI.getChangeId();

for (int j = i + 1; j < changeSets.size(); j++) {
ChangeSet changeSetJ = changeSets.get(j);
String changeSetJId = changeSetJ.getChangeId();

if (changeSetIId.equals(changeSetJId)) {
throw new ValidationException("ChangeSetId " + changeSetIId + " is not unique.");
}
}
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/org/mongeez/validation/ValidationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.mongeez.validation;

public class ValidationException extends RuntimeException {

public ValidationException() {
super();
}

public ValidationException(String message, Throwable cause) {
super(message);
}

public ValidationException(String message) {
super(message);
}

public ValidationException(Throwable cause) {
super(cause);
}

}
10 changes: 9 additions & 1 deletion src/test/java/org/mongeez/MongeezTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
Expand All @@ -17,6 +17,8 @@
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.Mongo;

import org.mongeez.validation.ValidationException;
import org.springframework.core.io.ClassPathResource;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -144,4 +146,10 @@ public void testChangesWContextContextSetToOrganizations() throws Exception {
assertEquals(db.getCollection("organization").count(), 2);
assertEquals(db.getCollection("house").count(), 2);
}

@Test(groups = "dao", expectedExceptions = ValidationException.class)
public void testFailDuplicateIds() throws Exception {
Mongeez mongeez = create("mongeez_fail_on_duplicate_changeset_ids.xml");
mongeez.process();
}
}
24 changes: 24 additions & 0 deletions src/test/resources/failing_changeset_on_duplicate_ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<mongoChangeLog>
<changeSet changeId="ChangeSet-1" author="d0x">
<script>
db.organization.findOne();
</script>
</changeSet>
<changeSet changeId="ChangeSet-1" author="d0x">
<script>
db.organization.findOne();
</script>
</changeSet>
</mongoChangeLog>
15 changes: 15 additions & 0 deletions src/test/resources/mongeez_fail_on_duplicate_changeset_ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
~ Copyright 2011 SecondMarket Labs, LLC.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
~ on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and limitations under the License.
-->

<changeFiles>
<file path="failing_changeset_on_duplicate_ids.xml"/>
</changeFiles>

0 comments on commit ae4d5d9

Please sign in to comment.