Skip to content

Commit

Permalink
Merge pull request #191 from nitin-ebi/setWriteResultChecking
Browse files Browse the repository at this point in the history
EVA-3375  set WriteResultChecking to Exception
  • Loading branch information
nitin-ebi authored Sep 12, 2023
2 parents 31144b5 + bd8665d commit f280604
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
import org.springframework.data.mongodb.core.WriteResultChecking;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
Expand Down Expand Up @@ -82,6 +83,7 @@ public static MongoTemplate getMongoTemplate(String databaseName, MongoConnectio
MappingMongoConverter mappingMongoConverter = getMappingMongoConverter(mongoFactory, mongoMappingContext);
MongoTemplate mongoTemplate = new MongoTemplate(mongoFactory, mappingMongoConverter);
mongoTemplate.setWriteConcern(WriteConcern.MAJORITY);
mongoTemplate.setWriteResultChecking(WriteResultChecking.EXCEPTION);
return mongoTemplate;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright 2023 EMBL - European Bioinformatics Institute
*
* 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 uk.ac.ebi.eva.pipeline.configuration;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.dao.DataAccessException;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import uk.ac.ebi.eva.commons.models.metadata.AnnotationMetadata;

import static org.mockito.Mockito.doThrow;

@RunWith(SpringRunner.class)
public class MongoTemplateTest {
@MockBean
private MongoTemplate mongoTemplate;

@Rule
public ExpectedException exceptionRule = ExpectedException.none();

@Before
public void setUp() {

}

@Test(expected = DataAccessException.class)
public void testMongoTemplateWriteResultChecking() {
AnnotationMetadata annotationMetadata = new AnnotationMetadata("vep_1", "vep_cache_1", true);
doThrow(new DataAccessException("Simulated exception") {})
.when(mongoTemplate).save(annotationMetadata, "AnnotationMetadata");

mongoTemplate.save(annotationMetadata, "AnnotationMetadata");
}
}

0 comments on commit f280604

Please sign in to comment.