Changeset 609

Show
Ignore:
Timestamp:
27-03-12 11:12:16 (2 years ago)
Author:
freek.de.bruijn@…
Message:

Changed class to use Mockito.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/code/conceptwiki/service/service-impl/src/test/java/nl/nbic/conceptwiki/service/ConceptServiceImplUnitTest.java

    r554 r609  
    11package nl.nbic.conceptwiki.service; 
    22 
    3 import static nl.nbic.conceptwiki.common.util.CommonTestObjects.ALT_LABEL_B; 
    4 import static nl.nbic.conceptwiki.common.util.CommonTestObjects.CONCEPT_A; 
    5 import static nl.nbic.conceptwiki.common.util.CommonTestObjects.TEST_BRANCH_A_ID; 
    6 import static org.junit.Assert.assertEquals; 
    73import static org.junit.Assert.assertFalse; 
     4import static org.junit.Assert.assertNotNull; 
     5import static org.mockito.Mockito.when; 
    86 
    9 import javax.inject.Inject; 
     7import java.util.ArrayList; 
     8import java.util.List; 
    109 
    11 import nl.nbic.conceptwiki.common.api.Changeset; 
    12 import nl.nbic.conceptwiki.common.api.Concept; 
    13 import nl.nbic.conceptwiki.common.api.Label; 
    14 import nl.nbic.conceptwiki.common.api.Label.LabelType; 
    15 import nl.nbic.conceptwiki.common.api.Language; 
    16 import nl.nbic.conceptwiki.common.impl.ChangesetImpl; 
    17 import nl.nbic.conceptwiki.common.impl.ChangesetImpl.ActionImpl; 
    18 import nl.nbic.conceptwiki.common.impl.ConceptImpl; 
    19 import nl.nbic.conceptwiki.common.impl.LabelImpl; 
    20 import nl.nbic.conceptwiki.common.impl.LanguageImpl; 
    21 import nl.nbic.conceptwiki.service.api.AbstractConceptServiceUnitTest; 
    22 import nl.nbic.conceptwiki.storage.neo4j.GraphTemplate; 
     10import nl.nbic.conceptwiki.common.api.SearchResult; 
     11import nl.nbic.conceptwiki.common.impl.QueryScopeImpl; 
     12import nl.nbic.conceptwiki.common.impl.SearchResultImpl; 
     13import nl.nbic.conceptwiki.common.util.ValidationUtility; 
     14import nl.nbic.conceptwiki.common.util.impl.ValidationUtilityImpl; 
     15import nl.nbic.conceptwiki.service.api.HistoryService; 
     16import nl.nbic.conceptwiki.service.impl.ConceptServiceImpl; 
     17import nl.nbic.conceptwiki.storage.api.ConceptSearchService; 
     18import nl.nbic.conceptwiki.storage.api.ConceptStorageService; 
    2319 
    24 import org.junit.After; 
    25 import org.junit.Before; 
    26 import org.junit.Ignore; 
    2720import org.junit.Test; 
    2821import org.junit.runner.RunWith; 
    29 import org.neo4j.graphdb.Transaction; 
    30 import org.springframework.test.context.ContextConfiguration; 
    31 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
    32 import org.springframework.transaction.annotation.Transactional; 
     22import org.mockito.InjectMocks; 
     23import org.mockito.Mock; 
     24import org.mockito.Mockito; 
     25import org.mockito.Spy; 
     26import org.mockito.runners.MockitoJUnitRunner; 
    3327 
    34 import com.google.common.collect.ImmutableSet; 
    35 import com.google.common.collect.Sets; 
    3628 
    3729/** 
    3830 * Implementation of the AbstractConceptServiceUnitTest tests. 
    3931 * @author <a href="mailto:kees.burger@nbic.nl">Kees Burger</a> 
     32 * @author <a href="mailto:freek.de.bruijn@nbic.nl">Freek de Bruijn</a> 
    4033 */ 
    41 @RunWith(SpringJUnit4ClassRunner.class) 
    42 @ContextConfiguration("/nl/nbic/conceptwiki/service/service-impl-test-context.xml") 
    43 @Ignore // until we figure out the solr unit tests 
    44 public class ConceptServiceImplUnitTest extends AbstractConceptServiceUnitTest { 
    45     /** 
    46      * Graph access utility. 
    47      */ 
    48     @Inject 
    49     private GraphTemplate graphTemplate; 
    50     /** 
    51      * Holds the programmatic transactions to limit the scope of dependencies for handling 
    52      * annotation based transactions in the abstract tests. 
    53      */ 
    54     private Transaction transaction; 
     34@RunWith(MockitoJUnitRunner.class) 
     35public class ConceptServiceImplUnitTest { 
     36        private static final int DEFAULT_LIMIT = 10; 
     37 
     38        /** 
     39         * System under test. 
     40         */ 
     41        @InjectMocks 
     42        private ConceptServiceImpl conceptService; 
     43         
     44        /** 
     45         * Collaborators. 
     46         */ 
     47        @Spy 
     48    private ValidationUtility validationUtility = new ValidationUtilityImpl(); 
     49        @Mock 
     50    private ConceptStorageService mockStorageService; 
     51        @Mock 
     52    private ConceptSearchService mockSearchService; 
     53        @Mock 
     54    private HistoryService mockHistoryService; 
     55         
     56//      public void testSearchConceptsEmptyQuery() 
     57//      public void testSearchConceptsInvalidLimit() 
     58//      public void testSearchConceptsNoResults() 
     59//      public void testSearchConceptsSingleResult() 
     60//      public void testSearchConceptsSingleResultWithLabel() 
     61//      public void testSearchConceptsMultipleResults() 
    5562     
    56     @Before 
    57     public void setUp() { 
    58         transaction = graphTemplate.getGraphDb().beginTx(); 
    59     } 
    60      
    61     @After 
    62     public void tearDown() { 
    63         transaction.failure(); 
    64         transaction.finish(); 
    65     } 
     63        @Test 
     64        public void testSearchConceptsSingleResult() { 
     65                // setup 
     66                final List<SearchResult> searchResults = new ArrayList<SearchResult>(); 
     67                searchResults.add(new SearchResultImpl("malaria", "123456")); 
     68                when(mockSearchService.searchConcepts(Mockito.anyString(), Mockito.anyInt(), Mockito.anyInt())).thenReturn(searchResults); 
    6669 
    67     @Test 
    68     public void testGetConcept() { 
    69         final Concept createdConcept = conceptService.createConcept(CONCEPT_A, TEST_BRANCH_A_ID); 
    70         // commit? 
    71          
    72         final Concept retrievedConcept = conceptService.getConcept(createdConcept.getUuid(), TEST_BRANCH_A_ID); 
    73         assertEquals(createdConcept.getUuid(), retrievedConcept.getUuid()); 
    74         assertEquals(createdConcept, retrievedConcept); 
    75     } 
    76      
    77     @Test 
    78     @Transactional("storage") 
    79     public void testUpdateConcept() { 
    80         final Concept createdConcept = conceptService.createConcept(CONCEPT_A, TEST_BRANCH_A_ID); 
    81         // commit? 
    82          
    83         final Changeset changeset = new ChangesetImpl().withAdd(new ActionImpl() 
    84                 .withLabels(Sets.newHashSet(ALT_LABEL_B))); 
    85          
    86         conceptService.updateConcept(createdConcept.getUuid(), changeset, TEST_BRANCH_A_ID); 
    87          
    88         final Concept retrievedConcept = conceptService.getConcept(createdConcept.getUuid(), TEST_BRANCH_A_ID); 
    89         assertEquals(3, retrievedConcept.getLabels().size()); 
    90     } 
    91      
    92     @Test 
    93     @Transactional("storage") 
    94     public void testFubar() { 
    95         final Language lang_en = new LanguageImpl("english"); 
    96         final Label l1 = new LabelImpl(LabelType.PREFERRED, "a", lang_en); 
    97         final Concept c1 = new ConceptImpl.Builder() 
    98                 .labels(ImmutableSet.of(l1)) 
    99                 .build(); 
    100          
    101         final Concept c2 = conceptService.createConcept(c1, 1); 
    102          
    103         final Label l2 = new LabelImpl(LabelType.PREFERRED, "b", lang_en); 
    104         final Changeset changeset = new ChangesetImpl().withAdd(new ActionImpl().withLabels(ImmutableSet.of(l2))); 
    105         conceptService.updateConcept(c2.getUuid(), changeset, 2); 
    106          
    107         final Concept cb1 = conceptService.getConcept(c2.getUuid(), 1); 
    108         final Concept cb2 = conceptService.getConcept(c2.getUuid(), 2); 
    109          
    110         assertFalse(cb1.equals(cb2)); 
    111     } 
     70                // exercise 
     71                final List<SearchResult> results = conceptService.searchConcepts("test", QueryScopeImpl.LITE_SCOPE, DEFAULT_LIMIT); 
     72                 
     73                // verify 
     74                assertNotNull(results); 
     75                assertFalse(results.isEmpty()); 
     76        } 
    11277}