Fixing Blueprint Typo's and docs
[ccsdk/cds.git] / ms / sdclistener / application / src / test / java / org / onap / ccsdk / cds / sdclistener / handler / BluePrintProcessorHandlerTest.java
1 /*
2  * Copyright © 2019 Bell Canada
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.sdclistener.handler;
18
19 import com.google.protobuf.ByteString;
20 import io.grpc.ManagedChannel;
21 import io.grpc.inprocess.InProcessChannelBuilder;
22 import io.grpc.inprocess.InProcessServerBuilder;
23 import io.grpc.stub.StreamObserver;
24 import io.grpc.testing.GrpcCleanupRule;
25 import org.apache.commons.io.FileUtils;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
32 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
34 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
37 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction;
38 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.boot.context.properties.EnableConfigurationProperties;
41 import org.springframework.boot.test.context.SpringBootTest;
42 import org.springframework.test.context.junit4.SpringRunner;
43
44 import java.io.File;
45 import java.io.IOException;
46 import java.nio.file.Paths;
47 import java.util.UUID;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertTrue;
51
52 @RunWith(SpringRunner.class)
53 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, SdcListenerAuthClientInterceptor.class})
54 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
55 public class BluePrintProcessorHandlerTest {
56
57     @Autowired
58     private BluePrintProcesssorHandler bluePrintProcesssorHandler;
59
60     @Autowired
61     private SdcListenerAuthClientInterceptor sdcListenerAuthClientInterceptor;
62
63     @Rule
64     public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
65
66     private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
67     private static final String SUCCESS_MSG = "Successfully uploaded CBA";
68     private static final int SUCCESS_CODE = 200;
69     private ManagedChannel channel;
70
71     @Before
72     public void setUp() throws IOException {
73         final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
74             @Override
75             public void uploadBlueprint(BluePrintUploadInput request,
76                     StreamObserver<BluePrintManagementOutput> responseObserver) {
77                 responseObserver.onNext(getBluePrintManagementOutput());
78                 responseObserver.onCompleted();
79             }
80         };
81
82         // Generate server name.
83         String serverName = InProcessServerBuilder.generateName();
84
85         // Create a server, add service, start, and register.
86         grpcCleanup.register(InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor()
87                 .build().start());
88
89         // Create a client channel.
90         channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
91     }
92
93     @Test
94     public void testApplicationEndPointSucess() throws IOException {
95         // Arrange
96         BluePrintUploadInput request = generateRequest();
97
98         // Act
99         Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
100
101         // Verify
102         assertEquals(SUCCESS_CODE, output.getCode());
103         assertTrue(output.getMessage().contains(SUCCESS_MSG));
104     }
105
106     private BluePrintUploadInput generateRequest() throws IOException {
107         File file = Paths.get(CBA_ARCHIVE).toFile();
108         byte[] bytes = FileUtils.readFileToByteArray(file);
109         FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
110
111         return BluePrintUploadInput.newBuilder()
112                 .setCommonHeader(CommonHeader.newBuilder().setRequestId(UUID.randomUUID().toString())
113                         .setSubRequestId(UUID.randomUUID().toString()).setOriginatorId("SDC-LISTENER").build())
114                 .setActionIdentifiers(
115                         ActionIdentifiers.newBuilder().setActionName(UploadAction.PUBLISH.toString()).build())
116                 .setFileChunk(fileChunk).build();
117     }
118
119     private BluePrintManagementOutput getBluePrintManagementOutput() {
120         return BluePrintManagementOutput.newBuilder()
121                 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build()).build();
122     }
123
124 }