Aggiunto l'esempio del capitolo su SOAP Web Service (JAX-WS)

This commit is contained in:
Fabio Scotto di Santolo
2019-04-02 21:58:30 +02:00
parent 9e07e624e0
commit e3bbb08317
11 changed files with 215 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package it.plague.jeedemo;
import javax.jws.WebService;
@WebService(endpointInterface = "it.plague.jeedemo.Validator")
public class CardValidator implements Validator {
@Override
public boolean validate(CreditCard creditCard) {
Character lastDigit = creditCard.getNumber().charAt(creditCard.getNumber().length() - 1);
return Integer.parseInt(lastDigit.toString()) % 2 == 0;
}
}

View File

@@ -0,0 +1,30 @@
package it.plague.jeedemo;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class CreditCard {
@XmlAttribute(required = true)
private String number;
@XmlAttribute(name = "expiry_date", required = true)
private String expiryDate;
@XmlAttribute(name = "control_number", required = true)
private Integer controlNumber;
@XmlAttribute(required = true)
private String type;
}

View File

@@ -0,0 +1,9 @@
package it.plague.jeedemo;
import javax.jws.WebService;
@WebService
public interface Validator {
boolean validate(CreditCard creditCard);
}

View File

@@ -0,0 +1,62 @@
package it.plague.jeedemo;
import static org.junit.Assert.*;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Endpoint;
import javax.xml.ws.Service;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class CardValidatorIT {
private static Endpoint endpoint;
private static Validator cardValidator;
@BeforeClass
public static void setUp() throws MalformedURLException {
final String url = "http://localhost:8081/cardValidator";
// Publishes the SOAP Web Service
endpoint = Endpoint.publish(url, new CardValidator());
// Needed properties to access the web service
URL wsdlDocumentLocation = new URL(url + "?wsdl");
String namespaceURI = "http://jeedemo.plague.it/";
String servicePart = "CardValidatorService";
String portName = "CardValidatorPort";
QName serviceQn = new QName(namespaceURI, servicePart);
QName portQn = new QName(namespaceURI, portName);
// Creates a service instance
Service service = Service.create(wsdlDocumentLocation, serviceQn);
cardValidator = service.getPort(portQn, Validator.class);
}
@AfterClass
public static void tearDown() {
if (endpoint != null) {
endpoint.stop();
}
}
@Test
public void shouldBePublished() {
assertTrue(endpoint.isPublished());
assertEquals("http://schemas.xmlsoap.org/wsdl/soap/http", endpoint.getBinding().getBindingID());
}
@Test
public void shouldCheckCreditCardValidity() {
CreditCard creditCard = new CreditCard("12341234", "10/10", 1234, "VISA");
assertTrue("Credit card should be valid", cardValidator.validate(creditCard));
creditCard.setNumber("12341233");
assertFalse("Credit card should not be valid", cardValidator.validate(creditCard));
}
}

View File

@@ -0,0 +1,21 @@
package it.plague.jeedemo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class CardValidatorTest {
@Test
public void shouldCheckCreditCardValidity() {
CardValidator cardValidator = new CardValidator();
CreditCard creditCard = new CreditCard("12341234", "10/10", 1234, "VISA");
assertTrue("Credit card should be valid", cardValidator.validate(creditCard));
creditCard.setNumber("12341233");
assertFalse("Credit card should not be valid", cardValidator.validate(creditCard));
}
}