From d0ed6f391ac2dca90cb3047b4cbab7412a1e2ca4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aitor=20Mag=C3=A1n?= Date: Fri, 8 Jan 2016 16:34:40 +0100 Subject: [PATCH] Test to check that URIs are escaped --- .../helpers/OfferingResolverTest.java | 72 ++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/src/test/java/org/fiware/apps/marketplace/helpers/OfferingResolverTest.java b/src/test/java/org/fiware/apps/marketplace/helpers/OfferingResolverTest.java index 40509c2..de77db5 100644 --- a/src/test/java/org/fiware/apps/marketplace/helpers/OfferingResolverTest.java +++ b/src/test/java/org/fiware/apps/marketplace/helpers/OfferingResolverTest.java @@ -63,6 +63,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.web.util.HtmlUtils; import com.hp.hpl.jena.shared.JenaException; @@ -77,6 +78,7 @@ public class OfferingResolverTest { @Before public void setUp() { + MockitoAnnotations.initMocks(this); this.offeringResolver = spy(offeringResolver); @@ -102,11 +104,13 @@ private void checkOfferingBasic(Offering offering, String offeringName, String t } private void checkPricePlan(PricePlan pricePlan, String title, String description) { + assertThat(pricePlan.getTitle()).isEqualTo(title); assertThat(pricePlan.getComment()).isEqualTo(description); } private void checkService(Service service, String title, String description) { + assertThat(service.getDisplayName()).isEqualTo(title); assertThat(service.getComment()).isEqualTo(description); } @@ -128,6 +132,7 @@ private void checkPricePlanInSet(Set pricePlans, String title, String } private void checkServiceInSet(Set services, String displayName, String description) { + boolean found = false; Iterator iterator = services.iterator(); @@ -142,8 +147,9 @@ private void checkServiceInSet(Set services, String displayName, String assertThat(found).isTrue(); } - private void checkPriceComponent(PriceComponent priceComponent, String currency, String title, String unit, - float value) { + private void checkPriceComponent(PriceComponent priceComponent, String currency, + String title, String unit, float value) { + assertThat(priceComponent.getCurrency()).isEqualTo(currency); assertThat(priceComponent.getTitle()).isEqualTo(title); assertThat(priceComponent.getUnit()).isEqualTo(unit); @@ -252,6 +258,7 @@ private void initPricePlan(String offeringUri, String title, String description, } private void initPricePlan(String offeringUri, String title, String description) { + initPricePlan(offeringUri, title, description, new ArrayList()); } @@ -267,17 +274,22 @@ private void initService(String offeringUri, String serviceUri, String title, St servicesUris.add(contexServiceUri); try { + if (existingService) { + Service service = new Service(); service.setDisplayName(title); service.setComment(description); service.setUri(serviceUri); Set classifications = new HashSet<>(); service.setCategories(classifications); + when(serviceBoMock.findByURI(serviceUri)).thenReturn(service); + } else { doThrow(new ServiceNotFoundException("service not found")).when(serviceBoMock).findByURI(serviceUri); } + } catch (ServiceNotFoundException e) { fail("Exception not expected", e); // Not expected... @@ -439,6 +451,7 @@ public void testDescriptionWithTwoOfferings() { public void testOfferingWithTwoPricePlans() { try { + String offeringTitle = "cool offering"; String offeringUri = "https://store.lab.fiware.org/offerings/offering1"; String offeringDesc = "a very long long description for the best offering"; @@ -473,7 +486,9 @@ public void testOfferingWithTwoPricePlans() { } private void testOfferingWithTwoServices(boolean existingService) { + try { + String offeringTitle = "cool offering"; String offeringUri = "https://store.lab.fiware.org/offerings/offering1"; String offeringDesc = "a very long long description for the best offering"; @@ -523,6 +538,7 @@ private void testOfferingWithTwoClassifications(List classifications, boolean existingClassification) { try { + String offeringTitle = "cool offering"; String offeringUri = "https://store.lab.fiware.org/offerings/offering1"; String offeringDesc = "a very long long description for the best offering"; @@ -566,6 +582,7 @@ private void testOfferingWithTwoClassifications(List classifications, @Test public void testOfferingTwoClassificationsDiffClassificationsNonExisting() { + // displayName == name String classification1 = "class1"; String classification2 = "class2"; @@ -579,6 +596,7 @@ public void testOfferingTwoClassificationsDiffClassificationsNonExisting() { @Test public void testOfferingTwoClassificationsDiffClassificationsExisting() { + // displayName == name String classification1 = "class1"; String classification2 = "class2"; @@ -592,6 +610,7 @@ public void testOfferingTwoClassificationsDiffClassificationsExisting() { @Test public void testOfferingTwoClassificationsSameClassificationsNonExisting() { + // displayName == name String classification = "class1"; @@ -604,6 +623,7 @@ public void testOfferingTwoClassificationsSameClassificationsNonExisting() { @Test public void testOfferingTwoClassificationsSameClassificationsExisting() { + // displayName == name String classification = "class1"; @@ -615,6 +635,7 @@ public void testOfferingTwoClassificationsSameClassificationsExisting() { } private void testException(Exception exception, String expectedMessage) { + // Configure mock try { doThrow(exception).when(offeringResolver).getRdfHelper(any(Description.class)); @@ -634,16 +655,19 @@ private void testException(Exception exception, String expectedMessage) { @Test public void testUSDLNotFound() { + testException(new FileNotFoundException(), "The file does not exist"); } @Test public void testHostUnreachable() { + testException(new ConnectException(), "The host cannot be reached"); } @Test public void testInvalidRDF() { + testException(new JenaException(), "The file does not contains a valid USDL file"); } @@ -667,22 +691,28 @@ private void testOfferingInvalid(String url, String title, String version, Strin @Test public void testOfferingNameNotFound() { + String url = "http://fiware.org"; + testOfferingInvalid(url, "", "", "", "", "Name for entity " + url + " cannot be retrieved"); } @Test public void testOfferingVersionNotFound() { + String url = "http://fiware.org"; String offeringName = "Offering Name"; + testOfferingInvalid(url, offeringName, "", "", "", "Version for offering " + offeringName + " cannot be retrieved"); } @Test public void testOfferingImageURLNotFound() { + String url = "http://fiware.org"; String offeringName = "Offering Name"; + testOfferingInvalid(url, offeringName, "1.0", "", "", "Image URL for offering " + offeringName + " cannot be retrieved"); } @@ -718,12 +748,14 @@ private void testOfferingPricePlanInvalid(Map> pricePlan, S @Test public void testOfferingPricePlanNameNotIncluded() { + testOfferingPricePlanInvalid(new HashMap>(), "Offering %s contains a price plan " + "without title"); } @Test public void testOfferingPricePlanNameEmpty() { + // Configure price plan Map> pricePlan = new HashMap>(); List titles = new ArrayList<>(); @@ -735,6 +767,7 @@ public void testOfferingPricePlanNameEmpty() { @Test public void testOfferingPricePlanNameBlank() { + // Configure price plan Map> pricePlan = new HashMap>(); List titles = new ArrayList<>(); @@ -746,6 +779,7 @@ public void testOfferingPricePlanNameBlank() { } private void testOfferingPriceComponentInvalid(Map> priceComponent, String message) { + // Configure price plan Map> pricePlan = new HashMap>(); @@ -763,12 +797,14 @@ private void testOfferingPriceComponentInvalid(Map> priceCo @Test public void testOfferingPriceComponentLabelMissing() { + testOfferingPriceComponentInvalid(new HashMap>(), "Offering %s contains a price " + "component without title"); } @Test public void testOfferingPriceComponentLabelEmpty() { + // Configure price component Map> priceComponent = new HashMap<>(); List labels = new ArrayList<>(); @@ -780,6 +816,7 @@ public void testOfferingPriceComponentLabelEmpty() { @Test public void testOfferingPriceComponentLabelBkank() { + Map> priceComponent = new HashMap<>(); List labels = new ArrayList<>(); labels.add(""); @@ -791,6 +828,7 @@ public void testOfferingPriceComponentLabelBkank() { @Test public void testOfferingPriceComponentSpecificationMissing() { + Map> priceComponent = new HashMap<>(); List labels = new ArrayList<>(); labels.add("a"); @@ -803,6 +841,7 @@ public void testOfferingPriceComponentSpecificationMissing() { @Test public void testOfferingPriceComponentSpecificationEmpty() { + Map> priceComponent = new HashMap<>(); List labels = new ArrayList<>(); labels.add("a"); @@ -837,18 +876,21 @@ private void testOfferingPriceComponentInvalidSpecification(List currenc @Test public void testOfferingPriceComponentCurrencyMissing() { + testOfferingPriceComponentInvalidSpecification(null, null, null, "Offering %s contains a price component without currency"); } @Test public void testOfferingPriceComponentEmptyCurrency() { + testOfferingPriceComponentInvalidSpecification(new ArrayList<>(), null, null, "Offering %s contains a price component without currency"); } @Test public void testOfferingPriceComponentBlankCurrency() { + List currencies = new ArrayList<>(); currencies.add(""); @@ -858,6 +900,7 @@ public void testOfferingPriceComponentBlankCurrency() { @Test public void testOfferingPriceComponentMissingUnit() { + List currencies = new ArrayList<>(); currencies.add("EUR"); @@ -867,6 +910,7 @@ public void testOfferingPriceComponentMissingUnit() { @Test public void testOfferingPriceComponentEmptyUnit() { + List currencies = new ArrayList<>(); currencies.add("EUR"); @@ -876,6 +920,7 @@ public void testOfferingPriceComponentEmptyUnit() { @Test public void testOfferingPriceComponentBlankUnit() { + List currencies = new ArrayList<>(); currencies.add("EUR"); List units = new ArrayList<>(); @@ -887,6 +932,7 @@ public void testOfferingPriceComponentBlankUnit() { @Test public void testOfferingPriceComponentValueMissing() { + List currencies = new ArrayList<>(); currencies.add("EUR"); List units = new ArrayList<>(); @@ -898,6 +944,7 @@ public void testOfferingPriceComponentValueMissing() { @Test public void testOfferingPriceComponentValueEmpty() { + List currencies = new ArrayList<>(); currencies.add("EUR"); List units = new ArrayList<>(); @@ -909,6 +956,7 @@ public void testOfferingPriceComponentValueEmpty() { @Test public void testOfferingPriceComponentValueBlank() { + List currencies = new ArrayList<>(); currencies.add("EUR"); List units = new ArrayList<>(); @@ -922,6 +970,7 @@ public void testOfferingPriceComponentValueBlank() { @Test public void testOfferingPriceComponentInvalidValue() { + List currencies = new ArrayList<>(); currencies.add("EUR"); List units = new ArrayList<>(); @@ -948,4 +997,23 @@ public void testEmptyDescription() { assertThat(ex.getMessage()).isEqualTo("Offerings URLs cannot be retrieved"); } } + + @Test + public void testUrisAreEscaped() throws ParseException { + + String offeringUri = ""; + + initOffering(offeringUri, "offering", "desc", "1.0", + "https://store.lab.fiware.org/static/img1.png", + "http://store.lab.fiware.org/offering/user/offering/1.0"); + + Description description = mock(Description.class); + when(description.getUrl()).thenReturn(DESCRIPTION_URI); + + List offerings = offeringResolver.resolveOfferingsFromServiceDescription(description); + + assertThat(offerings).hasSize(1); + assertThat(offerings.get(0).getUri()).isEqualTo(HtmlUtils.htmlEscape(offeringUri)); + + } }