Merge "Support for SO to ExtAPI"
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / validation / UserParamsValidationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandlerinfra.validation;
22
23 import java.io.IOException;
24 import java.nio.file.Files;
25 import java.nio.file.Paths;
26 import java.util.Map;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.onap.so.apihandlerinfra.Action;
31 import org.onap.so.exceptions.ValidationException;
32 import org.onap.so.serviceinstancebeans.Service;
33 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
34 import com.fasterxml.jackson.databind.ObjectMapper;
35
36 public class UserParamsValidationTest {
37
38     UserParamsValidation validation = new UserParamsValidation();
39
40     @Rule
41     public ExpectedException thrown = ExpectedException.none();
42
43     public ValidationInformation setupValidationInformation(String path) throws IOException {
44         String jsonInput = new String(Files.readAllBytes(Paths.get(path)));
45         ObjectMapper mapper = new ObjectMapper();
46         ServiceInstancesRequest sir = mapper.readValue(jsonInput, ServiceInstancesRequest.class);
47         ValidationInformation info = new ValidationInformation(sir, null, Action.createInstance, 7, false,
48                 sir.getRequestDetails().getRequestParameters());
49         for (Map<String, Object> params : sir.getRequestDetails().getRequestParameters().getUserParams()) {
50             ObjectMapper obj = new ObjectMapper();
51             String input = obj.writeValueAsString(params.get("service"));
52             Service validate = obj.readValue(input, Service.class);
53             info.setUserParams(validate);
54             break;
55         }
56         info.setRequestInfo(sir.getRequestDetails().getRequestInfo());
57         return info;
58     }
59
60     @Test
61     public void validateModelTypeExceptionTest() throws IOException, ValidationException {
62         thrown.expect(ValidationException.class);
63         thrown.expectMessage("No valid modelType in userParams service modelInfo is specified");
64         validation.validate(setupValidationInformation(
65                 "src/test/resources/Validation/UserParamsValidation/ModelInfoNoModelType.json"));
66     }
67
68     @Test
69     public void validateInstanceNameExceptionTest() throws IOException, ValidationException {
70         thrown.expect(ValidationException.class);
71         thrown.expectMessage("instanceName in requestInfo does not match instanceName in userParams service");
72         validation.validate(
73                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/MacroRequest.json"));
74     }
75
76     @Test
77     public void validateModelTypeTest() throws ValidationException, IOException {
78         thrown.expect(ValidationException.class);
79         thrown.expectMessage("modelType in modelInfo does not match modelType in userParams service");
80         validation.validate(
81                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/ModelType.json"));
82     }
83
84     @Test
85     public void validateModelInvariantIdTest() throws ValidationException, IOException {
86         thrown.expect(ValidationException.class);
87         thrown.expectMessage("modelInvariantId in modelInfo does not match modelInvariantId in userParams service");
88         validation.validate(
89                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/ModelInvariantId.json"));
90     }
91
92     @Test
93     public void validateModelVersionIdTest() throws ValidationException, IOException {
94         thrown.expect(ValidationException.class);
95         thrown.expectMessage("modelVersionId in modelInfo does not match modelVersionId in userParams service");
96         validation.validate(
97                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/ModelVersionId.json"));
98     }
99
100     @Test
101     public void validateModelNameTest() throws ValidationException, IOException {
102         thrown.expect(ValidationException.class);
103         thrown.expectMessage("modelName in modelInfo does not match modelName in userParams service");
104         validation.validate(
105                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/ModelName.json"));
106     }
107
108     @Test
109     public void validateModelVersionTest() throws ValidationException, IOException {
110         thrown.expect(ValidationException.class);
111         thrown.expectMessage("modelVersion in modelInfo does not match modelVersion in userParams service");
112         validation.validate(
113                 setupValidationInformation("src/test/resources/Validation/UserParamsValidation/ModelVersion.json"));
114     }
115
116     @Test
117     public void validateModelCustomizationIdTest() throws ValidationException, IOException {
118         thrown.expect(ValidationException.class);
119         thrown.expectMessage(
120                 "modelCustomizationId in modelInfo does not match modelCustomizationId in userParams service");
121         validation.validate(setupValidationInformation(
122                 "src/test/resources/Validation/UserParamsValidation/ModelCustomizationId.json"));
123     }
124 }