Advertisement
Guest User

Untitled

a guest
Nov 21st, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.39 KB | None | 0 0
  1. package ch.epfl.sweng.swissaffinity.NetworkTests;
  2.  
  3. import android.support.test.runner.AndroidJUnit4;
  4. import android.test.suitebuilder.annotation.LargeTest;
  5.  
  6. import org.json.JSONArray;
  7. import org.json.JSONException;
  8. import org.json.JSONObject;
  9. import org.junit.Test;
  10. import org.junit.runner.RunWith;
  11.  
  12. import java.io.IOException;
  13. import java.net.HttpURLConnection;
  14. import java.net.URL;
  15. import java.util.ArrayList;
  16. import java.util.Collection;
  17. import java.util.Date;
  18. import java.util.HashSet;
  19. import java.util.List;
  20. import java.util.Set;
  21.  
  22. import ch.epfl.sweng.swissaffinity.events.Establishment;
  23. import ch.epfl.sweng.swissaffinity.events.SpeedDatingEvent;
  24. import ch.epfl.sweng.swissaffinity.users.User;
  25. import ch.epfl.sweng.swissaffinity.utilities.Address;
  26. import ch.epfl.sweng.swissaffinity.utilities.Location;
  27. import ch.epfl.sweng.swissaffinity.utilities.network.DefaultNetworkProvider;
  28. import ch.epfl.sweng.swissaffinity.utilities.network.NetworkProvider;
  29. import ch.epfl.sweng.swissaffinity.utilities.network.ServerTags;
  30. import ch.epfl.sweng.swissaffinity.utilities.network.events.EventClient;
  31. import ch.epfl.sweng.swissaffinity.utilities.network.events.EventClientException;
  32. import ch.epfl.sweng.swissaffinity.utilities.network.events.NetworkEventClient;
  33. import ch.epfl.sweng.swissaffinity.utilities.network.users.NetworkUserClient;
  34. import ch.epfl.sweng.swissaffinity.utilities.network.users.UserClient;
  35. import ch.epfl.sweng.swissaffinity.utilities.network.users.UserClientException;
  36. import ch.epfl.sweng.swissaffinity.utilities.parsers.DateParser;
  37. import ch.epfl.sweng.swissaffinity.utilities.parsers.LocationParser;
  38. import ch.epfl.sweng.swissaffinity.utilities.parsers.ParserException;
  39. import ch.epfl.sweng.swissaffinity.utilities.parsers.SafeJSONObject;
  40.  
  41. import static junit.framework.Assert.assertEquals;
  42. import static junit.framework.Assert.assertTrue;
  43.  
  44. /**
  45.  * Created by Joel on 11/19/2015.
  46.  */
  47. @RunWith(AndroidJUnit4.class)
  48. @LargeTest
  49. public class NetworkEndToEndTest {
  50.     /**Admin (Dario) is used as test user here.
  51.      * */
  52.     @Test
  53.     public void testGetUser() throws UserClientException{
  54.         NetworkProvider networkProvider = new DefaultNetworkProvider();
  55.         UserClient userClient = new NetworkUserClient("http://beecreative.ch", networkProvider);
  56.         User user = userClient.fetchByFacebookID(Integer.toString(1271175799)); //Dario's fb id
  57.         Set<Location> locationsOfInterest = new HashSet<Location>();
  58.         locationsOfInterest.add(new Location(3, "Lausanne"));
  59.         Date birthDay;
  60.         try{
  61.             birthDay = DateParser.parseFromString("1993-02-02T00:00:00+0100");
  62.         } catch(ParserException e){
  63.             throw new UserClientException(e);
  64.         }
  65.  
  66.         assertTrue("Unexpected username",user.getUsername().equals("Admin"));
  67.         assertTrue("Unexpected first name",user.getFirstName().equals("Dario"));
  68.         assertTrue("Unexpected last name",user.getLastName().equals("Anongba"));
  69.         assertTrue("Unexpected profession",user.getProfession().equals("Student"));
  70.         assertTrue("Unexpected home phone",user.getHomePhone().equals(SafeJSONObject.DEFAULT_STRING));
  71.         assertTrue("Unexpected mobile phone",user.getMobilePhone().equals("+41799585615"));
  72.         assertTrue("Unexpected profile picture",user.getProfilePicture().equals(SafeJSONObject.DEFAULT_STRING));
  73.         assertTrue("Unexpected email",user.getEmail().equals("dario.anongba@epfl.ch"));
  74.         assertTrue("Unexpected id",user.getId() == 1);
  75.         assertTrue("Unexpected fb id",user.getFacebookId()==1271175799);
  76.         assertTrue("Unexpected, user should be enabled",user.getEnabled());
  77.         assertTrue("Unexpected, user should not be locked",!user.getLocked());
  78.         assertTrue("Unexpected Adress", new Address("CH", 1007, "Lausanne", "Vaud", 15, "Avenue de Cour")
  79.                 .equals(user.getAddress()));
  80.         assertTrue("Unexpected birthday", birthDay.compareTo(user.getBirthDate()) == 0);
  81.         assertTrue("Unexpected gender", User.Gender.MALE.equals(user.getGender()));
  82.         assertTrue("Unexpected areas of interest",
  83.                 new CollectionComparator<Location>().compare(user.getAreasOfInterest(),locationsOfInterest));
  84.         assertTrue("Unexpected events attended",user.getEventsAttended().size()==0);
  85.  
  86.     }
  87.  
  88.     @Test
  89.     public void postUserTest() throws UserClientException {
  90.         NetworkProvider networkProvider = new DefaultNetworkProvider();
  91.         UserClient userClient = new NetworkUserClient("http://beecreative.ch", networkProvider);
  92.         Set<Location> locationsOfInterest = new HashSet<Location>();
  93.         locationsOfInterest.add(new Location(2, "Genève"));
  94.         locationsOfInterest.add(new Location(3, "Lausanne"));
  95.         locationsOfInterest.add(new Location(4, "Fribourg"));
  96.         locationsOfInterest.add(new Location(5, "Zürich"));
  97.         locationsOfInterest.add(new Location(5, "Berne"));
  98.         locationsOfInterest.add(new Location(5, "Bulle"));
  99.  
  100.         JSONObject jsonUser;
  101.         jsonUser = new JSONObject();
  102.         SafeJSONObject confirmationObject;
  103.         try {
  104.             jsonUser.put("email", "dumbuser666@gmail.com");
  105.             jsonUser.put("username", "DumbUser666");
  106.             jsonUser.put("firstName", "Dumb");
  107.             jsonUser.put("lastName", "User");
  108.             jsonUser.put("gender", "male");
  109.             jsonUser.put("birthDate", "18/02/1993");
  110.             jsonUser.put("facebookId", "666");
  111.             jsonUser.put("plainPassword", "dumbpassword");
  112.             jsonUser.put("rest_user_registration", jsonUser);
  113.             confirmationObject = new SafeJSONObject(userClient.postUser(jsonUser));
  114.         } catch (JSONException e){
  115.             throw new UserClientException(e);
  116.         }
  117.  
  118.         Set<Location> areasOfInterest = new HashSet<>();
  119.         JSONArray areas;
  120.         try {
  121.             areas = confirmationObject.get(ServerTags.LOCATIONS_INTEREST.get(), new JSONArray());
  122.             for (int i = 0; i < areas.length(); i++) {
  123.  
  124.                 JSONObject jsonArea = areas.getJSONObject(i);
  125.                 Location location = new LocationParser().parse(new SafeJSONObject(jsonArea));
  126.                 areasOfInterest.add(location);
  127.             }
  128.         } catch (JSONException | ParserException e){
  129.             throw new UserClientException(e);
  130.         }
  131.  
  132.         assertEquals("Unexpected email", confirmationObject.get(ServerTags.EMAIL.get(), SafeJSONObject.DEFAULT_STRING)
  133.                 .equals("dumbuser666@gmail.com"));
  134.         assertEquals("Unexpected username", confirmationObject.get(ServerTags.USERNAME.get(),
  135.                 SafeJSONObject.DEFAULT_STRING).equals("DumbUser666"));
  136.         assertEquals("Unexpected firstName", confirmationObject.get(ServerTags.FIRST_NAME.get(),
  137.                 SafeJSONObject.DEFAULT_STRING).equals("Dumb"));
  138.         assertEquals("Unexpected lastName", confirmationObject.get(ServerTags.LAST_NAME.get(),
  139.                 SafeJSONObject.DEFAULT_STRING).equals("User"));
  140.         assertEquals("Unexpected gender", confirmationObject.get(ServerTags.GENDER.get(),
  141.                 SafeJSONObject.DEFAULT_STRING).equals("male"));
  142.         assertEquals("Unexpected birthDate", confirmationObject.get(ServerTags.BIRTHDAY.get(),
  143.                 SafeJSONObject.DEFAULT_STRING).equals("18/02/1993"));
  144.         assertEquals("Unexpected facebookId", confirmationObject.get(ServerTags.FACEBOOK_ID.get(),
  145.                 SafeJSONObject.DEFAULT_FB_ID) == 666);
  146.         assertEquals("Unexpected User should not be locked", confirmationObject.get(ServerTags.LOCKED.get(),
  147.                 true) == false);
  148.         assertEquals("Unexpected User should be enable", confirmationObject.get(ServerTags.ENABLED.get(),
  149.                 false) == true);
  150.         assertEquals("Unexpected locations of preference",
  151.                 new CollectionComparator<Location>().compare(locationsOfInterest, areasOfInterest));
  152.         URL url;
  153.         HttpURLConnection conn;
  154.         int respCode;
  155.         try {
  156.             url = new URL("http://beecreative.ch/api/users/DumbUser666");
  157.             conn = (HttpURLConnection) url.openConnection();
  158.             conn.setRequestMethod("DELETE");
  159.             respCode = conn.getResponseCode();
  160.         } catch (IOException e) {
  161.             e.printStackTrace();
  162.         }
  163.     }
  164.  
  165.     @Test
  166.     public void testGetSpeedDatingEvent() throws EventClientException {
  167.         NetworkProvider networkProvider = new DefaultNetworkProvider();
  168.         EventClient eventClient = new NetworkEventClient("http://beecreative.ch", networkProvider);
  169.         SpeedDatingEvent event = (SpeedDatingEvent) eventClient.fetchBy(6);
  170.         //Oktoberfest event at Zurich.
  171.         Date beginDate;
  172.         Date endDate;
  173.         try{
  174.             beginDate = DateParser.parseFromString("2016-10-15T21:00:00+0200");
  175.             endDate = DateParser.parseFromString("2016-10-16T00:00:00+0200");
  176.         } catch (ParserException e){
  177.             throw new EventClientException(e);
  178.         }
  179.  
  180.         assertTrue("Unexpected event name", event.getName().equals("Let's celebrate Oktoberfest"));
  181.         assertTrue("Unexpected event description", event.getDescription().equals("Come and join us" +
  182.                 " to celebrate the Oktoberfest , and use this occasion to meet new People . The event" +
  183.                 " will take place to Forum , a bar in the center of Zürich."));
  184.         assertTrue("Unexcpected event image path", event.getImagePath().equals("5647627162808.jpg"));
  185.         assertTrue("Unexcpected min age", event.getMinAge() == 21);
  186.         assertTrue("Unexpected max age", event.getMaxAge() == 32);
  187.         assertTrue("Unexcpected max people", event.getMaxPeople() == 40);
  188.         assertTrue("Unexpected men seats", event.getMenSeats() == 20);
  189.         assertTrue("Unexpected women seats", event.getWomenSeats() == 20);
  190.         assertTrue("Unexpected women registered", event.getWomenRegistered() == 0);
  191.         assertTrue("Unexpected men registered", event.getMenRegistered() == 0);
  192.         assertTrue("Unexpected base price", event.getBasePrice() == 35.0);
  193.         assertTrue("Unexpected begin date", event.getDateBegin().equals(beginDate));
  194.         assertTrue("Unexpected end date", event.getDateEnd().equals(endDate));
  195.         assertTrue("Unexpected Location", event.getLocation().equals(
  196.                 new Location(6, "Zürich")));
  197.         assertTrue("Unexpected Establishment", event.getEstablishment().equals(
  198.                 new Establishment(2,"Forum", Establishment.Type.BAR,
  199.                         new Address("CH",8004,"Zürich","Zürich",120,"Badenerstrasse" ),
  200.                         "+41 43 243 88 88",
  201.                         "Located at the corner of Badenerstrasse, Forum is an airy lounge bar and restaurant, ideal for kicking back and unwinding.",
  202.                         SafeJSONObject.DEFAULT_STRING,
  203.                         250,
  204.                         SafeJSONObject.DEFAULT_STRING))
  205.         );
  206.     }
  207.  
  208.  
  209.     private class CollectionComparator<E>{
  210.         boolean compare (Collection<E> coll1, Collection<E> coll2){
  211.             if (coll1.size()!=coll2.size()){
  212.                 return false;
  213.             }
  214.             List coll1List = new ArrayList(coll1);
  215.             List coll2List = new ArrayList(coll2);
  216.             for (int i = 0; i < coll1List.size() ; i++) {
  217.                 if(!coll1List.get(i).equals(coll2List.get(i))){
  218.                     return false;
  219.                 }
  220.             }
  221.             return true;
  222.         }
  223.     }
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement