Skip to content

Commit

Permalink
Implement getUpdates api method
Browse files Browse the repository at this point in the history
  • Loading branch information
d10xa committed Jun 13, 2016
1 parent bb18e44 commit f7f075b
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/main/java/ru/d10xa/groovyconsolebot/client/GetUpdates.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 ru.d10xa.groovyconsolebot.client;

import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest;
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse;

import java.io.IOException;

public interface GetUpdates {
GetUpdatesResponse get(GetUpdatesRequest request) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 ru.d10xa.groovyconsolebot.client;

import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest;
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse;

import java.io.IOException;

public class OkHttpGetUpdates implements GetUpdates {

private final GetUpdatesHttpUrlFactory httpUrlFactory;

private final OkHttpClient client;

private final Gson gson;

public OkHttpGetUpdates(
GetUpdatesHttpUrlFactory httpUrlFactory,
OkHttpClient client,
Gson gson) {
this.httpUrlFactory = httpUrlFactory;
this.client = client;
this.gson = gson;
}

@Override
public GetUpdatesResponse get(GetUpdatesRequest getUpdatesRequest) throws IOException {
Request okHttpRequest = new Request.Builder()
.url(httpUrlFactory.build(getUpdatesRequest))
.build();
Response response = client.newCall(okHttpRequest).execute();
handleErrors(response);
return gson.fromJson(response.body().string(), GetUpdatesResponse.class);
}

private void handleErrors(Response response) throws IOException {
if (!response.isSuccessful()) {
System.out.println(response);
throw new IOException(String.valueOf(response.code()));
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 ru.d10xa.groovyconsolebot.client

import com.google.gson.Gson
import okhttp3.OkHttpClient
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesRequest
import ru.d10xa.groovyconsolebot.botapi.GetUpdatesResponse
import spock.lang.Specification

class OkHttpGetUpdatesSpec extends Specification {

MockWebServer server

GetUpdatesHttpUrlFactory httpUrlFactory

def setup() {
server = new MockWebServer()
httpUrlFactory = { server.url("/getUpdates") }
}

def cleanup() {
server.shutdown()
}

def 'one message from private chat'() {
given:
String body = getClass().getClassLoader()
.getResource("json/getUpdates_1465736691.json").text

server.enqueue(new MockResponse().setBody(body))
server.start()

when:
GetUpdates getUpdates = new OkHttpGetUpdates(httpUrlFactory, new OkHttpClient(), new Gson())
GetUpdatesResponse updates = getUpdates.get(new GetUpdatesRequest())

def update0 = updates.result[0]

then:
update0.message.text == "2**3"
server.getRequestCount() == 1
server.takeRequest().body.readUtf8() == ""
}

def 'telegram nginx bad gateway'() {
given:
String body = getClass().getClassLoader()
.getResource("json/getUpdates_nginx_bad_gateway.html").text
server.enqueue(new MockResponse()
.setResponseCode(502)
.setBody(body))

server.start()

when:
GetUpdates getUpdates = new OkHttpGetUpdates(httpUrlFactory, new OkHttpClient(), new Gson())
getUpdates.get(new GetUpdatesRequest())

then:
IOException e = thrown()
e.message == "502"
}

}
7 changes: 7 additions & 0 deletions src/test/resources/json/getUpdates_nginx_bad_gateway.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
<hr><center>nginx/1.9.1</center>
</body>
</html>

0 comments on commit f7f075b

Please sign in to comment.