ExtensionMethod { public JSONObject getExtensionApiAsJson() { JSONObject fullApi = null; final HashMap<String, Object> map = new HashMap<String, Object>(); if (this.extensionDescriptor != null) { map.put(Tokens.DESCRIPTION, this.extensionDescriptor.description()); JSONObject api = null; final HashMap<String, String> innerMap = new HashMap<String, String>(); if (this.extensionDescriptor.apiBehavior() == ExtensionApiBehavior.DEFAULT || this.extensionDescriptor.apiBehavior() == ExtensionApiBehavior.EXTENSION_DESCRIPTOR_ONLY) { if (this.extensionDescriptor.api().length > 0) { for (ExtensionApi apiItem : this.extensionDescriptor.api()) { innerMap.put(apiItem.parameterName(), apiItem.description()); } } } if (this.extensionDescriptor.apiBehavior() == ExtensionApiBehavior.DEFAULT || this.extensionDescriptor.apiBehavior() == ExtensionApiBehavior.EXTENSION_PARAMETER_ONLY) { final Annotation[][] parametersAnnotations = method.getParameterAnnotations(); for (int ix = 0; ix < parametersAnnotations.length; ix++) { final Annotation[] annotation = parametersAnnotations[ix]; if (annotation != null && annotation.length > 0 && annotation[0] instanceof ExtensionRequestParameter) { final ExtensionRequestParameter extensionRequestParameter = (ExtensionRequestParameter) annotation[0]; innerMap.put(extensionRequestParameter.name(), extensionRequestParameter.description()); } } } if (!innerMap.isEmpty()) { api = new JSONObject(innerMap); map.put(Tokens.PARAMETERS, api); } fullApi = new JSONObject(map); } return fullApi; } ExtensionMethod(final Method method, final ExtensionDefinition extensionDefinition,
                           final ExtensionDescriptor extensionDescriptor,
                           final RexsterExtension rexsterExtension); Method getMethod(); ExtensionDefinition getExtensionDefinition(); RexsterExtension getRexsterExtension(); JSONObject getExtensionApiAsJson();  }
@Test public void getExtensionApiAsJsonNullDescriptor() { ExtensionMethod method = new ExtensionMethod(null, null, null, null); JSONObject api = method.getExtensionApiAsJson(); Assert.assertNull(api); }
@Test public void getExtensionApiAsJsonDescriptorOnlyNoApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[0])); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.EXTENSION_DESCRIPTOR_ONLY)); }}); ExtensionMethod extensionMethod = new ExtensionMethod(null, null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertFalse(api.has(Tokens.PARAMETERS)); }
@Test public void getExtensionApiAsJsonParameterOnlyNoApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[0])); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.EXTENSION_PARAMETER_ONLY)); }}); Method[] methods = ExtensionMethodTest.class.getMethods(); ExtensionMethod extensionMethod = new ExtensionMethod(methods[0], null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertFalse(api.has(Tokens.PARAMETERS)); }
@Test public void getExtensionApiAsJsonDefaultNoApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[0])); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.DEFAULT)); }}); Method[] methods = ExtensionMethodTest.class.getMethods(); ExtensionMethod extensionMethod = new ExtensionMethod(methods[0], null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertFalse(api.has(Tokens.PARAMETERS)); }
@Test public void getExtensionApiAsJsonDescriptorOnlyHasApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); final ExtensionApi extensionApi = this.mockery.mock(ExtensionApi.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[]{extensionApi})); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.EXTENSION_DESCRIPTOR_ONLY)); allowing(extensionApi).parameterName(); will(returnValue("param1")); allowing(extensionApi).description(); will(returnValue("value1")); }}); ExtensionMethod extensionMethod = new ExtensionMethod(null, null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertTrue(api.has(Tokens.PARAMETERS)); JSONObject params = api.optJSONObject(Tokens.PARAMETERS); Assert.assertNotNull(params); Assert.assertTrue(params.has("param1")); Assert.assertEquals("value1", params.optString("param1")); }
@Test public void getExtensionApiAsJsonParameterOnlyHasApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); final ExtensionApi extensionApi = this.mockery.mock(ExtensionApi.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[]{extensionApi})); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.EXTENSION_PARAMETER_ONLY)); }}); Method[] methods = MockMethodHelper.class.getDeclaredMethods(); ExtensionMethod extensionMethod = new ExtensionMethod(methods[0], null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertTrue(api.has(Tokens.PARAMETERS)); JSONObject params = api.optJSONObject(Tokens.PARAMETERS); Assert.assertNotNull(params); Assert.assertTrue(params.has("nme")); Assert.assertEquals("dsc", params.optString("nme")); }
@Test public void getExtensionApiAsJsonDefaultHasApiElements() { this.mockery = new JUnit4Mockery(); final ExtensionDescriptor extensionDescriptor = this.mockery.mock(ExtensionDescriptor.class); final ExtensionApi extensionApi = this.mockery.mock(ExtensionApi.class); this.mockery.checking(new Expectations() {{ allowing(extensionDescriptor).description(); will(returnValue("desc")); allowing(extensionDescriptor).api(); will(returnValue(new ExtensionApi[]{extensionApi})); allowing(extensionDescriptor).apiBehavior(); will(returnValue(ExtensionApiBehavior.DEFAULT)); allowing(extensionApi).parameterName(); will(returnValue("param1")); allowing(extensionApi).description(); will(returnValue("value1")); }}); Method[] methods = MockMethodHelper.class.getDeclaredMethods(); ExtensionMethod extensionMethod = new ExtensionMethod(methods[0], null, extensionDescriptor, null); JSONObject api = extensionMethod.getExtensionApiAsJson(); Assert.assertNotNull(api); Assert.assertTrue(api.has(Tokens.DESCRIPTION)); Assert.assertEquals("desc", api.opt(Tokens.DESCRIPTION)); Assert.assertTrue(api.has(Tokens.PARAMETERS)); JSONObject params = api.optJSONObject(Tokens.PARAMETERS); Assert.assertNotNull(params); Assert.assertTrue(params.has("nme")); Assert.assertEquals("dsc", params.optString("nme")); Assert.assertNotNull(params); Assert.assertTrue(params.has("param1")); Assert.assertEquals("value1", params.optString("param1")); }