import java.util.Calendar;
import java.util.Date;
import java.util.List;
-import org.junit.Ignore;
import org.junit.Test;
import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
/**
- * Unit test for {@link RestconfValidation}
+ * Unit test for {@link RestconfValidation}.
*/
public class RestconfValidationTest {
- private static final List<String> revisions = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
- private static final List<String> names = Arrays.asList("module1", "module2", "module3");
+ private static final List<String> REVISIONS = Arrays.asList("2014-01-01", "2015-01-01", "2016-01-01");
+ private static final List<String> NAMES = Arrays.asList("_module-1", "_module-2", "_module-3");
/**
* Test of successful validation of module revision.
*/
@Test
public void validateAndGetRevisionTest() {
- Date revision = RestconfValidation.validateAndGetRevision(revisions.iterator());
+ Date revision = RestconfValidation.validateAndGetRevision(REVISIONS.iterator());
assertNotNull("Correct module revision should be validated", revision);
- Calendar c = Calendar.getInstance();
- c.setTime(revision);
+ Calendar calendar = Calendar.getInstance();
+ calendar.setTime(revision);
- assertEquals(2014, c.get(Calendar.YEAR));
- assertEquals(0, c.get(Calendar.MONTH));
- assertEquals(1, c.get(Calendar.DAY_OF_MONTH));
+ assertEquals(2014, calendar.get(Calendar.YEAR));
+ assertEquals(0, calendar.get(Calendar.MONTH));
+ assertEquals(1, calendar.get(Calendar.DAY_OF_MONTH));
}
/**
*/
@Test
public void validateAndGetModulNameTest() {
- String moduleName = RestconfValidation.validateAndGetModulName(names.iterator());
+ String moduleName = RestconfValidation.validateAndGetModulName(NAMES.iterator());
assertNotNull("Correct module name should be validated", moduleName);
- assertEquals("module1", moduleName);
+ assertEquals("_module-1", moduleName);
}
/**
}
/**
- * Negative test of module name validation when supplied name is not parsable as module name. Test fails
- * catching <code>RestconfDocumentedException</code>.
- * <p>
- * This test is ignored because tested functionality is not implemented yet.
+ * Negative test of module name validation when supplied name is not parsable as module name on the first
+ * character. Test fails catching <code>RestconfDocumentedException</code> and checking for correct error type,
+ * error tag and error status code.
+ */
+ @Test
+ public void validateAndGetModuleNameNotParsableFirstTest() {
+ try {
+ RestconfValidation.validateAndGetModulName(
+ Arrays.asList("01-not-parsable-as-name-on-firts-char").iterator());
+ fail("Test should fail due to not parsable module name on the first character");
+ } catch (RestconfDocumentedException e) {
+ assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
+ assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
+ assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
+ }
+ }
+
+ /**
+ * Negative test of module name validation when supplied name is not parsable as module name on any of the
+ * characters after the first character. Test fails catching <code>RestconfDocumentedException</code> and checking
+ * for correct error type, error tag and error status code.
+ */
+ @Test
+ public void validateAndGetModuleNameNotParsableNextTest() {
+ try {
+ RestconfValidation.validateAndGetModulName(
+ Arrays.asList("not-parsable-as-name-after-first-char*").iterator());
+ fail("Test should fail due to not parsable module name on any character after the first character");
+ } catch (RestconfDocumentedException e) {
+ assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
+ assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
+ assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
+ }
+ }
+
+ /**
+ * Negative test of module name validation when supplied name begins with 'XML' ignore case. Test fails catching
+ * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
+ */
+ @Test
+ public void validateAndGetModuleNameNotParsableXmlTest() {
+ try {
+ RestconfValidation.validateAndGetModulName(Arrays.asList("xMl-module-name").iterator());
+ fail("Test should fail due to module name beginning with 'xMl'");
+ } catch (RestconfDocumentedException e) {
+ assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
+ assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
+ assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
+ }
+ }
+
+ /**
+ * Negative test of module name validation when supplied name is empty. Test fails catching
+ * <code>RestconfDocumentedException</code> and checking for correct error type, error tag and error status code.
*/
- @Ignore
- @Test(expected = RestconfDocumentedException.class)
- public void validateAndGetModuleNameNotParsableTest() {}
+ @Test
+ public void validateAndGetModuleNameEmptyTest() {
+ try {
+ RestconfValidation.validateAndGetModulName(Arrays.asList("").iterator());
+ fail("Test should fail due to empty module name");
+ } catch (RestconfDocumentedException e) {
+ assertEquals(RestconfError.ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());
+ assertEquals(RestconfError.ErrorTag.INVALID_VALUE, e.getErrors().get(0).getErrorTag());
+ assertEquals(400, e.getErrors().get(0).getErrorTag().getStatusCode());
+ }
+ }
}