-
-
Save bsideup/86c952cad0c61a53db70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'java' | |
apply plugin: 'spring-boot' | |
buildscript { | |
repositories { | |
jcenter() | |
} | |
dependencies { | |
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.1.RELEASE") | |
} | |
} | |
repositories { | |
jcenter() | |
} | |
sourceCompatibility = 1.8 | |
targetCompatibility = 1.8 | |
dependencies { | |
compile "org.projectlombok:lombok:1.16.2" | |
compile("org.springframework.boot:spring-boot-starter-web") | |
compile 'com.facebook.swift:swift-annotations:0.14.1' | |
compile 'com.facebook.swift:swift-service:0.14.1' | |
compile 'com.facebook.swift:swift-codec:0.14.1' | |
compile 'com.facebook.nifty:nifty-core:0.14.1' | |
compile 'org.apache.thrift:libthrift:0.9.1' | |
testCompile "org.springframework.boot:spring-boot-starter-test" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator; | |
import com.example.calculator.protocol.TCalculatorService; | |
import com.facebook.nifty.processor.NiftyProcessorAdapters; | |
import com.facebook.swift.codec.ThriftCodecManager; | |
import com.facebook.swift.service.ThriftEventHandler; | |
import com.facebook.swift.service.ThriftServiceProcessor; | |
import org.apache.thrift.protocol.*; | |
import org.apache.thrift.server.TServlet; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | |
import org.springframework.context.annotation.*; | |
import javax.servlet.Servlet; | |
import java.util.Arrays; | |
@Configuration | |
@EnableAutoConfiguration | |
@ComponentScan | |
public class CalculatorApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(CalculatorApplication.class, args); | |
} | |
@Bean | |
TProtocolFactory tProtocolFactory() { | |
return new TBinaryProtocol.Factory(); | |
} | |
@Bean | |
ThriftCodecManager thriftCodecManager() { | |
return new ThriftCodecManager(); | |
} | |
@Bean | |
Servlet thrift(ThriftCodecManager thriftCodecManager, TProtocolFactory protocolFactory, TCalculatorService exampleService) { | |
ThriftServiceProcessor processor = new ThriftServiceProcessor(thriftCodecManager, Arrays.<ThriftEventHandler>asList(), exampleService); | |
return new TServlet( | |
NiftyProcessorAdapters.processorToTProcessor(processor), | |
protocolFactory, | |
protocolFactory | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator; | |
import com.example.calculator.protocol.TCalculatorService; | |
import com.example.calculator.protocol.TDivisionByZeroException; | |
import com.example.calculator.protocol.TOperation; | |
import com.facebook.nifty.client.HttpClientConnector; | |
import com.facebook.swift.codec.ThriftCodecManager; | |
import com.facebook.swift.service.ThriftClientManager; | |
import org.apache.thrift.protocol.TProtocolFactory; | |
import org.junit.Before; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.beans.factory.annotation.Value; | |
import org.springframework.boot.test.IntegrationTest; | |
import org.springframework.boot.test.SpringApplicationConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
import org.springframework.test.context.web.WebAppConfiguration; | |
import java.net.URI; | |
import static org.junit.Assert.*; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@SpringApplicationConfiguration(classes = CalculatorApplication.class) | |
@WebAppConfiguration | |
@IntegrationTest("server.port:0") | |
public class CalculatorApplicationTest { | |
@Autowired | |
TProtocolFactory protocolFactory; | |
@Autowired | |
ThriftCodecManager thriftCodecManager; | |
@Value("${local.server.port}") | |
protected int port; | |
protected TCalculatorService client; | |
@Before | |
public void setUp() throws Exception { | |
HttpClientConnector connector = new HttpClientConnector(URI.create("http://localhost:" + port + "/thrift/")); | |
ThriftClientManager clientManager = new ThriftClientManager(thriftCodecManager); | |
client = clientManager.createClient(connector, TCalculatorService.class).get(); | |
} | |
@Test | |
public void testAdd() throws Exception { | |
assertEquals(5, client.calculate(2, 3, TOperation.ADD)); | |
} | |
@Test | |
public void testSubtract() throws Exception { | |
assertEquals(3, client.calculate(5, 2, TOperation.SUBTRACT)); | |
} | |
@Test | |
public void testMultiply() throws Exception { | |
assertEquals(10, client.calculate(5, 2, TOperation.MULTIPLY)); | |
} | |
@Test | |
public void testDivide() throws Exception { | |
assertEquals(2, client.calculate(10, 5, TOperation.DIVIDE)); | |
} | |
@Test(expected = TDivisionByZeroException.class) | |
public void testDivisionByZero() throws Exception { | |
client.calculate(10, 0, TOperation.DIVIDE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator.handler; | |
import com.example.calculator.protocol.TCalculatorService; | |
import com.example.calculator.protocol.TDivisionByZeroException; | |
import com.example.calculator.protocol.TOperation; | |
import org.springframework.stereotype.Component; | |
import com.example.calculator.service.CalculatorService; | |
import org.springframework.beans.factory.annotation.Autowired; | |
@Component | |
public class CalculatorServiceHandler implements TCalculatorService { | |
@Autowired | |
CalculatorService calculatorService; | |
@Override | |
public int calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException { | |
switch(op) { | |
case ADD: | |
return calculatorService.add(num1, num2); | |
case SUBTRACT: | |
return calculatorService.subtract(num1, num2); | |
case MULTIPLY: | |
return calculatorService.multiply(num1, num2); | |
case DIVIDE: | |
try { | |
return calculatorService.divide(num1, num2); | |
} catch(IllegalArgumentException e) { | |
throw new TDivisionByZeroException(); | |
} | |
default: | |
throw new IllegalArgumentException("Unknown operation " + op); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator.protocol; | |
import com.facebook.swift.service.ThriftMethod; | |
import com.facebook.swift.service.ThriftService; | |
import com.google.common.util.concurrent.ListenableFuture; | |
@ThriftService | |
public interface TCalculatorService { | |
@ThriftMethod | |
int calculate(int num1, int num2, TOperation op) throws TDivisionByZeroException; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator.protocol; | |
import com.facebook.swift.codec.ThriftStruct; | |
@ThriftStruct | |
public final class TDivisionByZeroException extends Exception { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.calculator.protocol; | |
public enum TOperation { | |
ADD, | |
SUBTRACT, | |
MULTIPLY, | |
DIVIDE | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment