### Question:
ScriptConfigurationService implements ScriptingService { @Override public ScriptConfiguration create(ScriptConfiguration config) throws ScriptException { failIfUuidExists(config.getId()); failIfNameExists(config.getName()); final ScriptConfiguration updatedConfig = setMetaData(config); dataStore.save(updatedConfig); return updatedConfig; } @Inject  ScriptConfigurationService(@Named("ScriptLogger") Logger logger,
                                      @Assisted Subject subject, @Assisted String realm,
                                      ScriptingDataStoreFactory dataStoreFactory); @Override ScriptConfiguration create(ScriptConfiguration config); @Override void delete(String uuid); @Override Set<ScriptConfiguration> getAll(); @Override Set<ScriptConfiguration> get(QueryFilter<String> queryFilter); @Override ScriptConfiguration get(String uuid); @Override ScriptConfiguration update(ScriptConfiguration config);  }### Answer:
@Test public void shouldModifyMetaDataOnCreate() throws ScriptException { ScriptConfiguration sc = ScriptConfiguration.builder() .generateId() .setName("MyJavaScript") .setDescription("This is a test script configuration") .setScript("var a = 123;var b = 456;") .setLanguage(JAVASCRIPT) .setContext(POLICY_CONDITION).build(); when(dataStore.containsUuid(anyString())).thenReturn(false); when(dataStore.containsName(anyString())).thenReturn(false); sc = service.create(sc); assertNotNull(sc.getCreatedBy()); assertEquals(sc.getCreatedBy(), TEST_USER); assertNotNull(sc.getCreationDate()); assertNotEquals(sc.getCreationDate(), 0); assertNotNull(sc.getLastModifiedBy()); assertEquals(sc.getLastModifiedBy(), TEST_USER); assertNotNull(sc.getLastModifiedDate()); assertNotEquals(sc.getLastModifiedDate(), 0); }
@Test public void shouldFailIfNameExistsOnCreate() throws ScriptException { ScriptConfiguration sc = ScriptConfiguration.builder() .generateId() .setName("MyJavaScript") .setDescription("This is a test script configuration") .setScript("var a = 123;var b = 456;") .setLanguage(JAVASCRIPT) .setContext(POLICY_CONDITION).build(); when(dataStore.containsUuid(anyString())).thenReturn(false); when(dataStore.containsName(anyString())).thenReturn(true); try { service.create(sc); fail("shouldFailIfNameExistsOnCreate"); } catch (ScriptException e) { assertEquals(e.getScriptErrorCode(), SCRIPT_NAME_EXISTS); } }
@Test public void shouldFailIfUuidExistsOnCreate() throws ScriptException { ScriptConfiguration sc = ScriptConfiguration.builder() .generateId() .setName("MyJavaScript") .setDescription("This is a test script configuration") .setScript("var a = 123;var b = 456;") .setLanguage(JAVASCRIPT) .setContext(POLICY_CONDITION).build(); when(dataStore.containsUuid(anyString())).thenReturn(true); when(dataStore.containsName(anyString())).thenReturn(false); try { service.create(sc); fail("shouldFailIfUuidExistsOnCreate"); } catch (ScriptException e) { assertEquals(e.getScriptErrorCode(), SCRIPT_UUID_EXISTS); } }