Advertisement
Kslash22

ServiceUserTest

Jul 27th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.26 KB | None | 0 0
  1. package org.ly.server.application.persistence.service;
  2.  
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import org.ly.server.application.persistence.model.accounting.ModelUser;
  6. import org.lyj.commons.util.CollectionUtils;
  7. import org.lyj.commons.util.FormatUtils;
  8. import org.lyj.commons.util.RandomUtils;
  9. import org.lyj.server.TestInitializer;
  10.  
  11. import static org.junit.Assert.assertEquals;
  12.  
  13. public class ServiceUserTest {
  14.  
  15.     // ------------------------------------------------------------------------
  16.     //                      c o n s t
  17.     // ------------------------------------------------------------------------
  18.     private final String NAME = "Marco %s";
  19.     private final String[] DATA = {"Rossi", "Bianchi", "Verdi", "Azzurri"};
  20.  
  21.     @Before
  22.     public void setUp() throws Exception {
  23.         TestInitializer.init();
  24.     }
  25.  
  26.     @Test
  27.     public void appTest() {
  28.         long count = ServiceUser.instance().count();
  29.         if(count <10000){
  30.             create10000Users(NAME);
  31.         }
  32.  
  33.     }
  34. // ------------------------------------------------------------------------
  35. //                      p r i v a t e
  36. // ------------------------------------------------------------------------
  37.     private void create10000Users(final String name){
  38.        
  39.         //generate random alias
  40.         for(int i=0; i<=1000; i++){
  41.             final int n = (int)RandomUtils.rnd(0, DATA.length-1);
  42.             final String alias = CollectionUtils.get(DATA,n,"peppapig");
  43.  
  44.         //combine with username
  45.         final String fullname = FormatUtils.format(name,alias);
  46.  
  47.         final String email = RandomUtils.randomAlphanumericLower(8)+ "@sergiomottola.it";
  48.  
  49.         final ModelUser user = new ModelUser();
  50.         user.name(fullname);
  51.         user.email(email);
  52.  
  53.             System.out.println(user.name());
  54.             ServiceUser.instance().upsert(user);
  55.         }
  56.  
  57.     }
  58.  
  59.     @Test
  60.     public void enumTest(){
  61.         ServiceUser.instance().forEach((user)->{
  62.             System.out.println(user.name());
  63.         });
  64.     }
  65.  
  66.     @Test
  67.     public void alias(){
  68.         for(final String ins : DATA) {
  69.             String fullname =  FormatUtils.format(NAME, ins);
  70.             long count = ServiceUser.instance().countByName(fullname);
  71.             System.out.println(fullname + ": " + count);
  72.         }
  73.     }
  74.  
  75.     @Test
  76.     public void pageTest(){
  77.         ModelUser[] userArray = ServiceUser.instance()
  78.                 .getPage(0,100)
  79.                 .toArray(new ModelUser[0]);
  80.  
  81.         assertEquals(userArray.length,100);
  82.  
  83.         ModelUser[] page1 = ServiceUser.instance()
  84.                 .getPage(0,10)
  85.                 .toArray(new ModelUser[0]);
  86.  
  87.         ModelUser[] page2 = ServiceUser.instance()
  88.                 .getPage(10,10)
  89.                 .toArray(new ModelUser[0]);
  90.  
  91.         assertEquals(page1.length,10);
  92.         assertEquals(page2.length,10);
  93.  
  94.         for(int i = 0; i<10; i++){
  95.             final String key_1 = userArray[i].key();
  96.             final String key_2 = page1[i].key();
  97.             assertEquals(key_1,key_2);
  98.         }
  99.  
  100.         for(int i = 0; i<10; i++){
  101.             final String key_1 = userArray[i+10].key();
  102.             final String key_2 = page2[i].key();
  103.             assertEquals(key_1,key_2);
  104.         }
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement