package org.matrix.androidsdk;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import junit.framework.Assert;
import junit.framework.TestCase;

import org.json.JSONObject;
import org.matrix.androidsdk.api.EventsApi;
import org.matrix.androidsdk.api.response.Event;
import org.matrix.androidsdk.api.response.InitialSyncResponse;
import org.matrix.androidsdk.api.response.PublicRoom;
import org.matrix.androidsdk.api.response.TokensChunkResponse;
import org.matrix.androidsdk.test.RetrofitUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.http.Path;
import retrofit.http.Query;
import retrofit.client.Response;
import retrofit.mime.TypedInput;


public class MXApiServiceTest extends TestCase {

    private static final String BASE_URL = "http://localhost:8008/_matrix/client/api/v1";

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testPublicRooms() {
        Gson gson = new Gson();
        final TokensChunkResponse<PublicRoom> roomsChunk = new TokensChunkResponse<PublicRoom>();
        roomsChunk.chunk = new ArrayList<PublicRoom>();

        EventsApi eventsApi = new EventsApi() {

            @Override
            public TokensChunkResponse<Event> events(@Query("from") String from,
                                                     @Query("timeout") int timeout) {
                return null;
            }

            @Override
            public JsonObject events(@Path("eventId") String eventId) {
                return null;
            }

            @Override
            public void publicRooms(Callback<TokensChunkResponse<PublicRoom>> callback) {
                try {
                    Response response = RetrofitUtils.createJsonResponse(BASE_URL, 200,
                            new JSONObject());
                    callback.success(roomsChunk, response);
                }
                catch (Exception e) {
                    Assert.assertTrue("Exception thrown: "+e, false);
                }

            }

            @Override
            public InitialSyncResponse initialSync(@Query("limit") int limit) {
                return null;
            }
        };


        MXApiService service = new MXApiService(eventsApi);
        service.loadPublicRooms(new MXApiService.LoadPublicRoomsCallback() {
            @Override
            public void onRoomsLoaded(List<PublicRoom> publicRooms) {
                Assert.assertEquals(0, publicRooms.size());
            }
        });

    }
}

@@ -48,20 +48,98 @@
 le.gson.
-Gson
+FieldNamingPolicy;%0Aimport com.google.gson.Gson;%0Aimport com.google.gson.GsonBuilder
 ;%0Aimport
@@ -171,39 +171,50 @@
 ct;%0A
-%0A
 import 
-junit.framework.Assert;
+com.google.gson.reflect.TypeToken;%0A
 %0Aimp
@@ -244,16 +244,43 @@
 tCase;%0A%0A
+import org.json.JSONArray;%0A
 import o
@@ -1216,193 +1216,919 @@
 s() 
-%7B%0A        Gson gson = new Gson();%0A        final TokensChunkResponse%3CPublicRoom%3E roomsChunk = new TokensChunkResponse%3CPublicRoom%3E();%0A        roomsChunk.chunk = new ArrayList%3CPublicRoom%3E(
+throws Exception %7B%0A        final String roomId = %22!faifuhew9:localhost%22;%0A        final String roomTopic = %22This is a test room.%22;%0A        final String roomName = %22Test Room%22;%0A        final int roomMembers = 6;%0A%0A        final JSONObject json = new JSONObject();%0A        json.put(%22start%22,%22abc%22);%0A        json.put(%22end%22, %22def%22);%0A        JSONArray rooms = new JSONArray();%0A%0A        JSONObject room = new JSONObject().put(%22name%22, roomName).put(%22num_joined_members%22, roomMembers)%0A            .put(%22room_id%22, roomId).put(%22topic%22, roomTopic);%0A        rooms.put(room);%0A        json.put(%22chunk%22, rooms);%0A%0A        Gson gson = new GsonBuilder()%0A                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)%0A                .create();%0A        final TokensChunkResponse%3CPublicRoom%3E roomsChunk = gson.fromJson(json.toString(),%0A                new TypeToken%3CTokensChunkResponse%3CPublicRoom%3E%3E()%7B%7D.getType()
 );%0A%0A
@@ -2655,24 +2655,66 @@
 callback) %7B%0A
+                Response response = null;%0A
             
@@ -2739,33 +2739,24 @@
             
-Response 
 response = R
@@ -2834,84 +2834,16 @@
     
-new JSONObject());%0A                    callback.success(roomsChunk, response
+    json
 );%0A 
@@ -2913,31 +2913,24 @@
             
-Assert.
 assertTrue(%22
@@ -2970,32 +2970,87 @@
               %7D%0A
+                callback.success(roomsChunk, response);
 %0A            %7D%0A%0A
@@ -3456,15 +3456,8 @@
     
-Assert.
 asse
@@ -3465,17 +3465,17 @@
 tEquals(
-0
+1
 , public
@@ -3489,16 +3489,281 @@
 ize());%0A
+                PublicRoom pr = publicRooms.get(0);%0A                assertEquals(roomName, pr.name);%0A                assertEquals(roomId, pr.roomId);%0A                assertEquals(roomTopic, pr.topic);%0A                assertEquals(roomMembers, pr.numJoinedMembers);%0A
         

Define the public room returned from the HS and verify that the correct info comes back in the callback.