/*
 * Copyright 2015 John Scattergood
 *
 * 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 weatherAlarm.endpoints;

import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.http.HttpMethod;
import io.reactivex.netty.protocol.http.server.HttpServerRequest;
import io.reactivex.netty.protocol.http.server.HttpServerResponse;
import org.easymock.Capture;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import rx.Observable;
import weatherAlarm.model.WeatherAlarm;
import weatherAlarm.services.IWeatherAlarmService;
import weatherAlarm.util.TestUtils;

public class WeatherAlarmEndpointTest {

    @Test
    public void testHandle() throws Exception {
        IWeatherAlarmService alarmService = TestUtils.getMockAlarmService();
        WeatherAlarm alarm = alarmService.getAlarms().get(0);
        WeatherAlarmEndpoint alarmEndpoint = new WeatherAlarmEndpoint();
        alarmEndpoint.setAlarmService(alarmService);

        Capture<byte[]> written = EasyMock.newCapture();
        HttpServerRequest<ByteBuf> mockServerRequest = createMockHttpServerRequest(HttpMethod.GET, "/weatherAlarm");
        HttpServerResponse<ByteBuf> mockHttpResponse = createMockHttpResponse(written);
        alarmEndpoint.handle(mockServerRequest, mockHttpResponse);
        Assert.assertTrue("Expected not null value", written.getValue().length > 0);
    }

    @SuppressWarnings("unchecked")
    private HttpServerRequest<ByteBuf> createMockHttpServerRequest(HttpMethod method, String uri) {
        HttpServerRequest<ByteBuf> mockServerRequest = EasyMock.createMock(HttpServerRequest.class);
        EasyMock.expect(mockServerRequest.getUri()).andReturn(uri).anyTimes();
        EasyMock.expect(mockServerRequest.getHttpMethod()).andReturn(method).anyTimes();
        EasyMock.replay(mockServerRequest);
        return mockServerRequest;
    }

    @SuppressWarnings("unchecked")
    private HttpServerResponse<ByteBuf> createMockHttpResponse(Capture<byte[]> capture) {
        HttpServerResponse<ByteBuf> mockResponse = EasyMock.createMock(HttpServerResponse.class);
        mockResponse.writeBytes(EasyMock.capture(capture));
        EasyMock.expect(mockResponse.close()).andReturn(Observable.empty()).anyTimes();
        EasyMock.replay(mockResponse);
        return mockResponse;
    }
}
@@ -839,16 +839,62 @@
 sponse;%0A
+import org.codehaus.jackson.map.ObjectMapper;%0A
 import o
@@ -914,16 +914,16 @@
 apture;%0A
-
 import o
@@ -1018,48 +1018,8 @@
 le;%0A
-import weatherAlarm.model.WeatherAlarm;%0A
 impo
@@ -1102,16 +1102,42 @@
 Utils;%0A%0A
+import java.util.Arrays;%0A%0A
 public c
@@ -1205,16 +1205,32 @@
 stHandle
+RequestForAlarms
 () throw
@@ -1324,70 +1324,8 @@
 ();%0A
-        WeatherAlarm alarm = alarmService.getAlarms().get(0);%0A
     
@@ -1768,32 +1768,122 @@
 kHttpResponse);%0A
+        byte%5B%5D expected = new ObjectMapper().writeValueAsBytes(alarmService.getAlarms());%0A
         Assert.a
@@ -1897,17 +1897,19 @@
 ue(%22
-E
+Une
 xpected 
 not 
@@ -1908,24 +1908,57 @@
 ted 
-not null
+value in written
  value
+ 
 %22,
+ Arrays.equals(expected,
  wri
@@ -1976,19 +1976,9 @@
 ue()
-.length %3E 0
+)
 );%0A 

Add test for weather alarm endpoint