Skip to content

Commit

Permalink
Merge pull request OpenLiberty#11 from a-saf/encodingAndHeader
Browse files Browse the repository at this point in the history
For issue OpenLiberty#1660: Encoding and header validators and unit tests
  • Loading branch information
a-saf authored Feb 5, 2018
2 parents b3fb456 + 876976a commit 94011c1
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2017 IBM Corporation and others.
# Copyright (c) 2018 IBM Corporation and others.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
Expand Down Expand Up @@ -32,6 +32,11 @@ infoTermsOfServiceInvalidURL=The Info object must contain a valid URL. The \"{0}
invalidUrl=The \"{0}\" object must contain a valid URL. The \"{1}\" value specified for the URL is not valid.
invalidUri=The \"{0}\" object must contain a valid URI. The \"{1}\" value specified for the URI is not valid.

headerExampleOrExamples=The Header object \"{0}\" specifies both an example object and an examples object. Only one of them should be specified.
headerSchemaAndContent=The Header object \"{0}\" MUST contain either a schema property, or a content property, but not both.
headerSchemaOrContent=The Header object \"{0}\" MUST contain either a schema property or a content property.
headerContentMap=The \"content\" map within header object \"{0}\" must only contain one entry.

licenseInvalidURL=The License object must contain a valid URL. The \"{0}\" value specified for the URL is not valid.

mediaTypeExampleOrExamples=The MediaType object cannot have both \"examples\" and \"example\".
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* Copyright (c) 2018 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,9 +11,12 @@
package com.ibm.ws.microprofile.openapi.impl.validation;

import org.eclipse.microprofile.openapi.models.headers.Header;
import org.eclipse.microprofile.openapi.models.media.Content;
import org.eclipse.microprofile.openapi.models.media.Schema;

import com.ibm.websphere.ras.Tr;
import com.ibm.websphere.ras.TraceComponent;
import com.ibm.ws.microprofile.openapi.impl.validation.OASValidationResult.ValidationEvent;
import com.ibm.ws.microprofile.openapi.utils.OpenAPIModelWalker.Context;

/**
Expand All @@ -36,14 +39,40 @@ private HeaderValidator() {}
public void validate(ValidationHelper helper, Context context, String key, Header t) {

if (t != null) {

String reference = t.getRef();

if (reference != null && !reference.isEmpty()) {
ValidatorUtils.referenceValidatorHelper(reference, t, helper, context, key);
return;
}

// validate
// The examples object is mutually exclusive of the example object.
if ((t.getExample() != null) && (t.getExamples() != null && !t.getExamples().isEmpty())) {
final String message = Tr.formatMessage(tc, "headerExampleOrExamples", t);
helper.addValidationEvent(new ValidationEvent(ValidationEvent.Severity.WARNING, context.getLocation(), message));
}

Schema schema = t.getSchema();
Content content = t.getContent();
// A parameter MUST contain either a schema property, or a content property, but not both.
if (schema == null && content == null) {
final String message = Tr.formatMessage(tc, "headerSchemaOrContent", t);
helper.addValidationEvent(new ValidationEvent(ValidationEvent.Severity.ERROR, context.getLocation(), message));
}

if (schema != null && content != null) {
final String message = Tr.formatMessage(tc, "headerSchemaAndContent", t);
helper.addValidationEvent(new ValidationEvent(ValidationEvent.Severity.ERROR, context.getLocation(), message));

}

//The 'content' map MUST only contain one entry.
if (content != null && content.size() > 1) {
final String message = Tr.formatMessage(tc, "headerContentMap", t);
helper.addValidationEvent(new ValidationEvent(ValidationEvent.Severity.ERROR, context.getLocation(), message));

}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.microprofile.openapi.models.info.License;
import org.eclipse.microprofile.openapi.models.links.Link;
import org.eclipse.microprofile.openapi.models.media.Discriminator;
import org.eclipse.microprofile.openapi.models.media.Encoding;
import org.eclipse.microprofile.openapi.models.media.MediaType;
import org.eclipse.microprofile.openapi.models.media.Schema;
import org.eclipse.microprofile.openapi.models.media.XML;
Expand Down Expand Up @@ -237,12 +236,6 @@ public void visitMediaType(Context context, String key, MediaType mediaType) {
v.validate(this, context, key, mediaType);
}

@Override
public void visitEncoding(Context context, String key, Encoding encoding) {
final EncodingValidator v = EncodingValidator.getInstance();
v.validate(this, context, key, encoding);
}

@Override
public void visitOAuthFlows(Context context, OAuthFlows authFlows) {
final OAuthFlowsValidator v = OAuthFlowsValidator.getInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* IBM Confidential
*
* OCO Source Materials
*
* WLP Copyright IBM Corp. 2018
*
* The source code for this program is not published or otherwise divested
* of its trade secrets, irrespective of what has been deposited with the
* U.S. Copyright Office.
*/
package com.ibm.ws.microprofile.openapi.validation.test;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.microprofile.openapi.models.examples.Example;
import org.eclipse.microprofile.openapi.models.headers.Header;
import org.eclipse.microprofile.openapi.models.headers.Header.Style;
import org.junit.Test;

import com.ibm.ws.microprofile.openapi.impl.model.OpenAPIImpl;
import com.ibm.ws.microprofile.openapi.impl.model.examples.ExampleImpl;
import com.ibm.ws.microprofile.openapi.impl.model.headers.HeaderImpl;
import com.ibm.ws.microprofile.openapi.impl.model.media.ContentImpl;
import com.ibm.ws.microprofile.openapi.impl.model.media.MediaTypeImpl;
import com.ibm.ws.microprofile.openapi.impl.model.media.SchemaImpl;
import com.ibm.ws.microprofile.openapi.impl.validation.HeaderValidator;
import com.ibm.ws.microprofile.openapi.test.utils.TestValidationContextHelper;
import com.ibm.ws.microprofile.openapi.test.utils.TestValidationHelper;
import com.ibm.ws.microprofile.openapi.utils.OpenAPIModelWalker.Context;

import junit.framework.Assert;

/**
*
*/
public class HeaderValidatorTest {

String key = null;
OpenAPIImpl model = new OpenAPIImpl();
Context context = new TestValidationContextHelper(model);

@Test
public void testHeaderIsNull() {

HeaderImpl headerIsNull = null;

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, headerIsNull);

//Check for number of events only to keep assert statement independent of error message
Assert.assertFalse(vh.hasEvents());
}

@Test
public void testExampleAndExamplesNotNull() {

HeaderImpl exampleAndExamplesNotNull = new HeaderImpl();
exampleAndExamplesNotNull.setExample("testExample");
Map<String, Example> examples = new HashMap<String, Example>();
ExampleImpl example = new ExampleImpl();
example.setDescription("testExample");
examples.put(key, example);
exampleAndExamplesNotNull.setExamples(examples);
SchemaImpl schema = new SchemaImpl();
schema.setName("testSchema");
exampleAndExamplesNotNull.setSchema(schema);

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, exampleAndExamplesNotNull);

//Check for number of events only to keep assert statement independent of error message
Assert.assertEquals(1, vh.getEventsSize());

}

@Test
public void testSchemaAndContentNull() {

HeaderImpl schemaAndContentNull = new HeaderImpl();

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, schemaAndContentNull);

//Check for number of events only to keep assert statement independent of error message
Assert.assertEquals(1, vh.getEventsSize());

}

@Test
public void testSchemaAndContentNotNull() {

HeaderImpl schemaAndContentNotNull = new HeaderImpl();

SchemaImpl schema = new SchemaImpl();
schema.setName("testSchema");
schemaAndContentNotNull.setSchema(schema);

ContentImpl content = new ContentImpl();
MediaTypeImpl firstType = new MediaTypeImpl();
content.put("first", firstType);
schemaAndContentNotNull.setContent(content);

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, schemaAndContentNotNull);

//Check for number of events only to keep assert statement independent of error message
Assert.assertEquals(1, vh.getEventsSize());
}

@Test
public void testContentMapWithTwoEntries() {

HeaderImpl contentMapWithTwoEntries = new HeaderImpl();

ContentImpl content = new ContentImpl();
MediaTypeImpl firstType = new MediaTypeImpl();
MediaTypeImpl secondType = new MediaTypeImpl();
content.put("first", firstType);
content.put("second", secondType);
contentMapWithTwoEntries.setContent(content);

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, contentMapWithTwoEntries);

//Check for number of events only to keep assert statement independent of error message
Assert.assertEquals(1, vh.getEventsSize());

}

@Test
public void testCorrectHeader() {

HeaderImpl correctHeader = new HeaderImpl();

Style style = Header.Style.SIMPLE;

SchemaImpl schema = new SchemaImpl();
schema.setName("testSchema");
correctHeader.setSchema(schema);

correctHeader.setStyle(style);

TestValidationHelper vh = new TestValidationHelper();
HeaderValidator validator = HeaderValidator.getInstance();
validator.validate(vh, context, key, correctHeader);

//Check for number of events only to keep assert statement independent of error message
Assert.assertEquals(0, vh.getEventsSize());

}

}

0 comments on commit 94011c1

Please sign in to comment.