SlideShare a Scribd company logo
1 of 53
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
web services & unit testing
Corey Stubbs – Claudio Brandani
Hội Thảo Công Nghệ Ở DIC
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Agenda
 Giới thiệu
 Web services là gì?
 Giải lao
 Java và tooling
IDE là gì
Basic Java coding
Những mẫu code ví dụ
 Unit testing
Tại sao sử dụng Unit testing
Frameworks
Ví dụ
 Nghỉ trưa
 Hands on development- phần 1
 Giải lao
 Hands on development- phần 2
 Tóm tắt và hỏi đáp
2
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and
how to use them
Web services là gì và cách sử dụng
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Agenda -
1. What is a Web Service: Web service là gì?
2. Web Services Specifications and a brief history: Đặc điểm và tóm tắt lịch sử
3. Types of Web Services and protocols (REST, RPC, ...): Các loại Web Service và protocols (REST,
RPC,...)
4. How is a Web Service build - design, versioning: Khi một Web Service được xây dựng- mảng
thiết kế và versioning như thế nào?
5. How is a Web Service build - what are verbs, type of verbs-: verbs là gì, các loại verbs
6. How is a Web Service build - securing web services: bảo mật Web Services
7. How is a Web Service consumed - concept of provider and consumer: Một Web Serviced được tiêu
thụ như thế nào- khái niệm về nhà cung cấp và khách hàng
8. How is a Web Service consumed - Invocation, Response Codes, Response Types, Verbs, URLs,
HTTP requests: Một Web Serviced được tiêu thụ như thế nào- Invocation, Response Codes,
Response Types, Verbs, URLs, HTTP requests
9. A brief look into Scaling Web Services (Cloud): Tổng quan về Scaling Web Services (Cloud)
10. Example (e.g. Youtube): Ví dụ
4
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- What is a Web Service -
 A web service is any piece of software that makes itself available over the internet and uses a
standardized XML messaging system: là bấy cứ phần mềm sẵn có trên Internet và sử
dụng XML messaging system đã được chuẩn hóa
 Web services are self-contained, modular, distributed, dynamic applications that can be
described, published, located, or invoked over the network: là các ứng dụng được khép kín,
mô đun, phân phối, thao tác nhanh, có thể được mô tả, phát hành, xác định vị trí, hoặc
thông qua mạng
 Web services are XML-based information exchange systems that use the Internet for direct
application-to-application interaction: Web services là những hệ thống trao đổi thông tin
dựa trên XML -sử dụng Internet để tương tác ứng dụng-ứng dụng trực tiếp.
 A web service is a collection of open protocols and standards used for exchanging data
between applications or systems: Một web service là một tập hợp các giao thức và tiêu
chuẩn mở được sử dụng để trao đổi dữ liệu giữa các ứng dụng hoặc hệ thống
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- What is a Web Service - why use it –
Tại sao lại sử dụng Web Service
The main benefits of Web Services are:
• Exposing the Existing Function on the network
• Interoperability
• Standardized Protocol
• Low Cost of Communication
Các lợi ích chính của Dịch vụ Web là:
• Có sẵn chức năng trên mạng
• Khả năng tương tác
• Giao thức mạng được chuẩn hóa
• Chi phí truyền thông mạng thấp
6
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Web Services Specifications - where it all began – WSDL, UDDI, SOAP -
7
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Web Services Specifications and a brief history -
 There is a wide range of categories of specifications needed for Web Services to be
successful:
– XML
– Messaging
– Metadata Exchange
– Security
– Privacy
– Reliable Messaging
– Resource
– Web Services Interoperability (WS-I)
– Business Process
– Transaction
– Management
– Presentation Oriented
– Draft Specifications
8
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Types of Web Services and protocols (REST, RPC, JSON) -
 RPC stands for Remote Procedure Call.
– As its name indicates, it is a mechanism to call a procedure or a function available on a
remote computer. RPC is a much older technology than the Web. Effectively, RPC gives
developers a mechanism for defining interfaces that can be called over a network. These
interfaces can be as simple as a single function call or as complex as a large API.
– là một bộ phận để gọi một thủ tục hoặc một chức năng có sẵn trên một máy tính từ xa.
RPC là một công nghệ cũ hơn Web. Hiệu quả, RPC cung cấp cho các nhà phát triển một
cơ chế để xác định các giao diện có thể được gọi qua mạng. Các giao diện này có thể
đơn giản như một cuộc gọi chức năng riêng lẻ hoặc phức tạp như một API lớn.
9
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Types of Web Services and protocols (REST, RPC, JSON) -
 JavaScript Object Notation (JSON) uses name/value pairs.
– This is similar to the tags used by XML. An example from the figure below shows, at the left, the XML tag of "<state>" with the value
of "MN." The pairs for JSON are at the right. It similarly shows the name "state" is paired with the value "MN." The name/value pairs
do not need to be in a specific order. Also, like XML, JSON provides resiliance to changes and avoids the brittleness of fixed record
formats. See XML Tagged Format Compared to Fixed Record Formats. This applies to JSON as well.
– JavaScript Object Notation (JSON) sử dụng cặp tên / giá trị.
– Điều này tương tự các thẻ sử dụng bởi XML. Một ví dụ từ hình bên dưới cho thấy, ở bên trái, thẻ XML của "<state>" với giá trị
"MN". Các cặp cho JSON nằm ở bên phải. Nó tương tự như vậy cho thấy tên "nhà nước" được ghép nối với giá trị "MN." Các cặp
tên / giá trị không cần phải theo thứ tự cụ thể. Ngoài ra, cũng giống như XML, JSON cung cấp khả năng giải quyết các thay đổi và
tránh được sự tắc nghẽn của các định dạng bản ghi cố định. Xem XML Tagged Format So với định dạng lưu trữ cố định. Điều này
cũng áp dụng cho JSON.
10
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Types of Web Services and protocols (REST, RPC, JSON) -
 Representational State Transfer (REST)
– Representational State Transfer (REST) is a style of architecture based on a set of
principles that describe how networked resources are defined and addressed. These
principles were first described in 2000 by Roy Fielding as part of his doctoral dissertation.
REST is an alternative to SOAP and JavaScript Object Notation (JSON).
– Representational State Transfer (REST) là một phong cách kiến trúc dựa trên một tập hợp
các nguyên tắc mô tả như thế nào nguồn tài nguyên mạng được định nghĩa và xác định.
Những nguyên tắc này được Roy Fielding mô tả lần đầu tiên vào năm 2000 như là một
phần của luận văn tiến sĩ của ông. REST là một thay thế cho SOAP và JavaScript Object
Notation (JSON).
11
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- How is a RESTful Web Service build – verbs -
12
Uniform Resource
Locator (URL)
GET PUT POST DELETE
Collection, such as
http://api.example.com/re
sources/
List the URIs and
perhaps other
details of the
collection's
members.
Replace the entire
collection with
another collection.
Create a new entry in
the collection. The
new entry's URI is
assigned
automatically and is
usually returned by
the operation
Delete the entire
collection.
Element, such as
http://api.example.com/re
sources/item17
Retrieve a
representation of
the addressed
member of the
collection,
expressed in an
appropriate
Internet media
type.
Replace the
addressed member of
the collection, or if it
does not
exist, create it.
Not generally used.
Treat the addressed
member as a
collection in its own
right and create a
new entry within it
Delete the addressed
member of the
collection.
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- How is a RESTful Web Service build – design and usage -
13
JSON representation of a resource.
{
"ID": "1",
"Name": "Mr. Doggy Dog",
"Email": "doggy@whoff.com",
"Country": "Houndland“
}
XML representation of a resource.
<Person>
<ID>1</ID>
<Name>Mr. Doggy Dog</Name>
<Email>doggy@whoff.com</Email>
<Country>Houndland</Country>
</Person>
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- How is a RESTful Web Service build – versioning -
14
 The dilemma of versioning
– The correct handling of API versioning has been one of the most difficult issues faced by
developers of distributed systems.
 Understanding change types
– Roughly speaking, there are two types of changes in a WSDL document that cannot break
an existing requestor, and several types of changes that can.
• Khó khăn của việc versioning
Việc xử lý chính xác phiên bản API là một trong những vấn đề khó khăn nhất mà nhà
phát triển các hệ thống phân phối phải đối mặt.
• Hiểu các loại thay đổi
Nói chung, có hai loại thay đổi trong một tài liệu WSDL không thể phá vỡ một người
yêu cầu hiện tại và một số loại thay đổi có thể.
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- How is a RESTful Web Service build - securing web services -
 Checklist for securing web services:
• Transport Confidentiality
• Server Authentication
• User Authentication
• Transport Encoding
• Message Integrity
• Message Confidentiality
• Authorization
• Schema Validation
• Content Validation
• Output Encoding
• Virus Protection
• Message Size
• Availability - Message Throughput
• Availability - XML Denial of Service Protection
• Endpoint Security Profile
15
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- A brief look into Scaling Web Services (Cloud) -
 Scalability addresses an architecture's need to support a large number of instances or
concurrent interactions. Four basic approaches for dealing with scalability demands are
identified and can be combined in various ways:
– scaling up - increasing the capacity of services, consumers, and network devices
– scaling out - distributing load across services and programs
– smoothing out - evening out the number of interactions over peak and non-peak periods to
optimize the infrastructure (thereby reducing the impact of the peaks to avoid the
infrastructure sitting idle at other times)
– decoupling the consumption of finite resources - such as memory from concurrent
consumers
16
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
What are web services and how to use them
- Example (e.g. Youtube) -
17
Proxy
WebServer
Application Server
1
Streaming
Service
2
Reccomendation
Service
3
Comment
Service
Advanced
History Look Up
Service
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Lập trình Java
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Javal là gì?
Ra mắt lần đầu tiên vào năm 1995
Là một ngôn ngữ lập trình có thể chạy trên máy ảo (Máy ảo
Java)
Dễ dàng để phát triển trên nhiều nền tảng khác nhau
(Windows, Mac, Linux,…)
Hiện tại đã phát hành phiên bản thứ 8
Có cú pháp tương tự như C#
19
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
IntelliJ IDEA
20
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Variables
21
Variable Declaration
Variable Assignment
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Variable Declaration
22
Data type
Variable Name
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Variable Assignment
23
Assignment Operator
Assignment Value
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Variables
24
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Functions
25
Function Parameters (Inputs)
Return Value (Output)
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Classes - Declaration
26
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Classes – Create an Instance
27
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Logic
28
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Output
29
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Developer Tooling
Công cụ phát triển
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
IntelliJ IDEA
31
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Git
32
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Github
33
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Unit Testing
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Unit testing là gì?
35
Viết mã để kiểm tra mã sản xuất
Cho phép chúng ta xác minh logic code
Cho phép xác minh một cách tự động
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Tại sao lại là Unit test?
36
Chúng ta sẽ biết khi nào chúng ta phá vỡ các code
Chúng ta sẽ thấy các chức năng của các mã
Đưa ra các ghi chú cho các lập trình viên khác về chức năng
của các mã/code khi họ muốn thay đổi điều gì đó
Tiết kiệm thời gian và tiền bạc nhờ vào tính năng tự động
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Sử dụng Unit test như thế nào?
37
Chúng ta viết code!
Sử dụng thư viện và framworks để tạo ra các báo cáo
Tích hợp chúng vào quy trình làm việc hàng ngày của
chúng ta
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Một Unit Test sẽ trong như thế nào?
38
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Các khó khăn đối với Unit testing
39
Sẽ là một khó khăn để viết các test nhỏ
Các đường có thể mờ giữa unit test và các test khác
Mất nhiều thời gian để học, nhưng sẽ rất có ít cho kế hoạch
dài hạn
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Các khó khăn đối với Unit testing
40
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Mocking là gì?
 Sử dụng để loại bỏ các đoạn mã không thể kiểm tra
– Ví dụ: database code, web services calls
 Tạo ra một giá trị ảo cho một đối tượng
 Ví dụ:
– https://www.mkyong.com/unittest/unit-test-what-is-mocking-and-why/
41
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Unit Testing in IntelliJ Tutorial
Unit testing trong Hướng dẫn IntelliJ
42
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Writing A Unit Test Tutorial
Viết một hướng dẫn Unit test
43
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Fixing An Implementation Tutorial
Khắc phục Thực hiện Hướng dẫn
44
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Hands on exercise!
Thực hành bài tập
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
46
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Day dựng một hệ thống ngôi sao 5 cạnh
47
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
48
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
49
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
50
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
51
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs
Xây dựng một hệ thống ngôi sao 5 cạnh
52
Central
Business
Data
Business
Service
1
Business
Service
2
Business
Service
3
Business
Service
4
Business
Service
5
© 2016 IBM CorporationCorporate Citizenship and Corporate Affairs53

More Related Content

Similar to Bản cuối

Kiến trúc hướng dịch vụ (webservice)
Kiến trúc hướng dịch vụ (webservice)Kiến trúc hướng dịch vụ (webservice)
Kiến trúc hướng dịch vụ (webservice)Nguyễn Thu Hằng
 
Web Architecture
Web ArchitectureWeb Architecture
Web ArchitectureHiep Luong
 
Báo cáo thực tập chuyên nghành lập trình Android GPSGroup
Báo cáo thực tập chuyên nghành lập trình Android GPSGroupBáo cáo thực tập chuyên nghành lập trình Android GPSGroup
Báo cáo thực tập chuyên nghành lập trình Android GPSGroupTinh Ngo
 
Chapter 1 introduction www
Chapter 1  introduction wwwChapter 1  introduction www
Chapter 1 introduction wwwxuandiencntt
 
Software Stack là gì.pdf
Software Stack là gì.pdfSoftware Stack là gì.pdf
Software Stack là gì.pdfGrowup Work
 
Semantic web
Semantic webSemantic web
Semantic webDuyen Do
 
Bao cao thuc tap - Điện toán đám mây
Bao cao thuc tap - Điện toán đám mâyBao cao thuc tap - Điện toán đám mây
Bao cao thuc tap - Điện toán đám mâyVan Pham
 
Bao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mâyBao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mâyVăn Ân Phạm
 
SEOWAP - Tài liệu SEO cho Mobile - Tuấn Hà
SEOWAP - Tài liệu SEO cho Mobile - Tuấn HàSEOWAP - Tài liệu SEO cho Mobile - Tuấn Hà
SEOWAP - Tài liệu SEO cho Mobile - Tuấn HàNguyễn Duy Nhân
 
Seo cho Mobile , Mobile web Seo
Seo cho Mobile , Mobile web SeoSeo cho Mobile , Mobile web Seo
Seo cho Mobile , Mobile web SeoVinalink Media JSC
 
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPT
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPTBÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPT
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPTMasterCode.vn
 
Tìm hiểu về cloud computing
Tìm hiểu về cloud computingTìm hiểu về cloud computing
Tìm hiểu về cloud computinglanhuonga3
 
Slide Web Service
Slide Web ServiceSlide Web Service
Slide Web Serviceask bills
 
Ex 1 chapter03-appliation-layer-tony_chen - tieng viet
Ex 1 chapter03-appliation-layer-tony_chen - tieng vietEx 1 chapter03-appliation-layer-tony_chen - tieng viet
Ex 1 chapter03-appliation-layer-tony_chen - tieng vietĐô GiẢn
 
1thietkemanglan wan
1thietkemanglan wan1thietkemanglan wan
1thietkemanglan wanlinhdv87hy
 
Bao cao web cake php
Bao cao web cake phpBao cao web cake php
Bao cao web cake phplaonap166
 

Similar to Bản cuối (20)

Kiến trúc hướng dịch vụ (webservice)
Kiến trúc hướng dịch vụ (webservice)Kiến trúc hướng dịch vụ (webservice)
Kiến trúc hướng dịch vụ (webservice)
 
Web Architecture
Web ArchitectureWeb Architecture
Web Architecture
 
Webservice
WebserviceWebservice
Webservice
 
Báo cáo thực tập chuyên nghành lập trình Android GPSGroup
Báo cáo thực tập chuyên nghành lập trình Android GPSGroupBáo cáo thực tập chuyên nghành lập trình Android GPSGroup
Báo cáo thực tập chuyên nghành lập trình Android GPSGroup
 
Chapter 1 introduction www
Chapter 1  introduction wwwChapter 1  introduction www
Chapter 1 introduction www
 
Software Stack là gì.pdf
Software Stack là gì.pdfSoftware Stack là gì.pdf
Software Stack là gì.pdf
 
Luận văn: Tích hợp dịch vụ nghiệp vụ ngân hàng theo mô hình soa
Luận văn: Tích hợp dịch vụ nghiệp vụ ngân hàng theo mô hình soaLuận văn: Tích hợp dịch vụ nghiệp vụ ngân hàng theo mô hình soa
Luận văn: Tích hợp dịch vụ nghiệp vụ ngân hàng theo mô hình soa
 
Semantic web
Semantic webSemantic web
Semantic web
 
Bao cao thuc tap - Điện toán đám mây
Bao cao thuc tap - Điện toán đám mâyBao cao thuc tap - Điện toán đám mây
Bao cao thuc tap - Điện toán đám mây
 
Bao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mâyBao cao thực tập Điện toán đám mây
Bao cao thực tập Điện toán đám mây
 
SEOWAP - Tài liệu SEO cho Mobile - Tuấn Hà
SEOWAP - Tài liệu SEO cho Mobile - Tuấn HàSEOWAP - Tài liệu SEO cho Mobile - Tuấn Hà
SEOWAP - Tài liệu SEO cho Mobile - Tuấn Hà
 
Seo cho Mobile , Mobile web Seo
Seo cho Mobile , Mobile web SeoSeo cho Mobile , Mobile web Seo
Seo cho Mobile , Mobile web Seo
 
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPT
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPTBÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPT
BÀI 7 Triển khai, hướng phát triển cho web mobile - Giáo trình FPT
 
Tìm hiểu về cloud computing
Tìm hiểu về cloud computingTìm hiểu về cloud computing
Tìm hiểu về cloud computing
 
Slide Web Service
Slide Web ServiceSlide Web Service
Slide Web Service
 
Ex 1 chapter03-appliation-layer-tony_chen - tieng viet
Ex 1 chapter03-appliation-layer-tony_chen - tieng vietEx 1 chapter03-appliation-layer-tony_chen - tieng viet
Ex 1 chapter03-appliation-layer-tony_chen - tieng viet
 
Megawan
MegawanMegawan
Megawan
 
Sof301 slide1
Sof301   slide1Sof301   slide1
Sof301 slide1
 
1thietkemanglan wan
1thietkemanglan wan1thietkemanglan wan
1thietkemanglan wan
 
Bao cao web cake php
Bao cao web cake phpBao cao web cake php
Bao cao web cake php
 

Bản cuối

  • 1. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs web services & unit testing Corey Stubbs – Claudio Brandani Hội Thảo Công Nghệ Ở DIC
  • 2. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Agenda  Giới thiệu  Web services là gì?  Giải lao  Java và tooling IDE là gì Basic Java coding Những mẫu code ví dụ  Unit testing Tại sao sử dụng Unit testing Frameworks Ví dụ  Nghỉ trưa  Hands on development- phần 1  Giải lao  Hands on development- phần 2  Tóm tắt và hỏi đáp 2
  • 3. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them Web services là gì và cách sử dụng
  • 4. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Agenda - 1. What is a Web Service: Web service là gì? 2. Web Services Specifications and a brief history: Đặc điểm và tóm tắt lịch sử 3. Types of Web Services and protocols (REST, RPC, ...): Các loại Web Service và protocols (REST, RPC,...) 4. How is a Web Service build - design, versioning: Khi một Web Service được xây dựng- mảng thiết kế và versioning như thế nào? 5. How is a Web Service build - what are verbs, type of verbs-: verbs là gì, các loại verbs 6. How is a Web Service build - securing web services: bảo mật Web Services 7. How is a Web Service consumed - concept of provider and consumer: Một Web Serviced được tiêu thụ như thế nào- khái niệm về nhà cung cấp và khách hàng 8. How is a Web Service consumed - Invocation, Response Codes, Response Types, Verbs, URLs, HTTP requests: Một Web Serviced được tiêu thụ như thế nào- Invocation, Response Codes, Response Types, Verbs, URLs, HTTP requests 9. A brief look into Scaling Web Services (Cloud): Tổng quan về Scaling Web Services (Cloud) 10. Example (e.g. Youtube): Ví dụ 4
  • 5. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - What is a Web Service -  A web service is any piece of software that makes itself available over the internet and uses a standardized XML messaging system: là bấy cứ phần mềm sẵn có trên Internet và sử dụng XML messaging system đã được chuẩn hóa  Web services are self-contained, modular, distributed, dynamic applications that can be described, published, located, or invoked over the network: là các ứng dụng được khép kín, mô đun, phân phối, thao tác nhanh, có thể được mô tả, phát hành, xác định vị trí, hoặc thông qua mạng  Web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction: Web services là những hệ thống trao đổi thông tin dựa trên XML -sử dụng Internet để tương tác ứng dụng-ứng dụng trực tiếp.  A web service is a collection of open protocols and standards used for exchanging data between applications or systems: Một web service là một tập hợp các giao thức và tiêu chuẩn mở được sử dụng để trao đổi dữ liệu giữa các ứng dụng hoặc hệ thống 5
  • 6. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - What is a Web Service - why use it – Tại sao lại sử dụng Web Service The main benefits of Web Services are: • Exposing the Existing Function on the network • Interoperability • Standardized Protocol • Low Cost of Communication Các lợi ích chính của Dịch vụ Web là: • Có sẵn chức năng trên mạng • Khả năng tương tác • Giao thức mạng được chuẩn hóa • Chi phí truyền thông mạng thấp 6
  • 7. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Web Services Specifications - where it all began – WSDL, UDDI, SOAP - 7
  • 8. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Web Services Specifications and a brief history -  There is a wide range of categories of specifications needed for Web Services to be successful: – XML – Messaging – Metadata Exchange – Security – Privacy – Reliable Messaging – Resource – Web Services Interoperability (WS-I) – Business Process – Transaction – Management – Presentation Oriented – Draft Specifications 8
  • 9. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Types of Web Services and protocols (REST, RPC, JSON) -  RPC stands for Remote Procedure Call. – As its name indicates, it is a mechanism to call a procedure or a function available on a remote computer. RPC is a much older technology than the Web. Effectively, RPC gives developers a mechanism for defining interfaces that can be called over a network. These interfaces can be as simple as a single function call or as complex as a large API. – là một bộ phận để gọi một thủ tục hoặc một chức năng có sẵn trên một máy tính từ xa. RPC là một công nghệ cũ hơn Web. Hiệu quả, RPC cung cấp cho các nhà phát triển một cơ chế để xác định các giao diện có thể được gọi qua mạng. Các giao diện này có thể đơn giản như một cuộc gọi chức năng riêng lẻ hoặc phức tạp như một API lớn. 9
  • 10. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Types of Web Services and protocols (REST, RPC, JSON) -  JavaScript Object Notation (JSON) uses name/value pairs. – This is similar to the tags used by XML. An example from the figure below shows, at the left, the XML tag of "<state>" with the value of "MN." The pairs for JSON are at the right. It similarly shows the name "state" is paired with the value "MN." The name/value pairs do not need to be in a specific order. Also, like XML, JSON provides resiliance to changes and avoids the brittleness of fixed record formats. See XML Tagged Format Compared to Fixed Record Formats. This applies to JSON as well. – JavaScript Object Notation (JSON) sử dụng cặp tên / giá trị. – Điều này tương tự các thẻ sử dụng bởi XML. Một ví dụ từ hình bên dưới cho thấy, ở bên trái, thẻ XML của "<state>" với giá trị "MN". Các cặp cho JSON nằm ở bên phải. Nó tương tự như vậy cho thấy tên "nhà nước" được ghép nối với giá trị "MN." Các cặp tên / giá trị không cần phải theo thứ tự cụ thể. Ngoài ra, cũng giống như XML, JSON cung cấp khả năng giải quyết các thay đổi và tránh được sự tắc nghẽn của các định dạng bản ghi cố định. Xem XML Tagged Format So với định dạng lưu trữ cố định. Điều này cũng áp dụng cho JSON. 10
  • 11. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Types of Web Services and protocols (REST, RPC, JSON) -  Representational State Transfer (REST) – Representational State Transfer (REST) is a style of architecture based on a set of principles that describe how networked resources are defined and addressed. These principles were first described in 2000 by Roy Fielding as part of his doctoral dissertation. REST is an alternative to SOAP and JavaScript Object Notation (JSON). – Representational State Transfer (REST) là một phong cách kiến trúc dựa trên một tập hợp các nguyên tắc mô tả như thế nào nguồn tài nguyên mạng được định nghĩa và xác định. Những nguyên tắc này được Roy Fielding mô tả lần đầu tiên vào năm 2000 như là một phần của luận văn tiến sĩ của ông. REST là một thay thế cho SOAP và JavaScript Object Notation (JSON). 11
  • 12. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - How is a RESTful Web Service build – verbs - 12 Uniform Resource Locator (URL) GET PUT POST DELETE Collection, such as http://api.example.com/re sources/ List the URIs and perhaps other details of the collection's members. Replace the entire collection with another collection. Create a new entry in the collection. The new entry's URI is assigned automatically and is usually returned by the operation Delete the entire collection. Element, such as http://api.example.com/re sources/item17 Retrieve a representation of the addressed member of the collection, expressed in an appropriate Internet media type. Replace the addressed member of the collection, or if it does not exist, create it. Not generally used. Treat the addressed member as a collection in its own right and create a new entry within it Delete the addressed member of the collection.
  • 13. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - How is a RESTful Web Service build – design and usage - 13 JSON representation of a resource. { "ID": "1", "Name": "Mr. Doggy Dog", "Email": "doggy@whoff.com", "Country": "Houndland“ } XML representation of a resource. <Person> <ID>1</ID> <Name>Mr. Doggy Dog</Name> <Email>doggy@whoff.com</Email> <Country>Houndland</Country> </Person>
  • 14. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - How is a RESTful Web Service build – versioning - 14  The dilemma of versioning – The correct handling of API versioning has been one of the most difficult issues faced by developers of distributed systems.  Understanding change types – Roughly speaking, there are two types of changes in a WSDL document that cannot break an existing requestor, and several types of changes that can. • Khó khăn của việc versioning Việc xử lý chính xác phiên bản API là một trong những vấn đề khó khăn nhất mà nhà phát triển các hệ thống phân phối phải đối mặt. • Hiểu các loại thay đổi Nói chung, có hai loại thay đổi trong một tài liệu WSDL không thể phá vỡ một người yêu cầu hiện tại và một số loại thay đổi có thể.
  • 15. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - How is a RESTful Web Service build - securing web services -  Checklist for securing web services: • Transport Confidentiality • Server Authentication • User Authentication • Transport Encoding • Message Integrity • Message Confidentiality • Authorization • Schema Validation • Content Validation • Output Encoding • Virus Protection • Message Size • Availability - Message Throughput • Availability - XML Denial of Service Protection • Endpoint Security Profile 15
  • 16. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - A brief look into Scaling Web Services (Cloud) -  Scalability addresses an architecture's need to support a large number of instances or concurrent interactions. Four basic approaches for dealing with scalability demands are identified and can be combined in various ways: – scaling up - increasing the capacity of services, consumers, and network devices – scaling out - distributing load across services and programs – smoothing out - evening out the number of interactions over peak and non-peak periods to optimize the infrastructure (thereby reducing the impact of the peaks to avoid the infrastructure sitting idle at other times) – decoupling the consumption of finite resources - such as memory from concurrent consumers 16
  • 17. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs What are web services and how to use them - Example (e.g. Youtube) - 17 Proxy WebServer Application Server 1 Streaming Service 2 Reccomendation Service 3 Comment Service Advanced History Look Up Service
  • 18. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Lập trình Java
  • 19. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Javal là gì? Ra mắt lần đầu tiên vào năm 1995 Là một ngôn ngữ lập trình có thể chạy trên máy ảo (Máy ảo Java) Dễ dàng để phát triển trên nhiều nền tảng khác nhau (Windows, Mac, Linux,…) Hiện tại đã phát hành phiên bản thứ 8 Có cú pháp tương tự như C# 19
  • 20. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs IntelliJ IDEA 20
  • 21. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Variables 21 Variable Declaration Variable Assignment
  • 22. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Variable Declaration 22 Data type Variable Name
  • 23. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Variable Assignment 23 Assignment Operator Assignment Value
  • 24. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Variables 24
  • 25. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Functions 25 Function Parameters (Inputs) Return Value (Output)
  • 26. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Classes - Declaration 26
  • 27. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Classes – Create an Instance 27
  • 28. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Logic 28
  • 29. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Output 29
  • 30. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Developer Tooling Công cụ phát triển
  • 31. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs IntelliJ IDEA 31
  • 32. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Git 32
  • 33. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Github 33
  • 34. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Unit Testing
  • 35. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Unit testing là gì? 35 Viết mã để kiểm tra mã sản xuất Cho phép chúng ta xác minh logic code Cho phép xác minh một cách tự động
  • 36. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Tại sao lại là Unit test? 36 Chúng ta sẽ biết khi nào chúng ta phá vỡ các code Chúng ta sẽ thấy các chức năng của các mã Đưa ra các ghi chú cho các lập trình viên khác về chức năng của các mã/code khi họ muốn thay đổi điều gì đó Tiết kiệm thời gian và tiền bạc nhờ vào tính năng tự động
  • 37. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Sử dụng Unit test như thế nào? 37 Chúng ta viết code! Sử dụng thư viện và framworks để tạo ra các báo cáo Tích hợp chúng vào quy trình làm việc hàng ngày của chúng ta
  • 38. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Một Unit Test sẽ trong như thế nào? 38
  • 39. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Các khó khăn đối với Unit testing 39 Sẽ là một khó khăn để viết các test nhỏ Các đường có thể mờ giữa unit test và các test khác Mất nhiều thời gian để học, nhưng sẽ rất có ít cho kế hoạch dài hạn
  • 40. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Các khó khăn đối với Unit testing 40
  • 41. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Mocking là gì?  Sử dụng để loại bỏ các đoạn mã không thể kiểm tra – Ví dụ: database code, web services calls  Tạo ra một giá trị ảo cho một đối tượng  Ví dụ: – https://www.mkyong.com/unittest/unit-test-what-is-mocking-and-why/ 41
  • 42. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Unit Testing in IntelliJ Tutorial Unit testing trong Hướng dẫn IntelliJ 42
  • 43. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Writing A Unit Test Tutorial Viết một hướng dẫn Unit test 43
  • 44. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Fixing An Implementation Tutorial Khắc phục Thực hiện Hướng dẫn 44
  • 45. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Hands on exercise! Thực hành bài tập
  • 46. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 46 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 47. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Day dựng một hệ thống ngôi sao 5 cạnh 47 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 48. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 48 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 49. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 49 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 50. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 50 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 51. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 51 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 52. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs Xây dựng một hệ thống ngôi sao 5 cạnh 52 Central Business Data Business Service 1 Business Service 2 Business Service 3 Business Service 4 Business Service 5
  • 53. © 2016 IBM CorporationCorporate Citizenship and Corporate Affairs53