usr/src/test/python/client/test_derivedtypes.py
author devjani.ray@oracle.com <devjani.ray@oracle.com>
Sun, 15 Apr 2012 13:51:55 -0400
changeset 843 8d361501a790
parent 842 abc3d63bd4da
child 849 e46ba1c7ace1
permissions -rw-r--r--
7157438 RAD: need to change name of adr password type

#!/usr/bin/python2.6
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
#

import os
import sys
import unittest
import common.tests as tests
import rad.client as rad
import rad.adaptor as adapt
import rad.util as util

#
# Test derived types
#

class TypeTest(tests.RADTestObject):
	""" Derived type functionality """

	#
	# Constants
	#

	# Note: enumfield, structfield values cannot be defined as constants
	VALUE_INTFIELD = 5
	VALUE_SF_STRINGFIELD = "test"
	VALUE_STRINGARRAY_0 = "a"
	VALUE_STRINGARRAY_1 = "b"
	VALUE_STRINGARRAY_2 = "c"
	VALUE_STRINGARRAY = [ VALUE_STRINGARRAY_0, VALUE_STRINGARRAY_1,
	    VALUE_STRINGARRAY_2 ]
	LEN_STRINGARRAY = len(VALUE_STRINGARRAY)
	LEN_STRUCT = 4

	#
	# Fixtures
	#

	def get_keys(self):
		return [("type", "derivedTypes")]

	#
	# Utility methods
	#

	def writeTest(self, prop, eval):
		self._obj.write(prop, eval)

	def badWriteTest(self, prop, eval):
		try:
			self._obj.write(prop, eval)
		except rad.ObjectError:
			pass
		else:
			self.fail(
			    "write of \"%s\" to %s succeeded" % (eval, prop))

	#
	# Test methods
	#

	def test_structure_read(self):
		""" read of structProp returns correct value """
		
		types = self._obj.types

		val = self._obj.read("structProp")
		self.assertEqual(val.intfield, self.VALUE_INTFIELD)
		self.assertEqual(val.enumfield, types.SubEnum.val_one)
		self.assertEqual(val.structfield.stringfield,
		    self.VALUE_SF_STRINGFIELD)
		self.assertEqual(val.stringarray[0], self.VALUE_STRINGARRAY_0)
		self.assertEqual(val.stringarray[1], self.VALUE_STRINGARRAY_1)
		self.assertEqual(val.stringarray[2], self.VALUE_STRINGARRAY_2)
		self.assertEqual(len(val.stringarray), self.LEN_STRINGARRAY)
		self.assertEqual(len(val.__dict__.keys()), self.LEN_STRUCT + 1)

	def test_structure_write(self):
		""" write to structProp writes correct value """

		types = self._obj.types

		val = types.ComplexStruct(self.VALUE_INTFIELD,
		    types.SubStruct(self.VALUE_SF_STRINGFIELD),
		    self.VALUE_STRINGARRAY, types.SubEnum.val_one)
		self.writeTest("structProp", val)

	def test_structure_write_read(self):
		""" write to structProp writes correct value """

		val = self._obj.read("structProp")
		self.writeTest("structProp", val)

	def test_structure_write_tagged(self):
		""" write to structProp writes correct value """

		types = self._obj.types

		val = types.ComplexStruct(
		    enumfield = types.SubEnum.val_one,
		    stringarray = self.VALUE_STRINGARRAY,
		    structfield = types.SubStruct(self.VALUE_SF_STRINGFIELD),
		    intfield = self.VALUE_INTFIELD)
		self.writeTest("structProp", val)

	def test_structure_write_tagged_mixed(self):
		""" write to structProp writes correct value """

		types = self._obj.types

		val = types.ComplexStruct(self.VALUE_INTFIELD,
		    types.SubStruct(self.VALUE_SF_STRINGFIELD),
		    self.VALUE_STRINGARRAY,
		    enumfield = types.SubEnum.val_one)
		self.writeTest("structProp", val)

	def test_structure_write_bad_mismatch(self):
		""" write to structProp of bad (but legal) value fails """

		types = self._obj.types

		val = types.ComplexStruct(
		    enumfield = types.SubEnum.val_one,
		    stringarray = self.VALUE_STRINGARRAY,
		    intfield = self.VALUE_INTFIELD)
		self.badWriteTest("structProp", val)

	def test_structure_construct_bad_extra(self):
		""" extra field causes failure """

		types = self._obj.types

		try:
			val = types.ComplexStruct(self.VALUE_INTFIELD,
			    types.SubStruct(self.VALUE_SF_STRINGFIELD),
			    self.VALUE_STRINGARRAY, types.SubEnum.val_one,
			    "surprise!")
		except Exception, ex:
			pass
		else:
			self.fail("failed to identify extra member")

	def test_structure_construct_bad_extra_labeled(self):
		""" extra labeled field causes failure """

		types = self._obj.types

		try:
			val = types.ComplexStruct(self.VALUE_INTFIELD,
			    types.SubStruct(self.VALUE_SF_STRINGFIELD),
			    self.VALUE_STRINGARRAY,
			    bogusfield = "surprise!",
			    enumfield = types.SubEnum.val_one)
		except Exception, ex:
			pass
		else:
			self.fail("failed to identify extra member")

	def test_structure_construct_bad_missing(self):
		""" missing non-nullable field causes failure """

		types = self._obj.types

		try:
			val = types.ComplexStruct(self.VALUE_INTFIELD,
			    types.SubStruct(self.VALUE_SF_STRINGFIELD),
			    enumfield = types.SubEnum.val_one)
		except Exception, ex:
			pass
		else:
			self.fail("failed to identify missing member")

	def test_structure_construct_bad_double(self):
		""" double-specified field causes failure """

		types = self._obj.types

		try:
			val = types.ComplexStruct(self.VALUE_INTFIELD,
			    types.SubStruct(self.VALUE_SF_STRINGFIELD),
			    self.VALUE_STRINGARRAY, types.SubEnum.val_one,
			    intfield = self.VALUE_INTFIELD)
		except Exception, ex:
			pass
		else:
			self.fail("failed to identify missing member")