urn_perun_resource_attribute_def_def_defaultShell extends ResourceAttributesModuleAbstract implements ResourceAttributesModuleImplApi { @Override public void checkAttributeSemantics(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException { if (attribute.getValue() == null) { throw new WrongReferenceAttributeValueException(attribute, null, resource, null, "Attribute value is null."); } Attribute resourceAttr; try { resourceAttr = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_shells); } catch (AttributeNotExistsException ex) { throw new InternalErrorException("Attribute with list of shells from resource " + resource.getId() + " could not obtained.", ex); } if (resourceAttr.getValue() == null) { throw new WrongReferenceAttributeValueException(resourceAttr, null, resource, null, "Attribute with list of shells from resource has null value."); } List<String> shells = resourceAttr.valueAsList(); if (!shells.contains(attribute.valueAsString())) { throw new WrongReferenceAttributeValueException(attribute, resourceAttr, resource, null, resource, null, "Shell " + attribute.getValue() + " is not at specified resource (" + resource + ")"); } }  @Override Attribute fillAttribute(PerunSessionImpl perunSession, Resource resource, AttributeDefinition attribute); @Override void checkAttributeSemantics(PerunSessionImpl perunSession, Resource resource, Attribute attribute); @Override List<String> getDependencies(); @Override AttributeDefinition getAttributeDefinition();  }
@SuppressWarnings("serial") @Test public void checkAttributeSemantics() throws Exception { System.out.println("checkAttributeSemantics()"); final Attribute attrToReturn = new Attribute(); final String shellName = "mujShell"; attrToReturn.setValue(new ArrayList<String>(){{add(shellName);}}); when(ps.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(attrToReturn); final Attribute attribute = new Attribute(); attribute.setValue(shellName); defShellAttr.checkAttributeSemantics(ps, new Resource(), attribute); }
@Test(expected=WrongReferenceAttributeValueException.class) public void checkAttributeWithoutValue() throws Exception { System.out.println("checkAttributeWithoutValue()"); defShellAttr.checkAttributeSemantics(ps, new Resource(), new Attribute()); }
@Test public void checkAttributeSemanticsWhichNotExists() throws Exception { System.out.println("checkAttributeSemanticsWhichNotExists()"); when(ps.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenThrow(new AttributeNotExistsException("neexistuje")); final Attribute attribute = new Attribute(); attribute.setValue("mujShell"); try { defShellAttr.checkAttributeSemantics(ps, new Resource(), attribute); fail(); } catch (InternalErrorException ex) { assertTrue("Mela byt vyhozena vyjimka AttributeNotExistsException", (ex.getCause() instanceof AttributeNotExistsException)); } }
@Test(expected=WrongReferenceAttributeValueException.class) public void checkAttributeSemanticsWithWrongReference() throws Exception { System.out.println("checkAttributeSemanticsWithWrongReference()"); final Attribute toReturn = new Attribute(); when(ps.getPerunBl().getAttributesManagerBl().getAttribute(any(PerunSession.class), any(Resource.class), anyString())).thenReturn(toReturn); final Attribute attribute = new Attribute(); attribute.setValue("mujShell"); defShellAttr.checkAttributeSemantics(ps, new Resource(), attribute); }