StorageCapabilitiesDeserializer implements JsonDeserializer<StorageCapabilities> { @Override public StorageCapabilities deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonArray()) { StorageCapabilities storageCapabilities = new StorageCapabilities(); storageCapabilities.setControllerModes(Lists.newArrayList(ControllerMode.RAID)); storageCapabilities.setDriveTechnologies(new ArrayList<DriveTechnology>()); storageCapabilities.setRaidLevels(new ArrayList<RaidLevel>()); Iterator<JsonElement> iterator = json.getAsJsonArray().iterator(); while (iterator.hasNext()) { JsonElement element = iterator.next(); if (!element.isJsonPrimitive()) { throw new JsonParseException("Expected a primitive (String) value but found " + element.toString()); } try { storageCapabilities.getRaidLevels().add(RaidLevel.valueOf(element.getAsString())); } catch (IllegalArgumentException e) { throw new JsonParseException("Unknown RAID Level", e); } } return storageCapabilities; } return new GsonBuilder().create().fromJson(json, StorageCapabilities.class); }  @Override StorageCapabilities deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context);  }
@Test(expected = JsonParseException.class) public void shouldThrowParserExceptionWhenDeserializeStorageCapabilitiesV3ContainingInvalidRaidLevel() { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(STORAGE_CAPABILITIES_V3_WITH_INVALID_RAID_LEVEL); deserializer.deserialize(jsonElement, StorageCapabilities.class, null); }
@Test(expected = JsonParseException.class) public void shouldThrowParserExceptionWhenDeserializeStorageCapabilitiesV3ContainingObjectRaidLevel() { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(STORAGE_CAPABILITIES_V3_WITH_OBJECT_RAID_LEVEL); deserializer.deserialize(jsonElement, StorageCapabilities.class, null); }
@Test public void shouldDeserializeStorageCapabilitiesV3() { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(STORAGE_CAPABILITIES_V3); StorageCapabilities capabilities = deserializer.deserialize(jsonElement, StorageCapabilities.class, null); assertThat(capabilities, is(notNullValue())); assertThat(capabilities.getRaidLevels(), containsInAnyOrder(RaidLevel.values())); assertThat(capabilities.getControllerModes(), contains(ControllerMode.RAID)); assertThat(capabilities.getDriveTechnologies(), is(Matchers.<DriveTechnology>empty())); }
@Test public void shouldDeserializeStorageCapabilitiesV4() { JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(STORAGE_CAPABILITIES_V4); StorageCapabilities capabilities = deserializer.deserialize(jsonElement, StorageCapabilities.class, null); assertThat(capabilities, is(notNullValue())); assertThat(capabilities.getRaidLevels(), containsInAnyOrder(RaidLevel.values())); assertThat(capabilities.getControllerModes(), containsInAnyOrder(ControllerMode.values())); assertThat(capabilities.getDriveTechnologies(), containsInAnyOrder(DriveTechnology.values())); assertThat(capabilities.getMaximumDrives(), is(Integer.valueOf(4))); }